Commit aab45409ffa88ad13485a3adcb8e175c969e8be5
Committed by
Georg Hopp
1 parent
d7a3f878
add first basic code to create new database entries
Showing
4 changed files
with
71 additions
and
24 deletions
@@ -7,10 +7,18 @@ class SystemData | @@ -7,10 +7,18 @@ class SystemData | ||
7 | end | 7 | end |
8 | 8 | ||
9 | def save | 9 | def save |
10 | + kind = self.class.to_s.to_sym | ||
11 | + | ||
10 | if @id | 12 | if @id |
11 | - @backend.update(self) | 13 | + @backend.update(kind, to_h) |
12 | else | 14 | else |
13 | - @backend.insert(self) | 15 | + @backend.insert(kind, to_h) |
14 | end | 16 | end |
15 | end | 17 | end |
18 | + | ||
19 | + def to_h | ||
20 | + Hash[instance_variables.map do |var| | ||
21 | + [var[1...var.size].to_sym, eval(var)] if var != '@backend' | ||
22 | + end] | ||
23 | + end | ||
16 | end | 24 | end |
@@ -12,23 +12,23 @@ class SystemDataBackend | @@ -12,23 +12,23 @@ class SystemDataBackend | ||
12 | end | 12 | end |
13 | 13 | ||
14 | def users | 14 | def users |
15 | - load(:user) | 15 | + load(:User) |
16 | end | 16 | end |
17 | 17 | ||
18 | def groups | 18 | def groups |
19 | - load(:group) | 19 | + load(:Group) |
20 | end | 20 | end |
21 | 21 | ||
22 | def sites | 22 | def sites |
23 | - load(:site) | 23 | + load(:Site) |
24 | end | 24 | end |
25 | 25 | ||
26 | def mailAliases | 26 | def mailAliases |
27 | - load(:mailAlias) | 27 | + load(:MailAlias) |
28 | end | 28 | end |
29 | 29 | ||
30 | def mailAccounts | 30 | def mailAccounts |
31 | - load(:mailAccount) | 31 | + load(:MailAccount) |
32 | end | 32 | end |
33 | 33 | ||
34 | def userByName(name) | 34 | def userByName(name) |
@@ -70,13 +70,11 @@ class SystemDataBackend | @@ -70,13 +70,11 @@ class SystemDataBackend | ||
70 | end | 70 | end |
71 | 71 | ||
72 | def load!(kind) | 72 | def load!(kind) |
73 | - @data[kind] = Array.new | ||
74 | - class_str = kind.to_s | ||
75 | - class_str[0] = class_str[0..0].capitalize | ||
76 | - | 73 | + @data[kind] = Array.new |
77 | @backend.load(kind) do |data| | 74 | @backend.load(kind) do |data| |
75 | + data[:backend] = @backend | ||
78 | @data[kind].push( | 76 | @data[kind].push( |
79 | - eval(class_str + '.new(data)') | 77 | + eval(kind.to_s + '.new(data)') |
80 | ) | 78 | ) |
81 | end | 79 | end |
82 | end | 80 | end |
@@ -21,20 +21,35 @@ class SystemDataBackendLdap | @@ -21,20 +21,35 @@ class SystemDataBackendLdap | ||
21 | LDAP_SITE_MAP = {:o => :name} | 21 | LDAP_SITE_MAP = {:o => :name} |
22 | 22 | ||
23 | LDAP_MAP = { | 23 | LDAP_MAP = { |
24 | - :user => LDAP_USER_MAP, | ||
25 | - :group => LDAP_GROUP_MAP, | ||
26 | - :site => LDAP_SITE_MAP | 24 | + :User => LDAP_USER_MAP, |
25 | + :Group => LDAP_GROUP_MAP, | ||
26 | + :Site => LDAP_SITE_MAP | ||
27 | } | 27 | } |
28 | 28 | ||
29 | LDAP_FILTER = { | 29 | LDAP_FILTER = { |
30 | - :user => Net::LDAP::Filter::eq('objectClass', 'posixAccount'), | ||
31 | - :group => Net::LDAP::Filter::eq('objectClass', 'posixGroup'), | ||
32 | - :site => Net::LDAP::Filter::eq('objectClass', 'organization') & | 30 | + :User => Net::LDAP::Filter::eq('objectClass', 'posixAccount'), |
31 | + :Group => Net::LDAP::Filter::eq('objectClass', 'posixGroup'), | ||
32 | + :Site => Net::LDAP::Filter::eq('objectClass', 'organization') & | ||
33 | (~Net::LDAP::Filter::eq('o', 'hosting')), | 33 | (~Net::LDAP::Filter::eq('o', 'hosting')), |
34 | - :mailAlias => Net::LDAP::Filter::eq('objectClass', 'mailAlias'), | ||
35 | - :mailAccount => Net::LDAP::Filter::eq('objectClass', 'mailAccount') | 34 | + :MailAlias => Net::LDAP::Filter::eq('objectClass', 'mailAlias'), |
35 | + :MailAccount => Net::LDAP::Filter::eq('objectClass', 'mailAccount') | ||
36 | } | 36 | } |
37 | 37 | ||
38 | + LDAP_OBJECTCLASS = { | ||
39 | + :User => [ 'account', 'posixAccount', 'shadowAccount' ], | ||
40 | + :Group => 'posixGroup' | ||
41 | + } | ||
42 | + | ||
43 | + LDAP_LAMBDA_USER = lambda do |entry| | ||
44 | + entry[:cn] = entry[:uid] | ||
45 | + entry[:shadowlastchange] = (Time::now.to_i/60/60/24).to_s | ||
46 | + entry[:shadowmax] = '99999' | ||
47 | + entry[:shadowwarning] = '7' | ||
48 | + end | ||
49 | + | ||
50 | + LDAP_LAMBDA = { | ||
51 | + :User => LDAP_LAMBDA_USER | ||
52 | + } | ||
38 | 53 | ||
39 | def initialize(host, port, baseDn, args={}) | 54 | def initialize(host, port, baseDn, args={}) |
40 | @baseDn = baseDn | 55 | @baseDn = baseDn |
@@ -62,8 +77,7 @@ class SystemDataBackendLdap | @@ -62,8 +77,7 @@ class SystemDataBackendLdap | ||
62 | map = { :dn => :id } | 77 | map = { :dn => :id } |
63 | map.merge!(LDAP_MAP[kind]) if LDAP_MAP[kind] | 78 | map.merge!(LDAP_MAP[kind]) if LDAP_MAP[kind] |
64 | 79 | ||
65 | - ydata = { :backend => self } | ||
66 | - | 80 | + ydata = {} |
67 | data.each do |key,value| | 81 | data.each do |key,value| |
68 | ydata.merge!({ map[key] || key => value.size==1?value[0]:value.to_a }) | 82 | ydata.merge!({ map[key] || key => value.size==1?value[0]:value.to_a }) |
69 | end | 83 | end |
@@ -72,12 +86,32 @@ class SystemDataBackendLdap | @@ -72,12 +86,32 @@ class SystemDataBackendLdap | ||
72 | end | 86 | end |
73 | end | 87 | end |
74 | 88 | ||
89 | + def update(kind, data) | ||
90 | + map = {} | ||
91 | + map.merge!(LDAP_MAP[kind].invert) if LDAP_MAP[kind] | ||
92 | + | ||
93 | + entry = Net::LDAP::Entry.new(data[:id]) | ||
94 | + data.delete(:id) | ||
95 | + | ||
96 | + entry[:changetype] = 'add' | ||
97 | + entry[:objectclass] = LDAP_OBJECTCLASS[kind] | ||
98 | + | ||
99 | + data.each do |key,value| | ||
100 | + key = map[key] if map[key] | ||
101 | + entry[key] = value | ||
102 | + end | ||
103 | + | ||
104 | + LDAP_LAMBDA[kind].call(entry) if LDAP_LAMBDA[kind] | ||
105 | + | ||
106 | + puts entry.to_ldif | ||
107 | + end | ||
108 | + | ||
75 | private | 109 | private |
76 | 110 | ||
77 | def ldapBase(kind) | 111 | def ldapBase(kind) |
78 | case(kind) | 112 | case(kind) |
79 | - when :user, :group: @systemDn | ||
80 | - when :site, :mailAlias, :mailAccount: @hostingDn | 113 | + when :User, :Group: @systemDn |
114 | + when :Site, :MailAlias, :MailAccount: @hostingDn | ||
81 | end | 115 | end |
82 | end | 116 | end |
83 | 117 |
@@ -44,3 +44,10 @@ backend.mailAccounts.each do |mailAccount| | @@ -44,3 +44,10 @@ backend.mailAccounts.each do |mailAccount| | ||
44 | puts 'mailAccount: '+mailAccount.mail.inspect+','+mailAccount.mailbox.inspect | 44 | puts 'mailAccount: '+mailAccount.mail.inspect+','+mailAccount.mailbox.inspect |
45 | end | 45 | end |
46 | 46 | ||
47 | +puts | ||
48 | + | ||
49 | +backend.userByName('georg').save | ||
50 | + | ||
51 | +puts | ||
52 | + | ||
53 | +backend.groupByName('wheel').save |
Please
register
or
login
to post a comment