diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ability.rb | 16 | ||||
-rw-r--r-- | app/models/backup_job.rb | 9 | ||||
-rw-r--r-- | app/models/call.rb | 78 | ||||
-rw-r--r-- | app/models/call_forward.rb | 2 | ||||
-rw-r--r-- | app/models/fax_document.rb | 11 | ||||
-rw-r--r-- | app/models/group.rb | 21 | ||||
-rw-r--r-- | app/models/group_permission.rb | 2 | ||||
-rw-r--r-- | app/models/intruder.rb | 86 | ||||
-rw-r--r-- | app/models/route_element.rb | 2 | ||||
-rw-r--r-- | app/models/sip_account.rb | 25 | ||||
-rw-r--r-- | app/models/softkey.rb | 63 | ||||
-rw-r--r-- | app/models/tenant.rb | 4 |
12 files changed, 206 insertions, 113 deletions
diff --git a/app/models/ability.rb b/app/models/ability.rb index 48cce84..3cd1d4d 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -134,9 +134,6 @@ class Ability can :read, SipAccount, :sip_accountable_type => 'User', :sip_accountable_id => user.id user.sip_accounts.each do |sip_account| can :read, PhoneNumber, :id => sip_account.phone_number_ids - can :manage, CallForward, :call_forwardable_id => sip_account.phone_number_ids - can :manage, Ringtone, :ringtoneable_type => 'PhoneNumber', :ringtoneable_id => sip_account.phone_number_ids - can :manage, Ringtone, :ringtoneable_type => 'SipAccount', :ringtoneable_id => sip_account.id can [:read, :destroy, :call] , CallHistory, :id => sip_account.call_history_ids end can :read, Phone, :phoneable_type => 'User', :phoneable_id => user.id @@ -161,10 +158,17 @@ class Ability can :manage, ConferenceInvitee, :conference_id => conference.id end - # User can manage CallForwards of the PhoneNumbers of his - # own SipAccounts: + # User can manage CallForwards of its SipAccount and PhoneNumbers # - can :manage, CallForward, :call_forwardable_id => user.phone_number_ids + can :manage, CallForward, :call_forwardable_type => 'PhoneNumber', :call_forwardable_id => user.phone_number_ids + can :manage, CallForward, :call_forwardable_type => 'SipAccount', :call_forwardable_id => user.sip_account_ids + can :create, CallForward + + # User can manage Ringtones of its SipAccount and PhoneNumbers + # + can :manage, Ringtone, :ringtoneable_type => 'PhoneNumber', :ringtoneable_id => user.phone_number_ids + can :manage, Ringtone, :ringtoneable_type => 'SipAccount', :ringtoneable_id => user.sip_account_ids + can :create, Ringtone # SoftkeyFunctions # diff --git a/app/models/backup_job.rb b/app/models/backup_job.rb index a04f6c0..48dd27e 100644 --- a/app/models/backup_job.rb +++ b/app/models/backup_job.rb @@ -12,12 +12,17 @@ class BackupJob < ActiveRecord::Base private def set_state_to_queued - self.state = 'queued' + self.state ||= 'queued' self.started_at = Time.now end def initiate_backup - self.delay.make_a_backup + if self.state == 'force now' + self.state = 'queued' + self.make_a_backup + else + self.delay.make_a_backup + end end def make_a_backup diff --git a/app/models/call.rb b/app/models/call.rb index db6d9d6..8f657fa 100644 --- a/app/models/call.rb +++ b/app/models/call.rb @@ -1,30 +1,32 @@ class Call < ActiveRecord::Base - self.table_name = 'detailed_calls' + self.table_name = 'calls_active' self.primary_key = 'uuid' - attr_writer :sip_account_id + belongs_to :sip_account + belongs_to :b_sip_account, :class_name => SipAccount - validates :dest, + validates :sip_account_id, :presence => true - - def create(attributes=nil) - if ! attributes - return - end - self.sip_account = SipAccount.where(:id => attributes[:sip_account_id]).first - self.dest = attributes[:dest] - return self + validates :destination, + :presence => true + + def save(attributes=nil) end - def save(attributes=nil) - - end + def call + if self.sip_account && self.destination + return self.sip_account.call(self.destination) + end - def call(number=nil) - if @sip_account && self.dest - return @sip_account.call(self.dest) + if !self.sip_account + errors.add(:sip_account_id, 'no sip_account') end + + if self.destination.blank? + errors.add(:destination, 'no destination') + end + return false end @@ -37,38 +39,6 @@ class Call < ActiveRecord::Base return FreeswitchAPI.execute('uuid_kill', self.uuid, true); end - def sip_account=(sip_a) - @sip_account = sip_a - end - - def sip_account - if @sip_account - return @sip_account - end - - result = self.presence_id.match('^(.+)@(.+)$') - - if result && ! result[1].blank? and ! result[2].blank? - domain = SipDomain.where(:host => result[2]).first - if domain - @sip_account = SipAccount.where(:auth_name => result[1], :sip_domain_id => domain.id).first - end - end - - return @sip_account - end - - def sip_account_bleg - result = self.b_presence_id.match('^(.+)@(.+)$') - - if result && ! result[1].blank? and ! result[2].blank? - domain = SipDomain.where(:host => result[2]).first - if domain - return SipAccount.where(:auth_name => result[1], :sip_domain_id => domain.id).first - end - end - end - def get_variable_from_uuid(channel_uuid, variable_name) if channel_uuid.blank? return nil @@ -92,14 +62,4 @@ class Call < ActiveRecord::Base return get_variable_from_uuid(self.b_uuid, variable_name); end - def is_sip - return self.name.match('^sofia') != nil - end - - def is_caller - if (self.uuid == self.call_uuid) || self.call_uuid.blank? - true - end - end - end diff --git a/app/models/call_forward.rb b/app/models/call_forward.rb index a932e11..195ac36 100644 --- a/app/models/call_forward.rb +++ b/app/models/call_forward.rb @@ -177,7 +177,7 @@ class CallForward < ActiveRecord::Base end def deactivate_concurring_entries - CallForward.where(:call_forwardable_id => self.call_forwardable_id, :call_forwardable_type => self.call_forwardable_type, :call_forward_case_id => self.call_forward_case_id, :active => true).each do |call_forwarding_entry| + CallForward.where(:call_forwardable_id => self.call_forwardable_id, :call_forwardable_type => self.call_forwardable_type, :call_forward_case_id => self.call_forward_case_id, :source => self.source, :active => true).each do |call_forwarding_entry| if call_forwarding_entry.id != self.id call_forwarding_entry.update_attributes(:active => false) end diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb index 5b27965..3cb92ea 100644 --- a/app/models/fax_document.rb +++ b/app/models/fax_document.rb @@ -96,9 +96,18 @@ class FaxDocument < ActiveRecord::Base page_size_command = "<< /Policies << /PageSize 3 >> /InputAttributes currentpagedevice /InputAttributes get dup { pop 1 index exch undef } forall dup 0 << /PageSize [ #{page_size_a4} ] >> put >> setpagedevice" working_path, file_name = File.split(self.document.to_s) tiff_file = File.basename(file_name.to_s.downcase, File.extname(file_name)) + '.tiff' - result = system "cd #{store_dir} && gs -q -r#{self.fax_resolution.resolution_value} -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=\"#{tiff_file}\" -c \"#{page_size_command}\" -- \"#{self.document.to_s}\"" + result = system "cd #{store_dir} && gs -q -r#{self.fax_resolution.resolution_value} -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=\"#{store_dir}/#{tiff_file}\" -c \"#{page_size_command}\" -- \"#{self.document.to_s}\"" if !File.exists?("#{store_dir}/#{tiff_file}") + page_size = "1728x1078" or "1728x1186"; + command = "cd #{store_dir} && convert -quiet -density #{self.fax_resolution.resolution_value} -units PixelsPerInch -resize #{page_size}\! -monochrome -compress Fax \"#{self.document.to_s}\" \"#{store_dir}/#{tiff_file}\"" + result = system(command) + if result.nil? + logger.error "### FAX_DOCUMENT_TO_TIFF - error: #{$?}, command: #{command}" + end + end + + if !File.exists?("#{store_dir}/#{tiff_file}") return nil end diff --git a/app/models/group.rb b/app/models/group.rb index e0cfaab..6c65f70 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -5,7 +5,28 @@ class Group < ActiveRecord::Base has_many :group_permissions, :dependent => :destroy has_many :permittances, :foreign_key => :target_group_id, :class_name => "GroupPermission", :dependent => :destroy + validates :name, + :presence => true + def to_s self.name end + + def permission_targets(permission) + group_permissions.where(:permission => permission).pluck(:target_group_id) + end + + def has_permission(target_type, target_id, permission) + target_group_ids = GroupMembership.where(:item_id => target_id, :item_type => target_type).pluck(:group_id) + return group_permissions.where(:permission => permission, :target_group_id => target_group_ids).first != nil + end + + def self.union(sets=[]) + group_ids = [] + sets.each do |set| + group_ids = group_ids + set + end + + return group_ids.uniq + end end diff --git a/app/models/group_permission.rb b/app/models/group_permission.rb index fe988ba..c859f52 100644 --- a/app/models/group_permission.rb +++ b/app/models/group_permission.rb @@ -1,7 +1,7 @@ class GroupPermission < ActiveRecord::Base attr_accessible :group_id, :permission, :target_group_id - PERMISSION_TYPES = ['pickup'] + PERMISSION_TYPES = ['pickup', 'presence'] belongs_to :group belongs_to :target_group, :class_name => "Group" diff --git a/app/models/intruder.rb b/app/models/intruder.rb index 97e3773..9a1c39a 100644 --- a/app/models/intruder.rb +++ b/app/models/intruder.rb @@ -17,6 +17,10 @@ class Intruder < ActiveRecord::Base before_validation :set_key_if_empty + after_create :check_if_new_entry_relevant + after_update :check_if_update_relevant + after_destroy :check_if_delete_relevant + def to_s key end @@ -31,26 +35,6 @@ class Intruder < ActiveRecord::Base end end - def self.write_firewall_blacklist - firewall_blacklist_file = GsParameter.get('blacklist_file', 'perimeter', 'general') - entry_template = GsParameter.get('blacklist_file_entry', 'perimeter', 'general') - comment_template = GsParameter.get('blacklist_file_comment', 'perimeter', 'general') - File.open(firewall_blacklist_file, 'w') do |file| - Intruder.where(:list_type => 'blacklist').where('bans > 0').all.each do |entry| - if ! comment_template.blank? - file.write(self.expand_variables(comment_template, entry.to_hash) + "\n") - end - file.write(self.expand_variables(entry_template, entry.to_hash) + "\n") - end - end - end - - def self.expand_variables(line, variables) - return line.gsub(/\{([a-z_]+)\}/) do |m| - variables[$1.to_sym] - end - end - def to_hash return { :key => self.key, @@ -72,4 +56,66 @@ class Intruder < ActiveRecord::Base self.key = self.contact_ip end end + + def expand_variables(line, variables) + return line.gsub(/\{([a-z_]+)\}/) do |m| + variables[$1.to_sym] + end + end + + def write_firewall_list + firewall_blacklist_file = GsParameter.get('blacklist_file', 'perimeter', 'general') + blacklist_entry_template = GsParameter.get('blacklist_file_entry', 'perimeter', 'general') + whitelist_entry_template = GsParameter.get('whitelist_file_entry', 'perimeter', 'general') + comment_template = GsParameter.get('blacklist_file_comment', 'perimeter', 'general') + File.open(firewall_blacklist_file, 'w') do |file| + Intruder.where(:list_type => ['whitelist', 'blacklist']).order('list_type DESC, contact_last ASC').all.each do |entry| + if !whitelist_entry_template.blank? && entry.list_type == 'whitelist' + if ! comment_template.blank? + file.write(expand_variables(comment_template, entry.to_hash) + "\n") + end + file.write(expand_variables(whitelist_entry_template, entry.to_hash) + "\n") + elsif !blacklist_entry_template.blank? && entry.list_type == 'blacklist' && entry.bans.to_i > 0 + if ! comment_template.blank? + file.write(expand_variables(comment_template, entry.to_hash) + "\n") + end + file.write(expand_variables(blacklist_entry_template, entry.to_hash) + "\n") + end + end + end + end + + def restart_firewall + command = GsParameter.get('ban_command', 'perimeter', 'general') + if !command.blank? + system expand_variables(command, self.to_hash) + end + end + + def check_if_update_relevant + if key_changed? || contact_ip_changed? || list_type_changed? || bans_changed? || points_changed? + if !GsParameter.get("#{self.list_type}_file_entry", 'perimeter', 'general').blank? + write_firewall_list + restart_firewall + end + end + end + + def check_if_new_entry_relevant + if !GsParameter.get("#{self.list_type}_file_entry", 'perimeter', 'general').blank? + if self.list_type != 'blacklist' || self.bans.to_i > 0 + write_firewall_list + restart_firewall + end + end + end + + def check_if_delete_relevant + if !GsParameter.get("#{self.list_type}_file_entry", 'perimeter', 'general').blank? + if self.list_type != 'blacklist' || self.bans.to_i > 0 + write_firewall_list + restart_firewall + end + end + end end diff --git a/app/models/route_element.rb b/app/models/route_element.rb index 94f0f84..7d37db0 100644 --- a/app/models/route_element.rb +++ b/app/models/route_element.rb @@ -3,7 +3,7 @@ class RouteElement < ActiveRecord::Base attr_accessible :call_route_id, :var_in, :var_out, :pattern, :replacement, :action, :mandatory, :position - belongs_to :call_route + belongs_to :call_route, :touch => true acts_as_list :scope => :call_route diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index 5660e37..cdb609d 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -38,7 +38,8 @@ class SipAccount < ActiveRecord::Base has_many :ringtones, :as => :ringtoneable, :dependent => :destroy - has_many :calls, :finder_sql => lambda { |s| "SELECT DISTINCT detailed_calls.* FROM detailed_calls WHERE presence_id LIKE '#{self.auth_name}@%' OR b_presence_id LIKE '#{self.auth_name}@%'" } + has_many :call_legs, :class_name => 'Call' + has_many :b_call_legs, :class_name => 'Call', :foreign_key => 'b_sip_account_id' # Delegations: # @@ -83,6 +84,10 @@ class SipAccount < ActiveRecord::Base def to_s truncate((self.caller_name || "SipAccount ID #{self.id}"), :length => GsParameter.get('TO_S_MAX_CALLER_NAME_LENGTH')) + " (#{truncate(self.auth_name, :length => GsParameter.get('TO_S_MAX_LENGTH_OF_AUTH_NAME'))}@...#{self.host.split(/\./)[2,3].to_a.join('.') if self.host })" end + + def calls + self.call_legs + self.b_call_legs + end def call_forwarding_toggle( call_forwarding_service, to_voicemail = nil ) if ! self.phone_numbers.first @@ -156,6 +161,24 @@ class SipAccount < ActiveRecord::Base end + def target_sip_accounts_by_permission(permission) + target_groups = Group.union(self.groups.collect{|g| g.permission_targets(permission)}) + target_groups = target_groups + Group.union(self.sip_accountable.groups.collect{|g| g.permission_targets(permission)}) + sip_accounts = [] + GroupMembership.where(:group_id => target_groups).each do |group_membership| + if group_membership.item.class == User || group_membership.item.class == Tenant + sip_accounts = sip_accounts + group_membership.item.sip_accounts + elsif group_membership.item.class == SipAccount + sip_accounts << group_membership.item + end + + sip_accounts = sip_accounts.uniq + end + + return sip_accounts + end + + private def save_value_of_to_s diff --git a/app/models/softkey.rb b/app/models/softkey.rb index 8049456..6063017 100644 --- a/app/models/softkey.rb +++ b/app/models/softkey.rb @@ -22,29 +22,28 @@ class Softkey < ActiveRecord::Base after_save :resync_phone after_destroy :resync_phone - def possible_blf_call_forwards - if self.sip_account.phone_numbers.count == 0 - nil - else - if self.sip_account.callforward_rules_act_per_sip_account == true - # We pick one phone_number and display the rules of it. - # - phone_number = self.sip_account.phone_numbers.order(:number).first - call_forwards = self.sip_account.call_forwards.where(:call_forwardable_id => phone_number.id, :call_forwardable_type => 'PhoneNumber') - else - call_forwards = self.sip_account.call_forwards - end - - phone_numbers_ids = self.sip_account.phone_number_ids - phone_numbers = PhoneNumber.where(:id => phone_numbers_ids).pluck(:number) + def possible_call_forwards + call_forwards = self.sip_account.call_forwards + self.sip_account.phone_numbers.each do |phone_number| + call_forwards = call_forwards + phone_number.call_forwards + end - hunt_group_ids = PhoneNumber.where(:phone_numberable_type => 'HuntGroupMember', :number => phone_numbers). - map{ |phone_number| phone_number.phone_numberable.hunt_group.id }. - uniq - call_forwards + CallForward.where(:destinationable_type => 'HuntGroup', :destinationable_id => hunt_group_ids, :call_forwardable_type => 'PhoneNumber'). - where('call_forwardable_id NOT IN (?)', phone_numbers_ids) - end + phone_numbers_ids = self.sip_account.phone_number_ids + phone_numbers = PhoneNumber.where(:id => phone_numbers_ids).pluck(:number) + + hunt_group_ids = PhoneNumber.where(:phone_numberable_type => 'HuntGroupMember', :number => phone_numbers). + map{ |phone_number| phone_number.phone_numberable.hunt_group.id }. + uniq + + call_forwards = call_forwards + CallForward.where(:destinationable_type => 'HuntGroup', :destinationable_id => hunt_group_ids, :call_forwardable_type => 'PhoneNumber'). + where('call_forwardable_id NOT IN (?)', phone_numbers_ids) + + return call_forwards + end + + def possible_blf_sip_accounts + self.sip_account.target_sip_accounts_by_permission('presence') end def to_s @@ -82,7 +81,29 @@ class Softkey < ActiveRecord::Base if self.softkey_function_id != nil case self.softkey_function.name when 'blf' + has_permission = false self.softkeyable = PhoneNumber.where(:number => self.number, :phone_numberable_type => 'SipAccount').first.try(:phone_numberable) + if self.softkeyable + self.sip_account.groups.each do |group| + if group.has_permission(self.softkeyable.class.name, self.softkeyable.id, :presence) + has_permission = true + break + end + end + if !has_permission && self.sip_account.sip_accountable + self.sip_account.sip_accountable.groups.each do |group| + if group.has_permission(self.softkeyable.class.name, self.softkeyable.id, :presence) + has_permission = true + break + end + end + end + end + + if !has_permission + self.softkeyable = nil + self.number = nil + end when 'conference' self.softkeyable = PhoneNumber.where(:number => self.number, :phone_numberable_type => 'Conference').first.try(:phone_numberable) when 'call_forwarding' diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 0622f52..ffa68a7 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -59,6 +59,10 @@ class Tenant < ActiveRecord::Base has_many :users_fax_accounts, :through => :users, :source => :fax_accounts, :readonly => true has_many :users_fax_accounts_phone_numbers, :through => :users_fax_accounts, :source => :phone_numbers, :readonly => true + # Groups + has_many :group_memberships, :as => :item, :dependent => :destroy, :uniq => true + has_many :groups, :through => :group_memberships + # Validations: # validates_presence_of :name, :state, :country, :language |