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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
-- Gemeinschaft 5 module: group class
-- (c) AMOOMA GmbH 2013
--
module(...,package.seeall)
Group = {}
MAX_GROUP_MEMBERSHIPS = 256;
-- create group object
function Group.new(self, arg)
arg = arg or {}
object = arg.object or {}
setmetatable(object, self);
self.__index = self;
self.class = 'group';
self.log = arg.log;
self.database = arg.database;
return object;
end
-- find group by id
function Group.find_by_id(self, id)
local sql_query = 'SELECT * FROM `groups` WHERE `id`= ' .. tonumber(id) .. ' LIMIT 1';
local group = nil;
self.database:query(sql_query, function(account_entry)
group = Group:new(self);
group.record = account_entry;
group.id = tonumber(account_entry.id);
group.name = account_entry.name;
end);
return group;
end
-- list groups by member permissions
function Group.name_id_by_permission(self, member_id, member_type, permission)
if not tonumber(member_id) then
return {};
end
local sql_query = 'SELECT DISTINCT `c`.`id`, `c`.`name` \
FROM `group_permissions` `a` \
JOIN `group_memberships` `b` ON `a`.`target_group_id` = `b`.`group_id` \
JOIN `groups` `c` ON `c`.`id` = `b`.`group_id` \
WHERE `b`.`item_type` = ' .. self.database:escape(member_type, '"') .. ' \
AND `b`.`item_id` = ' .. member_id .. ' \
AND `a`.`permission` = ' .. self.database:escape(permission, '"') .. ' \
AND `c`.`active` IS TRUE \
GROUP BY `b`.`group_id` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
local group_names = {};
local group_ids = {};
self.database:query(sql_query, function(account_entry)
table.insert(group_names, account_entry.name);
table.insert(group_ids, tonumber(account_entry.id));
end);
return group_names, group_ids;
end
-- list groups by member
function Group.name_id_by_member(self, member_id, member_type)
if not tonumber(member_id) then
return {};
end
local sql_query = 'SELECT DISTINCT `c`.`id`, `c`.`name` \
FROM `group_memberships` `b` \
JOIN `groups` `c` ON `c`.`id` = `b`.`group_id` \
WHERE `b`.`item_type` = ' .. self.database:escape(member_type, '"') .. ' \
AND `b`.`item_id` = ' .. member_id .. ' \
AND `c`.`active` IS TRUE \
GROUP BY `b`.`group_id` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
local group_names = {};
local group_ids = {};
self.database:query(sql_query, function(account_entry)
table.insert(group_names, account_entry.name);
table.insert(group_ids, tonumber(account_entry.id));
end);
return group_names, group_ids;
end
function Group.permission_targets(self, group_ids, permission)
if not group_ids or not permission then
return {};
end
local sql_query = 'SELECT DISTINCT `b`.`id`, `b`.`name` \
FROM `group_permissions` `a` \
JOIN `groups` `b` ON `b`.`id` = `a`.`target_group_id` \
WHERE `a`.`permission` = ' .. self.database:escape(permission, '"') .. ' \
AND `a`.`group_id` IN (' .. table.concat(group_ids, ',') .. ') \
AND `b`.`active` IS TRUE \
GROUP BY `a`.`target_group_id` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
local groups = {};
self.database:query(sql_query, function(account_entry)
groups[account_entry.id] = account_entry.name;
end);
return groups;
end
function Group.combine(self, ...)
local groups = {};
local group_sets = {...};
for set_index=1, #group_sets do
if type(group_sets[set_index]) == 'table' then
local group_ids = group_sets[set_index];
for index=1, #group_ids do
groups[tonumber(group_ids[index])] = true;
end
end
end
local group_ids = {};
for group_id, status in pairs(groups) do
table.insert(group_ids, group_id);
end
return group_ids;
end
|