Class: LdapApi

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap.rb

Constant Summary collapse

YOOX =
{
  name: 'YOOX',
  host: 'ydcrootblq.yoox.net',
  base: 'dc=yoox,dc=net',
  port: 389,
  user: ENV['YOOX_BIND_USER'],
  pass: ENV['YOOX_BIND_PASS'],
}
NAP =
{
  name: 'LONDON',
  host: 'RODC02-PR-IMO.london.net-a-porter.com',
  base: 'dc=london,dc=net-a-porter,dc=com',
  port: 389,
  user: ENV['NAP_BIND_USER'],
  pass: ENV['NAP_BIND_PASS'],
}
DOMAINS =
[ YOOX, NAP ]

Instance Method Summary collapse

Instance Method Details

#auth?(username, password) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/ldap.rb', line 44

def auth?(username, password)
  domains.each do |domain|
    domain.ldap.authenticate domain.name + "\\" + username, password
    return true if domain.ldap.bind
  end
  return false
end

#domainsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ldap.rb', line 22

def domains
  domain_structs = DOMAINS.map do |domain|
    d = OpenStruct.new(
      name: domain[:name],
      user: domain[:user],
      pass: domain[:pass],
      ldap: Net::LDAP.new(
        host: domain[:host],
        port: domain[:port],
        base: domain[:base],
        auth: {
          method: :simple,
          username: domain[:user],
          password: domain[:pass],
        },
      )
    )
    raise "BIND ERROR: #{domain}" unless d.ldap.bind
    d
  end
end

#group(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ldap.rb', line 76

def group(name)
  filter = Net::LDAP::Filter.eq("cn", name)
  results = []
  domains.map do |domain|
    domain.ldap.search(filter: filter) do |entry|
      results << entry
    end
    domain.ldap.get_operation_result
  end
  results.flatten
end

#groups(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ldap.rb', line 52

def groups(name)
  filter = Net::LDAP::Filter.eq("sAMAccountName", name)
  results = []
  domains.map do |domain|
    domain.ldap.search(filter: filter) do |entry|
      results << entry.memberof.map {|e| e.sub(/^CN=/,'').sub(/,.*$/,'') }
    end
    domain.ldap.get_operation_result
  end
  results.flatten
end

#in_group?(name, group) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ldap.rb', line 88

def in_group?(name, group)
  groups(name).include?(group)
end

#user(name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ldap.rb', line 64

def user(name)
  filter = Net::LDAP::Filter.eq("sAMAccountName", name)
  results = []
  domains.map do |domain|
    domain.ldap.search(filter: filter) do |entry|
      results << entry
    end
    domain.ldap.get_operation_result
  end
  results.flatten
end

#user_from_name(name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ldap.rb', line 104

def user_from_name(name)
  filter = Net::LDAP::Filter.eq("cn", name)
  results = []
  domains.map do |domain|
    domain.ldap.search(filter: filter) do |entry|
      results << entry[:samaccountname]
    end
    domain.ldap.get_operation_result
  end
  results.flatten
end

#users_in_group(group) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ldap.rb', line 92

def users_in_group(group)
  filter = Net::LDAP::Filter.eq("cn", group)
  results = []
  domains.map do |domain|
    domain.ldap.search(filter: filter) do |entry|
      results << entry.member.map {|e| user_from_name(e.sub(/^CN=/,'').sub(/,.*$/,'')) }
    end
    domain.ldap.get_operation_result
  end
  results.flatten
end