summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common
diff options
context:
space:
mode:
authorPeter Kozak <spag@golwen.net>2013-02-23 12:26:01 -0500
committerPeter Kozak <spag@golwen.net>2013-02-23 12:26:01 -0500
commitdfce431ca5c07aa53ce55eb0cdb5ecaff2cfc58a (patch)
treed4e41dab5f6672bbbd69955010287d7632ccfb5e /misc/freeswitch/scripts/common
parent3404cb248f32951aaf0508cfcfc87bfad533c6e5 (diff)
pickup groups added to dialplan
Diffstat (limited to 'misc/freeswitch/scripts/common')
-rw-r--r--misc/freeswitch/scripts/common/group.lua99
1 files changed, 99 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/group.lua b/misc/freeswitch/scripts/common/group.lua
new file mode 100644
index 0000000..b89585a
--- /dev/null
+++ b/misc/freeswitch/scripts/common/group.lua
@@ -0,0 +1,99 @@
+-- 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_member_permissions(self, member_id, member_type, permissions, targets)
+ if not tonumber(member_id) then
+ return {};
+ end
+
+ local sql_query = nil;
+
+ if targets then
+ 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` IN ("' .. table.concat(permissions, ',') .. '") \
+ GROUP BY `b`.`group_id` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
+ else
+ sql_query = 'SELECT DISTINCT `c`.`id`, `c`.`name` \
+ FROM `group_permissions` `a` \
+ JOIN `group_memberships` `b` ON `a`.`group_id` = `b`.`group_id` \
+ JOIN `groups` `c` ON `c`.`id` = `a`.`group_id` \
+ WHERE `b`.`item_type` = ' .. self.database:escape(member_type, '"') .. ' \
+ AND `b`.`item_id` = ' .. member_id .. ' \
+ AND `a`.`permission` IN ("' .. table.concat(permissions, ',') .. '") \
+ GROUP BY `b`.`group_id` LIMIT ' .. MAX_GROUP_MEMBERSHIPS;
+ end
+
+ 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 .. ' \
+ 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