SystemDataBackend.rb
1.4 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 'User'
require 'Group'
require 'Site'
require 'MailAlias'
require 'MailAccount'
class SystemDataBackend
def initialize(backend)
@backend = backend
@data = Hash.new
end
def users
load(:user)
end
def groups
load(:group)
end
def sites
load(:site)
end
def mailAliases
load(:mailAlias)
end
def mailAccounts
load(:mailAccount)
end
def userByName(name)
users.find{|user| user.name == name}
end
def groupByName(name)
groups.find{|group| group.name == name}
end
def siteByName(name)
sites.find{|site| site.name == name}
end
def usersInGroup(group)
case
when group.members.instance_of?(Array):
Hash[group.members.map{|uid| [uid, userByName(uid)] if userByName(uid)}]
when goup.members.nil?:
{}
else
{group.members => userByName(group.members)} if userByName(group.members)
end
end
def mailAliasesBySite(site)
mailAliases.find_all{|mail| mail.site == site.name}
end
def mailAccountsBySite(site)
mailAccounts.find_all{|mail| mail.site == site.name}
end
private
def load(kind)
load!(kind) if ! @data[kind]
@data[kind]
end
def load!(kind)
@data[kind] = Array.new
class_str = kind.to_s
class_str[0] = class_str[0..0].capitalize
@backend.load(kind) do |data|
@data[kind].push(
eval(class_str + '.new(data)')
)
end
end
end