summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspag <spag@golwen.net>2012-12-23 16:04:05 +0100
committerspag <spag@golwen.net>2012-12-23 16:04:05 +0100
commit55a0d5ce38f99226174f358279c02178eacd75d7 (patch)
tree8ad5fe87d51760bf8fa76bd60337c25ff16726f5
parentf425280a6312638f364f6ae8d915a30d78a8dce4 (diff)
login/logout methods
-rw-r--r--app/controllers/config_siemens_controller.rb18
-rw-r--r--app/controllers/config_snom_controller.rb19
-rw-r--r--app/controllers/phones_controller.rb5
-rw-r--r--app/models/phone.rb27
-rw-r--r--misc/freeswitch/scripts/dialplan/functions.lua2
-rw-r--r--misc/freeswitch/scripts/phones/phone.lua58
6 files changed, 75 insertions, 54 deletions
diff --git a/app/controllers/config_siemens_controller.rb b/app/controllers/config_siemens_controller.rb
index f398b1a..c09dfcf 100644
--- a/app/controllers/config_siemens_controller.rb
+++ b/app/controllers/config_siemens_controller.rb
@@ -80,6 +80,7 @@ class ConfigSiemensController < ApplicationController
@phone.mac_address = mac_address
@phone.hot_deskable = true
@phone.phone_model = PhoneModel.where('name LIKE ?', "#{phone_type}").first
+ @phone.tenant = tenant
if ! @phone.save
render(
:status => 500,
@@ -128,16 +129,13 @@ class ConfigSiemensController < ApplicationController
return
end
- phone_sip_account = PhoneSipAccount.new()
- phone_sip_account.phone_id = @phone.id
- phone_sip_account.sip_account_id = @sip_account.id
-
- if ! phone_sip_account.save
+ @phone.fallback_sip_account = @sip_account
+ if ! @phone.save
render(
:status => 500,
:layout => false,
:content_type => 'text/plain',
- :text => "<!-- #{phone_sip_account.errors.messages.inspect} -->",
+ :text => "<!-- #{@phone.errors.messages.inspect} -->",
)
return
end
@@ -152,13 +150,17 @@ class ConfigSiemensController < ApplicationController
@sip_account = @phone.sip_accounts.where(:sip_accountable_type => @phone.phoneable_type,
:sip_accountable_id => @phone.phoneable_id).first
+ if !@sip_account
+ @sip_account = @phone.fallback_sip_account
+ end
+
+ tenant = @phone.tenant
+
if @phone.phoneable
if @phone.phoneable_type == 'Tenant'
- tenant = @phone.phoneable
language = tenant.language.code
elsif @phone.phoneable_type == 'User'
language = @phone.phoneable.language.code
- tenant = @phone.phoneable.current_tenant
end
end
diff --git a/app/controllers/config_snom_controller.rb b/app/controllers/config_snom_controller.rb
index 74a117d..4d12082 100644
--- a/app/controllers/config_snom_controller.rb
+++ b/app/controllers/config_snom_controller.rb
@@ -49,6 +49,7 @@ class ConfigSnomController < ApplicationController
@phone = tenant.phones.build
@phone.mac_address = @mac_address
@phone.hot_deskable = true
+ @phone.tenant = tenant
mac_address_to_model = {
'00041325' => 'Snom 300',
@@ -130,20 +131,16 @@ class ConfigSnomController < ApplicationController
return
end
- phone_sip_account = PhoneSipAccount.new()
- phone_sip_account.phone_id = @phone.id
- phone_sip_account.sip_account_id = @sip_account.id
-
- if ! phone_sip_account.save
+ @phone.fallback_sip_account = @sip_account
+ if ! @phone.save
render(
:status => 500,
:layout => false,
:content_type => 'text/plain',
- :text => "<!-- #{phone_sip_account.errors.messages.inspect} -->",
+ :text => "<!-- #{@phone.errors.messages.inspect} -->",
)
return
end
-
end
elsif ! params[:phone].blank? then
@phone = Phone.where({ :id => params[:phone].to_i }).first
@@ -238,9 +235,15 @@ class ConfigSnomController < ApplicationController
@softkeys = Array.new()
@sip_accounts = Array.new()
+ phone_sip_accounts = Array.new()
if send_sensitve
- @phone.sip_accounts.each do |sip_account|
+ if @phone.sip_accounts && @phone.sip_accounts.count > 0
+ phone_sip_accounts = @phone.sip_accounts
+ elsif @phone.fallback_sip_account
+ phone_sip_accounts.push( @phone.fallback_sip_account )
+ end
+ phone_sip_accounts.each do |sip_account|
if (sip_account.sip_accountable_type == @phone.phoneable_type) and (sip_account.sip_accountable_id == @phone.phoneable_id)
snom_sip_account = {
:id => sip_account.id,
diff --git a/app/controllers/phones_controller.rb b/app/controllers/phones_controller.rb
index d46bf86..0b765e8 100644
--- a/app/controllers/phones_controller.rb
+++ b/app/controllers/phones_controller.rb
@@ -14,7 +14,6 @@ class PhonesController < ApplicationController
def new
@phone = @phoneable.phones.build()
-
# Use the last phone.phone_model as the default.
#
@phone.phone_model_id = Phone.last.try(:phone_model).try(:id)
@@ -22,6 +21,10 @@ class PhonesController < ApplicationController
def create
@phone = @phoneable.phones.build(params[:phone])
+ if !@tenant
+ @tenant = @user.current_tenant
+ end
+ @phone.tenant = @tenant
if @phone.save
m = method( :"#{@phoneable.class.name.underscore}_phone_path" )
redirect_to m.( @phoneable, @phone ), :notice => t('phones.controller.successfuly_created')
diff --git a/app/models/phone.rb b/app/models/phone.rb
index 89371eb..bd6e3f6 100644
--- a/app/models/phone.rb
+++ b/app/models/phone.rb
@@ -14,6 +14,9 @@ class Phone < ActiveRecord::Base
has_many :phone_sip_accounts, :dependent => :destroy, :uniq => true, :order => :position
has_many :sip_accounts, :through => :phone_sip_accounts
+ belongs_to :tenant
+ belongs_to :fallback_sip_account, :class_name => "SipAccount"
+
# Validations
#
before_validation :sanitize_mac_address
@@ -138,6 +141,8 @@ class Phone < ActiveRecord::Base
end
end
+ PhoneSipAccount.where(:phone_id => self.id).destroy_all
+
self.phoneable = user
sip_accounts.each do |sip_account|
if ! self.sip_accounts.where(:id => sip_account.id).first
@@ -163,37 +168,21 @@ class Phone < ActiveRecord::Base
# OPTIMIZE i18n translations
def user_logout
- if ! self.hot_deskable or self.phoneable_type == 'Tenant'
+ if ! self.hot_deskable
errors.add(:hot_deskable, "Phone not hot-deskable")
return false
end
sip_account = self.sip_accounts.where(:sip_accountable_type => self.phoneable_type).first
- tenant_sip_account = self.sip_accounts.where(:sip_accountable_type => 'Tenant').first
- if tenant_sip_account
- tenant = tenant_sip_account.sip_accountable
- end
-
- sip_account_ids = Array.new()
- self.sip_accounts.where(:sip_accountable_type => 'User', :hotdeskable => true).each do |sip_account|
- sip_account_ids.push(sip_account.id)
- end
-
- if tenant
- self.phoneable = tenant
- @not_destroy_phones_sip_accounts = true
+ if self.tenant
+ self.phoneable = self.tenant
if ! self.save
errors.add(:phoneable, "Could not change owner")
return false
end
end
- if ! PhoneSipAccount.destroy_all(:sip_account_id => sip_account_ids)
- errors.add(:sip_accounts, "Could not delete sip_accounts")
- return false
- end
-
sleep(0.5)
if ! self.resync(true, sip_account)
diff --git a/misc/freeswitch/scripts/dialplan/functions.lua b/misc/freeswitch/scripts/dialplan/functions.lua
index c104f89..9a89857 100644
--- a/misc/freeswitch/scripts/dialplan/functions.lua
+++ b/misc/freeswitch/scripts/dialplan/functions.lua
@@ -31,7 +31,7 @@ function Functions.dialplan_function(self, caller, dialed_number)
local fid = tostring(parameters[2]);
local result = { continue = false, code = 404, phrase = 'Function not found', no_cdr = true };
- self.log:debug('DIALPLAN_DUNCTION - execute: ', dialed_number);
+ self.log:debug('DIALPLAN_FUNCTION - execute: ', dialed_number);
if fid == "ta" then
result = self:transfer_all(caller, parameters[3]);
diff --git a/misc/freeswitch/scripts/phones/phone.lua b/misc/freeswitch/scripts/phones/phone.lua
index 5cd210b..4a32c37 100644
--- a/misc/freeswitch/scripts/phones/phone.lua
+++ b/misc/freeswitch/scripts/phones/phone.lua
@@ -18,6 +18,26 @@ function Phone.new(self, arg)
end
+function Phone.list_by_sql(self, sql_query)
+ local account_phones = {};
+
+ self.database:query(sql_query, function(account_entry)
+ local phone = Phone:new(self, {object = parent_class});
+ phone.record = account_entry;
+ phone.record.ieee_name = common.str.downcase(account_entry.ieee_name);
+
+ if phone.record.ieee_name == 'snom technology ag' then
+ require 'phones.snom'
+ phone.model = phones.snom.Snom:new();
+ elseif account_entry.ieee_name == 'siemens enterprise communicationsgmbh & co. kg' then
+ require 'phones.siemens'
+ phone.model = phones.siemens.Siemens:new();
+ end
+ table.insert(account_phones, phone);
+ end)
+
+ return account_phones;
+end
-- Find a hot-deskable phone by sip-account
function Phone.find_all_hot_deskable_by_account(self, account_id)
@@ -25,7 +45,7 @@ function Phone.find_all_hot_deskable_by_account(self, account_id)
local sql_query = 'SELECT \
`b`.`id`, `b`.`mac_address`, `b`.`ip_address`, `b`.`http_user`, `b`.`http_password`, `b`.`phoneable_type`, `b`.`phoneable_id`, \
- `d`.`ieee_name` \
+ `b`.`tenant_id`, `b`.`fallback_sip_account_id`, `d`.`ieee_name` \
FROM `phone_sip_accounts` `a` \
JOIN `phones` `b` ON `a`.`phone_id` = `b`.`id` \
JOIN `phone_models` `c` ON `b`.`phone_model_id` = `c`.`id` \
@@ -35,22 +55,21 @@ function Phone.find_all_hot_deskable_by_account(self, account_id)
AND `d`.`state` = "active" \
AND `a`.`sip_account_id` = ' .. tonumber(account_id);
- local account_phones = {};
+ local account_phones = self:list_by_sql(sql_query);
- self.database:query(sql_query, function(account_entry)
- local phone = Phone:new(self, {object = parent_class});
- phone.record = account_entry;
- phone.record.ieee_name = common.str.downcase(account_entry.ieee_name);
+ if #account_phones == 0 then
+ sql_query = 'SELECT `b`.`id`, `b`.`mac_address`, `b`.`ip_address`, `b`.`http_user`, `b`.`http_password`, `b`.`phoneable_type`, `b`.`phoneable_id`, \
+ `b`.`tenant_id`, `b`.`fallback_sip_account_id`, `d`.`ieee_name` \
+ FROM `phones` `b` \
+ JOIN `phone_models` `c` ON `b`.`phone_model_id` = `c`.`id` \
+ JOIN `manufacturers` `d` ON `c`.`manufacturer_id` = `d`.`id` \
+ WHERE `b`.`hot_deskable` IS TRUE \
+ AND `c`.`state` = "active" \
+ AND `d`.`state` = "active" \
+ AND `b`.`fallback_sip_account_id` = ' .. tonumber(account_id);
- if phone.record.ieee_name == 'snom technology ag' then
- require 'phones.snom'
- phone.model = phones.snom.Snom:new();
- elseif account_entry.ieee_name == 'siemens enterprise communicationsgmbh & co. kg' then
- require 'phones.siemens'
- phone.model = phones.siemens.Siemens:new();
- end
- table.insert(account_phones, phone);
- end)
+ account_phones = self:list_by_sql(sql_query);
+ end
return account_phones;
end
@@ -82,7 +101,7 @@ function Phone.phoneable_set(self, phoneable_id, phoneable_type)
end
function Phone.logout(self, account_id)
- local tenant_id = self:tenant_id_get();
+ local tenant_id = tonumber(self.record.tenant_id);
if not tenant_id then
self.log:info('PHONE_LOGOUT - tenant not found');
@@ -91,7 +110,12 @@ function Phone.logout(self, account_id)
self:phoneable_set(tenant_id, 'Tenant');
- sql_query = 'DELETE FROM `phone_sip_accounts` WHERE `sip_account_id` = ' .. tonumber(account_id);
+ if account_id then
+ sql_query = 'DELETE FROM `phone_sip_accounts` WHERE `sip_account_id` = ' .. tonumber(account_id);
+ self.database:query(sql_query);
+ end
+
+ sql_query = 'DELETE FROM `phone_sip_accounts` WHERE `phone_id` = ' .. self.record.id;
return self.database:query(sql_query);
end