summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/ability.rb12
-rw-r--r--app/models/backup_job.rb51
-rw-r--r--app/models/call.rb70
-rw-r--r--app/models/call_forward.rb2
-rw-r--r--app/models/call_route.rb8
-rw-r--r--app/models/gateway.rb6
-rw-r--r--app/models/gateway_setting.rb1
-rw-r--r--app/models/intruder.rb37
-rw-r--r--app/models/sip_account.rb2
-rw-r--r--app/models/softkey.rb32
-rw-r--r--app/models/system_message.rb7
11 files changed, 175 insertions, 53 deletions
diff --git a/app/models/ability.rb b/app/models/ability.rb
index 0d13dab..4c0844c 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -56,14 +56,14 @@ class Ability
can :read, PhoneBookEntry, :phone_book => { :id => user_group.phone_book_ids }
end
- # SystemMessages
- #
- cannot [:destroy, :edit, :update], SystemMessage
-
# A FacDocument can't be changed
#
cannot [:edit, :update], FaxDocument
+ # Backups can't be edited
+ #
+ cannot [:edit, :update], BackupJob
+
# Can manage GsNodes
#
can :manage, GsNode
@@ -156,10 +156,6 @@ class Ability
#
can :manage, CallForward, :phone_number_id => user.phone_number_ids
- # SystemMessages
- #
- can :read, SystemMessage, :user_id => user.id
-
# SoftkeyFunctions
#
can :read, SoftkeyFunction
diff --git a/app/models/backup_job.rb b/app/models/backup_job.rb
new file mode 100644
index 0000000..96574a7
--- /dev/null
+++ b/app/models/backup_job.rb
@@ -0,0 +1,51 @@
+class BackupJob < ActiveRecord::Base
+ attr_accessible :started_at, :finished_at, :state, :directory
+
+ mount_uploader :backup_file, BackupFileUploader
+
+ before_create :set_state_to_queued
+ after_create :initiate_backup
+
+ def to_s
+ self.started_at.to_s
+ end
+
+ private
+ def set_state_to_queued
+ self.state = 'queued'
+ self.started_at = Time.now
+ end
+
+ def initiate_backup
+ self.delay.make_a_backup
+ end
+
+ def make_a_backup
+ backup_directory = '/var/backups/GS5'
+ backup_name_prefix = 'GS5-backup-'
+ if self.finished_at.nil? && self.state == 'queued'
+ self.state = 'running'
+ self.save
+ original_directories = Dir.glob("#{backup_directory}/*")
+ system "backup perform --trigger GS5 --config_file #{Rails.root.join('config','backup.rb')}"
+ tmp_backup_directory = (Dir.glob("#{backup_directory}/*") - original_directories).first
+ if tmp_backup_directory.blank?
+ self.state = 'failed'
+ else
+ system "cd #{backup_directory} && tar czf #{backup_name_prefix}#{File.basename(tmp_backup_directory)}.tar.gz #{File.basename(tmp_backup_directory)}"
+ require 'fileutils'
+ FileUtils.rm_rf tmp_backup_directory
+ file = File::Stat.new("#{backup_directory}/#{backup_name_prefix}#{File.basename(tmp_backup_directory)}.tar.gz")
+
+ self.directory = File.basename(tmp_backup_directory)
+
+ self.backup_file = File.open("#{backup_directory}/#{backup_name_prefix}#{File.basename(tmp_backup_directory)}.tar.gz")
+
+ self.finished_at = Time.now
+ self.state = 'successful'
+ end
+ self.save
+ end
+ end
+
+end
diff --git a/app/models/call.rb b/app/models/call.rb
index 57961ec..c0f0f08 100644
--- a/app/models/call.rb
+++ b/app/models/call.rb
@@ -1,36 +1,72 @@
class Call < ActiveRecord::Base
- self.table_name = 'channels'
+ self.table_name = 'detailed_calls'
self.primary_key = 'uuid'
- # Makes sure that this is a readonly model.
def readonly?
return true
end
-
- # Prevent objects from being destroyed
- def before_destroy
- raise ActiveRecord::ReadOnlyRecord
- end
- # Prevent objects from being deleted
- def self.delete_all
- raise ActiveRecord::ReadOnlyRecord
+ def destroy
+ return self.delete
end
- # Prevent objects from being deleted
def delete
- raise ActiveRecord::ReadOnlyRecord
+ require 'freeswitch_event'
+ return FreeswitchAPI.execute('uuid_kill', self.uuid, true);
end
def sip_account
- auth_name = self.name.match('^.+[/:](.+)@.+$')
- if auth_name && ! auth_name[1].blank?
- return SipAccount.where(:auth_name => auth_name[1]).first
+ result = self.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 kill
+ 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
+ end
+
require 'freeswitch_event'
- return FreeswitchAPI.execute('uuid_kill', self.uuid, true);
+ result = FreeswitchAPI.channel_variable_get(channel_uuid, variable_name);
+
+ if result == '_undef_'
+ return nil
+ end
+
+ return result
+ end
+
+ def get_variable(variable_name)
+ return get_variable_from_uuid(self.uuid, variable_name);
+ end
+
+ def get_variable_bleg(variable_name)
+ 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 8a8d1df..c668993 100644
--- a/app/models/call_forward.rb
+++ b/app/models/call_forward.rb
@@ -145,7 +145,7 @@ class CallForward < ActiveRecord::Base
softkey_function_deactivated = SoftkeyFunction.find_by_name('deactivated')
self.softkeys.each do |softkey|
if softkey.softkey_function_id != softkey_function_deactivated.id
- softkey.update_attributes(:call_forward_id => nil, :softkey_function_id => softkey_function_deactivated.id)
+ softkey.update_attributes(:softkeyable_id => nil, :softkeyable_type => nil, :softkey_function_id => softkey_function_deactivated.id)
end
end
end
diff --git a/app/models/call_route.rb b/app/models/call_route.rb
index b4496ab..8bc811a 100644
--- a/app/models/call_route.rb
+++ b/app/models/call_route.rb
@@ -2,9 +2,9 @@ class CallRoute < ActiveRecord::Base
# https://github.com/rails/strong_parameters
include ActiveModel::ForbiddenAttributesProtection
- ROUTING_TABLES = ['prerouting', 'outbound', 'inbound']
+ ROUTING_TABLES = ['prerouting', 'outbound', 'inbound', 'dtmf']
- has_many :route_elements, :dependent => :destroy
+ has_many :route_elements, :dependent => :destroy, :order => :position
validates :name,
:presence => true
@@ -239,6 +239,10 @@ class CallRoute < ActiveRecord::Base
end
end
+ def endpoint_str
+ "#{endpoint_type}=#{endpoint_id}"
+ end
+
def endpoint
if self.endpoint_id.to_i > 0
begin
diff --git a/app/models/gateway.rb b/app/models/gateway.rb
index a8df41f..2f17b57 100644
--- a/app/models/gateway.rb
+++ b/app/models/gateway.rb
@@ -1,10 +1,12 @@
class Gateway < ActiveRecord::Base
TECHNOLOGIES = ['sip', 'xmpp']
+ GATEWAY_PREFIX = 'gateway'
attr_accessible :name, :technology, :inbound, :outbound, :description
has_many :gateway_settings, :dependent => :destroy
has_many :gateway_parameters, :dependent => :destroy
+ has_many :call_routes, :as => :endpoint, :dependent => :nullify
validates :name,
:presence => true,
@@ -21,6 +23,10 @@ class Gateway < ActiveRecord::Base
name
end
+ def identifier
+ "#{GATEWAY_PREFIX}#{self.id}"
+ end
+
private
def downcase_technology
self.technology = self.technology.downcase if !self.technology.blank?
diff --git a/app/models/gateway_setting.rb b/app/models/gateway_setting.rb
index e3eaadb..381dde5 100644
--- a/app/models/gateway_setting.rb
+++ b/app/models/gateway_setting.rb
@@ -10,6 +10,7 @@ class GatewaySetting < ActiveRecord::Base
'auth_source' => 'String',
'auth_pattern' => 'String',
'number_source' => 'String',
+ 'profile' => 'String',
},
'xmpp' => {
'server' => 'String',
diff --git a/app/models/intruder.rb b/app/models/intruder.rb
new file mode 100644
index 0000000..db14bf8
--- /dev/null
+++ b/app/models/intruder.rb
@@ -0,0 +1,37 @@
+class Intruder < ActiveRecord::Base
+ attr_accessible :list_type, :key, :points, :bans, :ban_last, :ban_end, :contact_ip, :contact_port, :contact_count, :contact_last, :contacts_per_second, :contacts_per_second_max, :user_agent, :to_user, :comment
+
+ LIST_TYPES = ['blacklist', 'whitelist']
+
+ validates :list_type,
+ :presence => true,
+ :inclusion => { :in => LIST_TYPES }
+
+ validates :key,
+ :presence => true,
+ :uniqueness => true
+
+ validates :contact_ip,
+ :presence => true,
+ :uniqueness => true
+
+ before_validation :set_key_if_empty
+
+
+ def whois
+ if ! self.contact_ip.blank?
+ begin
+ return Whois.whois(self.contact_ip)
+ rescue
+ return nil
+ end
+ end
+ end
+
+ private
+ def set_key_if_empty
+ if self.key.blank?
+ self.key = self.contact_ip
+ end
+ end
+end
diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb
index 444eb12..9ba1f8b 100644
--- a/app/models/sip_account.rb
+++ b/app/models/sip_account.rb
@@ -21,7 +21,7 @@ class SipAccount < ActiveRecord::Base
belongs_to :tenant
belongs_to :sip_domain
- has_many :softkeys, :dependent => :destroy
+ has_many :softkeys, :dependent => :destroy, :order => :position
has_many :voicemail_messages, :foreign_key => 'username', :primary_key => 'auth_name'
diff --git a/app/models/softkey.rb b/app/models/softkey.rb
index a709036..83c88ab 100644
--- a/app/models/softkey.rb
+++ b/app/models/softkey.rb
@@ -1,13 +1,11 @@
class Softkey < ActiveRecord::Base
- attr_accessible :softkey_function_id, :number, :label, :call_forward_id, :uuid
+ attr_accessible :softkey_function_id, :number, :label, :uuid, :softkeyable_type, :softkeyable_id
belongs_to :sip_account
belongs_to :softkey_function
- belongs_to :call_forward
+ belongs_to :softkeyable, :polymorphic => true
- # Any CallForward BLF must have a valid softkey_call_forward_id.
- #
- validates_presence_of :call_forward_id, :if => Proc.new{ |softkey| self.softkey_function_id != nil &&
+ validates_presence_of :softkeyable_id, :if => Proc.new{ |softkey| self.softkey_function_id != nil &&
self.softkey_function_id == SoftkeyFunction.find_by_name('call_forwarding').try(:id) }
# These functions need a number to act.
@@ -21,7 +19,6 @@ class Softkey < ActiveRecord::Base
acts_as_list :scope => :sip_account
before_validation :clean_up_and_leave_only_values_which_make_sense_for_the_current_softkey_function_id
- after_validation :save_function_name_in_function, :if => Proc.new{ |softkey| self.call_forward_id.blank? }
after_save :resync_phone
after_destroy :resync_phone
@@ -52,7 +49,7 @@ class Softkey < ActiveRecord::Base
def to_s
if (['call_forwarding'].include?(self.softkey_function.name))
- "#{self.call_forward}"
+ "#{self.softkeyable}"
else
if ['log_out', 'log_in'].include?(self.softkey_function.name)
I18n.t("softkeys.functions.#{self.softkey_function.name}")
@@ -78,21 +75,22 @@ class Softkey < ActiveRecord::Base
end
private
-
- def save_function_name_in_function
- self.function = self.softkey_function.name
- end
-
# Make sure that no number is set when there is no need for one.
# And make sure that there is no CallForward connected when not needed.
#
def clean_up_and_leave_only_values_which_make_sense_for_the_current_softkey_function_id
- if self.softkey_function_id != nil
- if ['blf','speed_dial','dtmf','conference'].include?(self.softkey_function.name)
- self.call_forward_id = nil
- end
- if ['call_forwarding'].include?(self.softkey_function.name)
+ if self.softkey_function_id != nil
+ case self.softkey_function.name
+ when 'blf'
+ self.softkeyable = PhoneNumber.where(:number => self.number, :phone_numberable_type => 'SipAccount').first.try(:phone_numberable)
+ when 'conference'
+ self.softkeyable = PhoneNumber.where(:number => self.number, :phone_numberable_type => 'Conference').first.try(:phone_numberable)
+ when 'call_forwarding'
+ self.softkeyable = CallForward.where(:id => self.softkeyable_id).first
self.number = nil
+ else
+ self.softkeyable_id = nil
+ self.softkeyable_type = nil
end
end
end
diff --git a/app/models/system_message.rb b/app/models/system_message.rb
deleted file mode 100644
index 0d9e862..0000000
--- a/app/models/system_message.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-class SystemMessage < ActiveRecord::Base
- attr_accessible :content
-
- belongs_to :user
-
- validates_presence_of :content
-end