summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/group.lua
diff options
context:
space:
mode:
Diffstat (limited to 'misc/freeswitch/scripts/common/group.lua')
-rw-r--r--misc/freeswitch/scripts/common/group.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/group.lua b/misc/freeswitch/scripts/common/group.lua
index c4125bc..b9cae61 100644
--- a/misc/freeswitch/scripts/common/group.lua
+++ b/misc/freeswitch/scripts/common/group.lua
@@ -86,3 +86,87 @@ function Group.name_id_by_member(self, member_id, member_type)
return group_names, group_ids;
end
+
+
+function Group.permission_targets(self, group_ids, permission)
+ if not group_ids or #group_ids == 0 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 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.is_target(self, group_id, permission)
+ if not group_id or not permission then
+ return nil;
+ end
+
+ local sql_query = 'SELECT `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` = ' .. tonumber(group_id) .. ' \
+ AND `b`.`active` IS TRUE \
+ LIMIT 1';
+
+ return self.database:query_return_value(sql_query);
+end
+
+
+function Group.union(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
+
+
+function Group.intersection(self, set_one, set_two)
+ if not set_one or not set_two then
+ return {};
+ end
+
+ local basic_set = {};
+ for index=1, #set_one do
+ basic_set[set_one[index]] = true;
+ end
+
+ local final_set = {};
+ for index=1, #set_two do
+ if basic_set[set_two[index]] then
+ table.insert(final_set, set_two[index]);
+ end
+ end
+
+ return final_set;
+end