class Cul::LDAP

Constants

CONFIG_DEFAULTS
CONFIG_FILENAME

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/cul/ldap.rb, line 19
def initialize(options = {})
  super(build_config(options)) # All keys have to be symbols.
end
version() click to toggle source
# File lib/cul/ldap/version.rb, line 5
def self.version
  IO.read("VERSION").strip
end

Public Instance Methods

find_by_name(name) click to toggle source

LDAP lookup based on name.

@param [String] name @return [Cul::LDAP::Entry] containing the entry matching this name, if it is unique @return [nil] if record could not be found or if there is more than one match

# File lib/cul/ldap.rb, line 38
def find_by_name(name)
  if name.include?(',')
    name = name.split(',').map(&:strip).reverse.join(" ")
  end
  entries = search(base: "ou=People,o=Columbia University, c=US", filter: Net::LDAP::Filter.eq("cn", name))
  (entries.count == 1) ? entries.first : nil
end
find_by_uni(uni) click to toggle source

LDAP lookup based on UNI. If record could not be found returns nil.

@param [String] uni @return [Cul::LDAP::Entry] containing all the ldap information available for the uni given @return [nil] if record for uni could not be found, or more than one record was found

# File lib/cul/ldap.rb, line 28
def find_by_uni(uni)
  entries = search(base: "ou=People,o=Columbia University, c=US", filter: Net::LDAP::Filter.eq("uid", uni))
  (entries.count == 1) ? entries.first : nil
end

Private Instance Methods

build_config(options) click to toggle source
# File lib/cul/ldap.rb, line 58
def build_config(options)
  config = CONFIG_DEFAULTS.merge(options)
  credentials = config.fetch(:auth, nil)
  credentials = nil if !credentials.nil? && credentials.empty?

  # If rails app fetch credentials using rails code, otherwise read from
  # cul_ldap.yml if credentials are nil.
  if credentials.nil?
    credentials = rails_credentials || credentials_from_file
    credentials = nil if !credentials.nil? && credentials.empty?
  end

  unless credentials.nil?
    credentials = credentials.map { |k, v| [k.to_sym, v] }.to_h
    credentials[:method] = :simple unless credentials.key?(:method)
  end

  config[:auth] = credentials
  config
end
credentials_from_file() click to toggle source
# File lib/cul/ldap.rb, line 79
def credentials_from_file
  (File.exist?(CONFIG_FILENAME)) ? YAML.load_file(CONFIG_FILENAME) : nil
end
rails_credentials() click to toggle source
# File lib/cul/ldap.rb, line 83
def rails_credentials
  if defined?(Rails.application.config_for) && File.exist?(File.join(Rails.root, 'config', CONFIG_FILENAME))
    raise "Missing cul-ldap credentials in config/#{CONFIG_FILENAME}" if Rails.application.config_for(:cul_ldap).empty?
    Rails.application.config_for(:cul_ldap)
  else
    nil
  end
end