From dfce431ca5c07aa53ce55eb0cdb5ecaff2cfc58a Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Sat, 23 Feb 2013 12:26:01 -0500 Subject: pickup groups added to dialplan --- misc/freeswitch/scripts/common/group.lua | 99 +++++++++++++++++++++++++++ misc/freeswitch/scripts/dialplan/dialplan.lua | 35 ++++++---- 2 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 misc/freeswitch/scripts/common/group.lua (limited to 'misc') 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 diff --git a/misc/freeswitch/scripts/dialplan/dialplan.lua b/misc/freeswitch/scripts/dialplan/dialplan.lua index bae5264..93a952d 100644 --- a/misc/freeswitch/scripts/dialplan/dialplan.lua +++ b/misc/freeswitch/scripts/dialplan/dialplan.lua @@ -148,6 +148,9 @@ function Dialplan.object_find(self, class, identifier, auth_name) require 'common.str' class = common.str.downcase(class); + require 'common.group'; + local group_class = common.group.Group:new{ log = self.log, database = self.database }; + if class == 'user' then require 'dialplan.user' local user = nil; @@ -158,7 +161,8 @@ function Dialplan.object_find(self, class, identifier, auth_name) end if user then - user.groups = user:list_groups(); + user.user_groups = user:list_groups(); + user.groups = group_class:name_id_by_member(user.id, user.class); end return user; @@ -171,6 +175,10 @@ function Dialplan.object_find(self, class, identifier, auth_name) tenant = dialplan.tenant.Tenant:new{ log = self.log, database = self.database }:find_by_uuid(identifier); end + if tenant then + tenant.groups = group_class:name_id_by_member(tenant.id, tenant.class); + end + return tenant; elseif class == 'sipaccount' then require 'common.sip_account' @@ -184,6 +192,7 @@ function Dialplan.object_find(self, class, identifier, auth_name) end if sip_account then sip_account.owner = self:object_find(sip_account.record.sip_accountable_type, tonumber(sip_account.record.sip_accountable_id)); + sip_account.groups = group_class:name_id_by_member(sip_account.id, sip_account.class); end return sip_account; elseif class == 'huntgroup' then @@ -198,6 +207,7 @@ function Dialplan.object_find(self, class, identifier, auth_name) if hunt_group then hunt_group.owner = self:object_find('tenant', tonumber(hunt_group.record.tenant_id)); + hunt_group.groups = group_class:name_id_by_member(hunt_group.id, hunt_group.class); end return hunt_group; @@ -213,6 +223,7 @@ function Dialplan.object_find(self, class, identifier, auth_name) if acd then acd.owner = self:object_find(acd.record.automatic_call_distributorable_type, tonumber(acd.record.automatic_call_distributorable_id)); + acd.groups = group_class:name_id_by_member(acd.id, acd.class); end return acd; @@ -226,6 +237,7 @@ function Dialplan.object_find(self, class, identifier, auth_name) end if fax_account then fax_account.owner = self:object_find(fax_account.record.fax_accountable_type, tonumber(fax_account.record.fax_accountable_id)); + fax_account.groups = group_class:name_id_by_member(fax_account.id, fax_account.class); end return fax_account; @@ -235,7 +247,6 @@ end function Dialplan.retrieve_caller_data(self) require 'common.str' - self.caller.caller_phone_numbers_hash = {} -- TODO: Set auth_account on transfer initiated by calling party @@ -252,9 +263,9 @@ function Dialplan.retrieve_caller_data(self) end if self.caller.auth_account then - self.log:info('CALLER_DATA - auth account: ', self.caller.auth_account.class, '=', self.caller.auth_account.id, '/', self.caller.auth_account.uuid); + self.log:info('CALLER_DATA - auth account: ', self.caller.auth_account.class, '=', self.caller.auth_account.id, '/', self.caller.auth_account.uuid, ', groups: ', table.concat(self.caller.auth_account.groups, ',')); if self.caller.auth_account.owner then - self.log:info('CALLER_DATA - auth owner: ', self.caller.auth_account.owner.class, '=', self.caller.auth_account.owner.id, '/', self.caller.auth_account.owner.uuid); + self.log:info('CALLER_DATA - auth owner: ', self.caller.auth_account.owner.class, '=', self.caller.auth_account.owner.id, '/', self.caller.auth_account.owner.uuid, ', groups: ', table.concat(self.caller.auth_account.owner.groups, ',')); else self.log:error('CALLER_DATA - auth owner not found'); end @@ -273,9 +284,9 @@ function Dialplan.retrieve_caller_data(self) if not common.str.blank(self.caller.account.record.language_code) then self.caller.language = self.caller.account.record.language_code; end - self.log:info('CALLER_DATA - caller account: ', self.caller.account.class, '=', self.caller.account.id, '/', self.caller.account.uuid, ', phone_numbers: ', #self.caller.caller_phone_numbers, ', language: ', self.caller.language); + self.log:info('CALLER_DATA - caller account: ', self.caller.account.class, '=', self.caller.account.id, '/', self.caller.account.uuid, ', phone_numbers: ', #self.caller.caller_phone_numbers, ', language: ', self.caller.language, ', groups: ', table.concat(self.caller.account.groups, ',')); if self.caller.account.owner then - self.log:info('CALLER_DATA - caller owner: ', self.caller.account.owner.class, '=', self.caller.account.owner.id, '/', self.caller.account.owner.uuid); + self.log:info('CALLER_DATA - caller owner: ', self.caller.account.owner.class, '=', self.caller.account.owner.id, '/', self.caller.account.owner.uuid, ', groups: ', table.concat(self.caller.account.owner.groups, ',')); else self.log:error('CALLER_DATA - caller owner not found'); end @@ -378,6 +389,12 @@ function Dialplan.dial(self, destination) self.caller:set_callee_id(destination.number, destination.account.record.caller_name); table.insert(destination.pickup_groups, 's' .. destination.account.id ); end + require 'common.group'; + local group_names, group_ids = common.group.Group:new{ log = self.log, database = self.database }:name_id_by_member_permissions(destination.id, destination.type, {'pickup'}, true) + self.log:debug('DESTINATION_GROUPS - pickup_groups: ', table.concat(group_names, ',')); + for index=1, #group_ids do + table.insert(destination.pickup_groups, 'g' .. group_ids[index]); + end end if destination.account and destination.account.owner then @@ -385,12 +402,6 @@ function Dialplan.dial(self, destination) user_id = destination.account.owner.id; tenant_id = tonumber(destination.account.owner.record.current_tenant_id); local user = self:object_find(destination.account.owner.class, tonumber(user_id)); - if user then - group_ids = user:list_group_ids(); - for index=1, #group_ids do - table.insert(destination.pickup_groups, 'g' .. group_ids[index]); - end - end elseif destination.account.owner.class == 'tenant' then tenant_id = destination.account.owner.id; end -- cgit v1.2.3