SystemDataBackendLdap.rb
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'active_support/secure_random'
require 'net/ldap'
class SystemDataBackendLdap
LDAP_USER_MAP = {
:uid => :name,
:userpassword => :pass,
:uidnumber => :uid,
:gidnumber => :gid,
:loginshell => :shell,
:homedirectory => :home
}
LDAP_GROUP_MAP = {
:cn => :name,
:gidnumber => :gid,
:memberuid => :members
}
LDAP_SITE_MAP = {:o => :name}
LDAP_MAP = {
:user => LDAP_USER_MAP,
:group => LDAP_GROUP_MAP,
:site => LDAP_SITE_MAP
}
LDAP_FILTER = {
:user => Net::LDAP::Filter::eq('objectClass', 'posixAccount'),
:group => Net::LDAP::Filter::eq('objectClass', 'posixGroup'),
:site => Net::LDAP::Filter::eq('objectClass', 'organization') &
(~Net::LDAP::Filter::eq('o', 'hosting')),
:mailAlias => Net::LDAP::Filter::eq('objectClass', 'mailAlias'),
:mailAccount => Net::LDAP::Filter::eq('objectClass', 'mailAccount')
}
def initialize(host, port, baseDn, args={})
@baseDn = baseDn
@systemDn = 'o=system,' + @baseDn
@hostingDn = 'o=hosting,' + @baseDn
@systemDn = args[:systemDn] if args[:systemDn]
@hostingDn = args[:hostingDn] if args[:hostingDn]
@ldap = Net::LDAP.new(:host => host, :port => port)
@ldapData = Hash.new
end
def load!(kind)
@ldapData[kind] = @ldap.search(
:base => ldapBase(kind),
:filter => LDAP_FILTER[kind]
)
end
def load(kind)
load!(kind) if ! @ldapData[kind]
@ldapData[kind].each do |data|
map = { :dn => :id }
map.merge!(LDAP_MAP[kind]) if LDAP_MAP[kind]
ydata = { :backend => self }
data.each do |key,value|
ydata.merge!({ map[key] || key => value.size==1?value[0]:value.to_a })
end
yield ydata
end
end
private
def ldapBase(kind)
case(kind)
when :user, :group: @systemDn
when :site, :mailAlias, :mailAccount: @hostingDn
end
end
end