Class: Cul::LDAP

Inherits:
Net::LDAP
  • Object
show all
Defined in:
lib/cul/ldap.rb,
lib/cul/ldap/version.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

CONFIG_FILENAME =
'cul_ldap.yml'
CONFIG_DEFAULTS =
{
  host: 'ldap.columbia.edu',
  port: '636',
  encryption: {
    method: :simple_tls,
    tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LDAP

Returns a new instance of LDAP.



19
20
21
# File 'lib/cul/ldap.rb', line 19

def initialize(options = {})
  super(build_config(options)) # All keys have to be symbols.
end

Class Method Details

.versionObject



5
6
7
# File 'lib/cul/ldap/version.rb', line 5

def self.version
  IO.read("VERSION").strip
end

Instance Method Details

#find_by_name(name) ⇒ Cul::LDAP::Entry?

LDAP lookup based on name.

Parameters:

  • name (String)

Returns:

  • (Cul::LDAP::Entry)

    containing the entry matching this name, if it is unique

  • (nil)

    if record could not be found or if there is more than one match



38
39
40
41
42
43
44
# 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) ⇒ Cul::LDAP::Entry?

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

Parameters:

  • uni (String)

Returns:

  • (Cul::LDAP::Entry)

    containing all the ldap information available for the uni given

  • (nil)

    if record for uni could not be found, or more than one record was found



28
29
30
31
# 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

#search(args = {}) ⇒ Object

Wrapper around Net::LDAP#search, converts Net::LDAP::Entry objects to Cul::LDAP::Entry objects.



48
49
50
51
52
53
54
# File 'lib/cul/ldap.rb', line 48

def search(args = {})
  super(args).tap do |result|
    if result.is_a?(Array)
      result.map!{ |e| Cul::LDAP::Entry.new(e) }
    end
  end
end