summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/ability.rb13
-rw-r--r--app/models/backup_job.rb2
-rw-r--r--app/models/call.rb39
-rw-r--r--app/models/call_forward.rb128
-rw-r--r--app/models/fax_document.rb74
-rw-r--r--app/models/group.rb11
-rw-r--r--app/models/group_membership.rb37
-rw-r--r--app/models/group_permission.rb24
-rw-r--r--app/models/gs_parameter.rb8
-rw-r--r--app/models/hunt_group.rb2
-rw-r--r--app/models/intruder.rb35
-rw-r--r--app/models/phone_book_entry.rb2
-rw-r--r--app/models/phone_number.rb2
-rw-r--r--app/models/restore_job.rb24
-rw-r--r--app/models/ringtone.rb14
-rw-r--r--app/models/sip_account.rb45
-rw-r--r--app/models/softkey.rb6
-rw-r--r--app/models/user.rb16
18 files changed, 351 insertions, 131 deletions
diff --git a/app/models/ability.rb b/app/models/ability.rb
index d66577d..3ba4481 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -90,6 +90,11 @@ class Ability
# SIM cards
#
cannot [:edit, :update], SimCard
+
+ # Restore is only possible on a new system.
+ #
+ cannot :manage, RestoreJob
+
else
# Any user can do the following stuff.
#
@@ -126,11 +131,12 @@ class Ability
# SipAccounts and Phones
#
- can :read, SipAccount, :sip_accountable_type => 'User', :sip_accountable_id => user.id
+ can [:read, :call], 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, :phone_number_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
@@ -158,7 +164,7 @@ class Ability
# User can manage CallForwards of the PhoneNumbers of his
# own SipAccounts:
#
- can :manage, CallForward, :phone_number_id => user.phone_number_ids
+ can :manage, CallForward, :call_forwardable_id => user.phone_number_ids
# SoftkeyFunctions
#
@@ -176,6 +182,7 @@ class Ability
#
can :create, GemeinschaftSetup
can :manage, SipDomain
+ can [:create, :new, :show, :index], RestoreJob
end
end
diff --git a/app/models/backup_job.rb b/app/models/backup_job.rb
index 96574a7..a04f6c0 100644
--- a/app/models/backup_job.rb
+++ b/app/models/backup_job.rb
@@ -32,7 +32,7 @@ class BackupJob < ActiveRecord::Base
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)}"
+ system "cd #{backup_directory} && sudo /bin/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")
diff --git a/app/models/call.rb b/app/models/call.rb
index c0f0f08..db6d9d6 100644
--- a/app/models/call.rb
+++ b/app/models/call.rb
@@ -1,9 +1,31 @@
class Call < ActiveRecord::Base
self.table_name = 'detailed_calls'
self.primary_key = 'uuid'
+
+ attr_writer :sip_account_id
+
+ validates :dest,
+ :presence => true
- def readonly?
- return 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
+ end
+
+ def save(attributes=nil)
+
+ end
+
+ def call(number=nil)
+ if @sip_account && self.dest
+ return @sip_account.call(self.dest)
+ end
+ return false
end
def destroy
@@ -15,15 +37,25 @@ 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
- return SipAccount.where(:auth_name => result[1], :sip_domain_id => domain.id).first
+ @sip_account = SipAccount.where(:auth_name => result[1], :sip_domain_id => domain.id).first
end
end
+
+ return @sip_account
end
def sip_account_bleg
@@ -69,4 +101,5 @@ class Call < ActiveRecord::Base
true
end
end
+
end
diff --git a/app/models/call_forward.rb b/app/models/call_forward.rb
index c668993..a932e11 100644
--- a/app/models/call_forward.rb
+++ b/app/models/call_forward.rb
@@ -6,17 +6,17 @@ class CallForward < ActiveRecord::Base
:destination, :source, :depth, :active, :to_voicemail,
:hunt_group_id,
:call_forwardable_type, :call_forwardable_id,
- :call_forwarding_destination, :position, :uuid
+ :call_forwarding_destination, :position, :uuid,
+ :destinationable_type, :destinationable_id
- belongs_to :phone_number
belongs_to :call_forwardable, :polymorphic => true
- has_many :softkeys
+ belongs_to :destinationable, :polymorphic => true
+ has_many :softkeys, :as => :softkeyable
- acts_as_list :scope => [ :phone_number_id, :call_forward_case_id ]
+ acts_as_list :scope => [ :call_forwardable_id, :call_forwardable_type, :call_forward_case_id ]
- validates_presence_of :phone_number
validates_presence_of :call_forward_case_id
- validates_presence_of :destination, :if => Proc.new { |cf| cf.call_forwardable_type.to_s.downcase == 'phonenumber' || cf.call_forwardable_type.blank? }
+ validates_presence_of :destination, :if => Proc.new { |cf| cf.destinationable_type.to_s.downcase == 'phonenumber' || cf.destinationable_type.blank? }
validates_inclusion_of :destination,
:in => [ nil, '' ],
@@ -24,8 +24,8 @@ class CallForward < ActiveRecord::Base
belongs_to :call_forward_case
- validates_presence_of :depth
validates_numericality_of :depth,
+ :allow_nil => true,
:only_integer => true,
:greater_than_or_equal_to => 1,
:less_than_or_equal_to => (GsParameter.get('MAX_CALL_FORWARD_DEPTH').nil? ? 0 : GsParameter.get('MAX_CALL_FORWARD_DEPTH'))
@@ -44,25 +44,21 @@ class CallForward < ActiveRecord::Base
:in => [ nil ],
:if => Proc.new { |cf| cf.call_forward_case_id != 3 }
- validate :validate_empty_hunt_group, :if => Proc.new { |cf| cf.active == true && cf.call_forwardable_type == 'HuntGroup' && cf.call_forward_case.value == 'assistant' }
+ validate :validate_empty_hunt_group, :if => Proc.new { |cf| cf.active == true && cf.destinationable_type == 'HuntGroup' && cf.call_forward_case.value == 'assistant' }
validates_presence_of :uuid
validates_uniqueness_of :uuid
# Make sure the call forward's parent can't be changed:
before_validation { |cfwd|
- if cfwd.id \
- && cfwd.phone_number_id != cfwd.phone_number_id_was
- errors.add( :phone_number_id, "cannot be changed." )
+ if cfwd.id && (cfwd.call_forwardable_id != cfwd.call_forwardable_id_was || cfwd.call_forwardable_type != cfwd.call_forwardable_type_was)
+ errors.add( :call_forwardable_id, "cannot be changed." )
end
}
- #before_validation :set_call_forwardable
before_save :split_and_format_destination_numbers
after_save :set_presence
- after_save :work_through_callforward_rules_act_per_sip_account
after_save :deactivate_concurring_entries, :if => Proc.new { |cf| cf.active == true }
- before_destroy :check_if_other_callforward_rules_have_to_be_destroyed
before_destroy :deactivate_connected_softkeys
def case_string
@@ -70,70 +66,25 @@ class CallForward < ActiveRecord::Base
end
def to_s
- if self.call_forwardable_type.blank?
- self.call_forwardable_type = ''
+ if self.destinationable_type.blank?
+ self.destinationable_type = ''
else
- call_forwardable_type = " #{self.call_forwardable_type}"
+ destinationable_type = " #{self.destinationable_type}"
end
- if self.call_forwardable
- destination = "#{self.call_forwardable}#{call_forwardable_type}"
+ if self.destinationable
+ destination = "#{self.destinationable}#{destinationable_type}"
else
- destination = "#{self.destination}#{call_forwardable_type}"
+ destination = "#{self.destination}#{destinationable_type}"
end
- "#{self.phone_number} (#{I18n.t("call_forward_cases.#{self.call_forward_case}")}) -> #{destination}"
- end
-
- def set_this_callforward_rule_to_all_phone_numbers_of_the_parent_sip_account
- # This is to make sure that no recursion kicks in.
- #
- if ! self.phone_number.phone_numberable.respond_to? :callforward_rules_act_per_sip_account
- return false
- end
-
- old_value_of_callforward_rules_act_per_sip_account = self.phone_number.phone_numberable.callforward_rules_act_per_sip_account
- self.phone_number.phone_numberable.update_attributes({:callforward_rules_act_per_sip_account => false})
-
- attributes_of_this_call_forward = self.attributes.delete_if {|key, value| ['id','updated_at','created_at','phone_number_id','call_forward_case_id', 'uuid'].include?(key)}
- phone_numbers = self.phone_number.phone_numberable.phone_numbers.where('id != ?', self.phone_number.id)
-
- phone_numbers.each do |phone_number|
- # Problem
- call_forward = phone_number.call_forwards.find_or_create_by_call_forward_case_id_and_position(self.call_forward_case_id, self.position, attributes_of_this_call_forward)
- call_forward.update_attributes(attributes_of_this_call_forward)
- end
-
- self.phone_number.phone_numberable.update_attributes({:callforward_rules_act_per_sip_account => old_value_of_callforward_rules_act_per_sip_account})
- end
-
- def destroy_all_similar_callforward_rules_of_the_parent_sip_account
- # This is to make sure that no recursion kicks in.
- #
- if ! self.phone_number.phone_numberable.respond_to? :callforward_rules_act_per_sip_account
- return false
- end
-
- old_value_of_callforward_rules_act_per_sip_account = self.phone_number.phone_numberable.callforward_rules_act_per_sip_account
- self.phone_number.phone_numberable.update_attributes({:callforward_rules_act_per_sip_account => false})
-
- phone_numbers_of_parent_sip_account = self.phone_number.phone_numberable.phone_numbers.where('id != ?', self.phone_number.id)
-
- phone_numbers_of_parent_sip_account.each do |phone_number|
- if self.call_forwardable_type != 'Voicemail'
- phone_number.call_forwards.where(:call_forward_case_id => self.call_forward_case_id, :destination => self.destination).destroy_all
- else
- phone_number.call_forwards.where(:call_forward_case_id => self.call_forward_case_id, :call_forwardable_type => self.call_forwardable_type).destroy_all
- end
- end
-
- self.phone_number.phone_numberable.update_attributes({:callforward_rules_act_per_sip_account => old_value_of_callforward_rules_act_per_sip_account})
+ "#{self.call_forwardable} (#{I18n.t("call_forward_cases.#{self.call_forward_case}")}) -> #{destination}"
end
def call_forwarding_destination
- "#{self.call_forwardable_id}:#{self.call_forwardable_type}"
+ "#{self.destinationable_id}:#{self.destinationable_type}"
end
def call_forwarding_destination=(destination_record)
- self.call_forwardable_id, delimeter, self.call_forwardable_type = destination_record.to_s.partition(':')
+ self.destinationable_id, delimeter, self.destinationable_type = destination_record.to_s.partition(':')
end
def toggle
@@ -168,7 +119,7 @@ class CallForward < ActiveRecord::Base
state = 'terminated'
if self.active
- if self.call_forwardable_type and self.call_forwardable_type.downcase() == 'voicemail'
+ if self.destinationable_type and self.destinationable_type.downcase() == 'voicemail'
state = 'early'
else
state = 'confirmed'
@@ -187,47 +138,28 @@ class CallForward < ActiveRecord::Base
#return send_presence_event(self.call_forward_case.value, state)
end
- def set_call_forwardable
+ def set_destinationable
if @hunt_group_id && HuntGroup.where(:id => @hunt_group_id.to_i).count > 0
- self.call_forwardable = HuntGroup.where(:id => @hunt_group_id.to_i).first
+ self.destinationable = HuntGroup.where(:id => @hunt_group_id.to_i).first
end
if @to_voicemail && @to_voicemail.first.downcase == 'true'
- self.call_forwardable_type = 'Voicemail'
- self.call_forwardable_id = nil
+ self.destinationable_type = 'Voicemail'
+ self.destinationable_id = nil
end
end
- def work_through_callforward_rules_act_per_sip_account
- if ! self.phone_number.phone_numberable.respond_to? :callforward_rules_act_per_sip_account
- return false
- end
-
- if self.phone_number.phone_numberable.callforward_rules_act_per_sip_account == true
- self.set_this_callforward_rule_to_all_phone_numbers_of_the_parent_sip_account
- end
- end
-
- def check_if_other_callforward_rules_have_to_be_destroyed
- if ! self.phone_number.phone_numberable.respond_to? :callforward_rules_act_per_sip_account
- return false
- end
-
- if self.phone_number.phone_numberable.callforward_rules_act_per_sip_account == true
- self.destroy_all_similar_callforward_rules_of_the_parent_sip_account
- end
- end
def send_presence_event(state, call_forwarding_service = nil)
dialplan_function = "cftg-#{self.id}"
unique_id = "call_forwarding_#{self.id}"
if call_forwarding_service == 'always'
- dialplan_function = "cfutg-#{self.phone_number.id}"
- unique_id = "call_forwarding_number_#{self.phone_number.id}"
+ dialplan_function = "cfutg-#{self.call_forwardable.id}"
+ unique_id = "call_forwarding_number_#{self.call_forwardable.id}"
elsif call_forwarding_service == 'assistant'
- dialplan_function = "cfatg-#{self.phone_number.id}"
- unique_id = "call_forwarding_number_#{self.phone_number.id}"
+ dialplan_function = "cfatg-#{self.call_forwardable.id}"
+ unique_id = "call_forwarding_number_#{self.call_forwardable.id}"
end
if dialplan_function
@@ -245,7 +177,7 @@ class CallForward < ActiveRecord::Base
end
def deactivate_concurring_entries
- CallForward.where(:phone_number_id => self.phone_number_id, :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, :active => true).each do |call_forwarding_entry|
if call_forwarding_entry.id != self.id
call_forwarding_entry.update_attributes(:active => false)
end
@@ -253,7 +185,7 @@ class CallForward < ActiveRecord::Base
end
def validate_empty_hunt_group
- hunt_group = self.call_forwardable
+ hunt_group = self.destinationable
if hunt_group && hunt_group.hunt_group_members.where(:active => true).count == 0
errors.add(:call_forwarding_destination, 'HuntGroup has no active members')
end
diff --git a/app/models/fax_document.rb b/app/models/fax_document.rb
index 564d3bb..5b27965 100644
--- a/app/models/fax_document.rb
+++ b/app/models/fax_document.rb
@@ -1,8 +1,7 @@
class FaxDocument < ActiveRecord::Base
# attr_accessible :inbound, :transmission_time, :sent_at, :document_total_pages, :document_transferred_pages, :ecm_requested, :ecm_used, :image_resolution, :image_size, :local_station_id, :result_code, :result_text, :remote_station_id, :success, :transfer_rate, :t38_gateway_format, :t38_peer, :document
- mount_uploader :document, DocumentUploader
- mount_uploader :tiff, TiffUploader
+ mount_uploader :document, FaxDocumentUploader
validates_presence_of :document
validates_numericality_of :retry_counter, :only_integer => true, :greater_than_or_equal_to => 0
@@ -18,8 +17,10 @@ class FaxDocument < ActiveRecord::Base
has_many :fax_thumbnails, :order => :position, :dependent => :destroy
- after_create :convert_pdf_to_tiff
+ after_save :convert_to_tiff
after_create :render_thumbnails
+
+ after_destroy :remove_storage_dir
# Scopes
scope :inbound, where(:state => 'inbound')
@@ -57,7 +58,7 @@ class FaxDocument < ActiveRecord::Base
end
def create_thumbnails_and_save_them
- tmp_dir = "/tmp/fax_convertions/#{self.id}"
+ tmp_dir = "/var/spool/gemeinschaft/fax_convertions/#{self.id}"
FileUtils.mkdir_p tmp_dir
system("cd #{tmp_dir} && convert #{self.document.path} -colorspace Gray PNG:'fax_page.png'")
Dir.glob("#{tmp_dir}/fax_page*.png").each do |thumbnail|
@@ -68,17 +69,64 @@ class FaxDocument < ActiveRecord::Base
FileUtils.rm_rf tmp_dir
end
- private
- def convert_pdf_to_tiff
+ def tiff_to_pdf
+ if !File.exists?(self.tiff)
+ return nil
+ end
+
+ working_path, file_name = File.split(self.tiff)
+ pdf_file = "#{working_path}/#{File.basename(self.tiff, '.tiff')}.pdf"
+
+ system "tiff2pdf \\
+ -o \"#{pdf_file}\" \\
+ -p letter \\
+ -a \"#{self.remote_station_id}\" \\
+ -c \"AMOOMA Gemeinschaft version #{GsParameter.get('GEMEINSCHAFT_VERSION')}\" \\
+ -t \"#{self.remote_station_id}\" \"#{self.tiff}\""
+
+ if !File.exists?(pdf_file)
+ return nil
+ end
+
+ return pdf_file
+ end
+
+ def to_tiff
page_size_a4 = '595 842'
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"
- directory = "/tmp/GS-#{GsParameter.get('GEMEINSCHAFT_VERSION')}/faxes/#{self.id}"
- FileUtils.mkdir_p directory
- tiff_file_name = File.basename(self.document.to_s.downcase, ".pdf") + '.tiff'
- system "cd #{directory} && gs -q -r#{self.fax_resolution.resolution_value} -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=\"#{tiff_file_name}\" -c \"#{page_size_command}\" -- \"#{Rails.root.to_s}/public#{self.document.to_s}\""
- self.tiff = File.open("#{directory}/#{tiff_file_name}")
- self.save
- FileUtils.rm_rf directory
+ 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}\""
+
+ if !File.exists?("#{store_dir}/#{tiff_file}")
+ return nil
+ end
+
+ return "#{store_dir}/#{tiff_file}"
+ end
+
+ def store_dir
+ if self.try(:inbound)
+ "/var/opt/gemeinschaft/fax/in/#{self.id.to_i}"
+ else
+ "/var/opt/gemeinschaft/fax/out/#{self.id.to_i}"
+ end
+ end
+
+ private
+ def convert_to_tiff
+ if self.tiff.blank?
+ self.tiff = self.to_tiff
+ if self.tiff
+ return self.save
+ end
+ end
+ end
+
+ def remove_storage_dir
+ if File.directory?(self.store_dir)
+ FileUtils.rm_rf(self.store_dir)
+ end
end
end
diff --git a/app/models/group.rb b/app/models/group.rb
new file mode 100644
index 0000000..e0cfaab
--- /dev/null
+++ b/app/models/group.rb
@@ -0,0 +1,11 @@
+class Group < ActiveRecord::Base
+ attr_accessible :name, :active, :comment
+
+ has_many :group_memberships, :dependent => :destroy
+ has_many :group_permissions, :dependent => :destroy
+ has_many :permittances, :foreign_key => :target_group_id, :class_name => "GroupPermission", :dependent => :destroy
+
+ def to_s
+ self.name
+ end
+end
diff --git a/app/models/group_membership.rb b/app/models/group_membership.rb
new file mode 100644
index 0000000..0f04ae1
--- /dev/null
+++ b/app/models/group_membership.rb
@@ -0,0 +1,37 @@
+class GroupMembership < ActiveRecord::Base
+ attr_accessible :group_id, :item_type, :item_id
+
+ belongs_to :group
+ belongs_to :item, :polymorphic => true
+
+ validates :item_id,
+ :presence => true,
+ :uniqueness => { :scope => [:group_id, :item_type] }
+
+ validates :item_type,
+ :presence => true,
+ :uniqueness => { :scope => [:group_id, :item_id] }
+
+ validate :validate_item_type_consitency
+
+ validates :item,
+ :presence => true
+
+ def to_s
+ "#{self.item_type}: #{self.item}"
+ end
+
+ def item_type_allowed
+ fist_item = self.group.group_memberships.first.try(:item)
+ if fist_item
+ return fist_item.class.name
+ end
+ end
+
+ def validate_item_type_consitency
+ type_allowed = self.item_type_allowed
+ if type_allowed && type_allowed != self.item_type
+ errors.add(:item_type, "must be of type: #{type_allowed}")
+ end
+ end
+end
diff --git a/app/models/group_permission.rb b/app/models/group_permission.rb
new file mode 100644
index 0000000..fe988ba
--- /dev/null
+++ b/app/models/group_permission.rb
@@ -0,0 +1,24 @@
+class GroupPermission < ActiveRecord::Base
+ attr_accessible :group_id, :permission, :target_group_id
+
+ PERMISSION_TYPES = ['pickup']
+
+ belongs_to :group
+ belongs_to :target_group, :class_name => "Group"
+
+ validates :target_group_id,
+ :presence => true,
+ :uniqueness => { :scope => [:group_id, :permission] }
+
+ validates :target_group,
+ :presence => true
+
+ validates :permission,
+ :presence => true,
+ :uniqueness => { :scope => [:group_id, :target_group_id] },
+ :inclusion => { :in => PERMISSION_TYPES }
+
+ def to_s
+ "#{self.permission} => #{self.target_group}"
+ end
+end
diff --git a/app/models/gs_parameter.rb b/app/models/gs_parameter.rb
index fe2a845..cd4f47b 100644
--- a/app/models/gs_parameter.rb
+++ b/app/models/gs_parameter.rb
@@ -10,9 +10,13 @@ class GsParameter < ActiveRecord::Base
:presence => true,
:inclusion => { :in => ['String', 'Integer', 'Boolean', 'YAML', 'Nil'] }
- def self.get(wanted_variable)
+ def self.get(wanted_variable, entity=nil, section=nil)
if GsParameter.table_exists?
- item = GsParameter.where(:name => wanted_variable).first
+ if entity || section
+ item = GsParameter.where(:name => wanted_variable, :entity => entity, :section => section).first
+ else
+ item = GsParameter.where(:name => wanted_variable).first
+ end
if item.nil? || item.class_type == 'Nil'
return nil
else
diff --git a/app/models/hunt_group.rb b/app/models/hunt_group.rb
index 5011bf0..7338606 100644
--- a/app/models/hunt_group.rb
+++ b/app/models/hunt_group.rb
@@ -2,7 +2,7 @@ class HuntGroup < ActiveRecord::Base
attr_accessible :name, :strategy, :seconds_between_jumps, :phone_numbers_attributes
belongs_to :tenant, :touch => true
- has_many :call_forwards, :as => :call_forwardable, :dependent => :destroy
+ has_many :call_forwards, :as => :destinationable, :dependent => :destroy
validates_uniqueness_of :name, :scope => :tenant_id,
:allow_nil => true, :allow_blank => true
diff --git a/app/models/intruder.rb b/app/models/intruder.rb
index 249fffc..97e3773 100644
--- a/app/models/intruder.rb
+++ b/app/models/intruder.rb
@@ -31,6 +31,41 @@ 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,
+ :points => self.points,
+ :bans => self.bans,
+ :received_port => self.contact_port,
+ :received_ip => self.contact_ip,
+ :contact_count => self.contact_count,
+ :user_agent => self.user_agent,
+ :to_user => self.to_user,
+ :comment => self.comment,
+ :date => DateTime.now.strftime('%Y-%m-%d %X')
+ }
+ end
+
private
def set_key_if_empty
if self.key.blank?
diff --git a/app/models/phone_book_entry.rb b/app/models/phone_book_entry.rb
index 275c7b6..96c8468 100644
--- a/app/models/phone_book_entry.rb
+++ b/app/models/phone_book_entry.rb
@@ -1,6 +1,8 @@
# encoding: UTF-8
class PhoneBookEntry < ActiveRecord::Base
+ PHONE_NUMBER_NAMES = ['Phone', 'Office', 'Home', 'Mobile', 'Fax']
+
before_save :run_phonetic_algorithm
before_save :save_value_of_to_s
diff --git a/app/models/phone_number.rb b/app/models/phone_number.rb
index d1e950f..f6453ce 100644
--- a/app/models/phone_number.rb
+++ b/app/models/phone_number.rb
@@ -3,7 +3,7 @@ class PhoneNumber < ActiveRecord::Base
attr_accessible :name, :number, :gs_node_id, :access_authorization_user_id
- has_many :call_forwards, :dependent => :destroy
+ has_many :call_forwards, :as => :call_forwardable, :dependent => :destroy
has_many :ringtones, :as => :ringtoneable, :dependent => :destroy
diff --git a/app/models/restore_job.rb b/app/models/restore_job.rb
new file mode 100644
index 0000000..80741ac
--- /dev/null
+++ b/app/models/restore_job.rb
@@ -0,0 +1,24 @@
+class RestoreJob < ActiveRecord::Base
+ attr_accessible :state, :backup_file
+
+ mount_uploader :backup_file, BackupFileUploader
+
+ after_create :queue_the_restore_rake_task
+
+ def to_s
+ if self.backup_file?
+ File.basename(self.backup_file.to_s)
+ else
+ "RestoreJob ID #{self.id}"
+ end
+ end
+
+ private
+ def queue_the_restore_rake_task
+ self.delay.run_the_restore_rake_task
+ end
+
+ def run_the_restore_rake_task
+ system "cd #{Rails.root} && rake backup:restore"
+ end
+end
diff --git a/app/models/ringtone.rb b/app/models/ringtone.rb
index 36053c0..45ecd93 100644
--- a/app/models/ringtone.rb
+++ b/app/models/ringtone.rb
@@ -1,11 +1,25 @@
class Ringtone < ActiveRecord::Base
attr_accessible :audio, :bellcore_id
+ CORE_RINGTONES_AVAILABLE = {
+ 'Silence' => 0,
+ 'Ringtone 1' => 1,
+ 'Ringtone 2' => 2,
+ 'Ringtone 3' => 3,
+ 'Ringtone 4' => 4,
+ 'Ringtone 5' => 5,
+ 'Ringtone 6' => 6,
+ 'Ringtone 7' => 7,
+ 'Ringtone 8' => 8,
+ 'Ringtone 9' => 9,
+ 'Ringtone 10' => 10,
+ }
mount_uploader :audio, AudioUploader
validates_presence_of :audio, :if => Proc.new{ |ringtone| ringtone.bellcore_id.blank? }
validates_presence_of :ringtoneable_type
validates_presence_of :ringtoneable_id
validates_presence_of :ringtoneable
+ validates_uniqueness_of :ringtoneable_id, :scope => [:ringtoneable_type]
belongs_to :ringtoneable, :polymorphic => true
diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb
index 7df8e3b..2040b41 100644
--- a/app/models/sip_account.rb
+++ b/app/models/sip_account.rb
@@ -16,7 +16,7 @@ class SipAccount < ActiveRecord::Base
has_many :phones, :through => :phone_sip_accounts
has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy
- has_many :call_forwards, :through => :phone_numbers
+ has_many :call_forwards, :as => :call_forwardable, :dependent => :destroy
belongs_to :tenant
belongs_to :sip_domain
@@ -33,6 +33,13 @@ class SipAccount < ActiveRecord::Base
belongs_to :language, :foreign_key => 'language_code', :primary_key => 'code'
+ has_many :group_memberships, :as => :item, :dependent => :destroy, :uniq => true
+ has_many :groups, :through => :group_memberships
+
+ 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}@%'" }
+
# Delegations:
#
delegate :host, :to => :sip_domain, :allow_nil => true
@@ -67,6 +74,7 @@ class SipAccount < ActiveRecord::Base
validates_uniqueness_of :uuid
after_create { self.create_on_other_gs_nodes('sip_accountable', self.sip_accountable.try(:uuid)) }
+ after_create :create_default_group_memberships
after_destroy :destroy_on_other_gs_nodes
after_update { self.update_on_other_gs_nodes('sip_accountable', self.sip_accountable.try(:uuid)) }
@@ -92,8 +100,8 @@ class SipAccount < ActiveRecord::Base
if call_forwarding_master.active
call_forwarding_master.active = false
else
- if call_forwarding_service = 'assistant' && call_forwarding_master.call_forwardable_type == 'HuntGroup' && call_forwarding_master.call_forwardable
- if call_forwarding_master.call_forwardable.hunt_group_members.where(:active => true).count > 0
+ if call_forwarding_service = 'assistant' && call_forwarding_master.destinationable_type == 'HuntGroup' && call_forwarding_master.destinationable
+ if call_forwarding_master.destinationable.hunt_group_members.where(:active => true).count > 0
call_forwarding_master.active = true
else
call_forwarding_master.active = false
@@ -105,7 +113,7 @@ class SipAccount < ActiveRecord::Base
call_forwarding = phone_number.call_forwards.where(:call_forward_case_id => service_id).order(:active).all(:conditions => 'source IS NULL OR source = ""').first
if ! call_forwarding
call_forwarding = CallForward.new()
- call_forwarding.phone_number_id = phone_number.id
+ call_forwarding.call_forwardable = phone_number
end
if to_voicemail == nil
@@ -146,7 +154,7 @@ class SipAccount < ActiveRecord::Base
true
);
end
-
+
private
@@ -202,7 +210,7 @@ class SipAccount < ActiveRecord::Base
# log out phone if sip_account is not on this node
def log_out_phone_if_not_local
- if self.gs_node_id && ! GsNode.where(:ip_address => GsParameter.get('HOMEBASE_IP_ADDRESS'), :id => self.gs_node_id).first
+ if self.gs_node_id && GsNode.count > 1 && ! GsNode.where(:ip_address => GsParameter.get('HOMEBASE_IP_ADDRESS'), :id => self.gs_node_id).first
self.phones.each do |phone|
phone.user_logout;
end
@@ -220,4 +228,29 @@ class SipAccount < ActiveRecord::Base
voicemail_setting.purge = false
voicemail_setting.save
end
+
+ def create_default_group_memberships
+ default_groups = Hash.new()
+ templates = GsParameter.get('SipAccount', 'group', 'default')
+ if templates.class == Array
+ templates.each do |group_name|
+ default_groups[group_name] = true
+ end
+ end
+
+ templates = GsParameter.get("SipAccount.#{self.sip_accountable_type}", 'group', 'default')
+ if templates.class == Array
+ templates.each do |group_name|
+ default_groups[group_name] = true
+ end
+ end
+
+ default_groups.each do |group_name, value|
+ group = Group.where(:name => group_name).first
+ if group
+ self.group_memberships.create(:group_id => group.id)
+ end
+ end
+ end
+
end
diff --git a/app/models/softkey.rb b/app/models/softkey.rb
index 4b758e0..8049456 100644
--- a/app/models/softkey.rb
+++ b/app/models/softkey.rb
@@ -30,7 +30,7 @@ class Softkey < ActiveRecord::Base
# 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(:phone_number_id => phone_number.id)
+ 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
@@ -42,8 +42,8 @@ class Softkey < ActiveRecord::Base
map{ |phone_number| phone_number.phone_numberable.hunt_group.id }.
uniq
- call_forwards + CallForward.where(:call_forwardable_type => 'HuntGroup', :call_forwardable_id => hunt_group_ids).
- where('phone_number_id NOT IN (?)', phone_numbers_ids)
+ 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
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 6c67351..913d75f 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -6,6 +6,7 @@ class User < ActiveRecord::Base
# Sync other nodes when this is a cluster.
#
after_create :create_on_other_gs_nodes
+ after_create :create_default_group_memberships
after_destroy :destroy_on_other_gs_nodes
after_update :update_on_other_gs_nodes
@@ -89,6 +90,9 @@ class User < ActiveRecord::Base
belongs_to :gs_node
has_many :parking_stalls, :as => :parking_stallable, :dependent => :destroy
+
+ has_many :group_memberships, :as => :item, :dependent => :destroy, :uniq => true
+ has_many :groups, :through => :group_memberships
# Avatar like photo
mount_uploader :image, ImageUploader
@@ -226,4 +230,16 @@ class User < ActiveRecord::Base
end
end
+ def create_default_group_memberships
+ templates = GsParameter.get('User', 'group', 'default')
+ if templates.class == Array
+ templates.each do |group_name|
+ group = Group.where(:name => group_name).first
+ if group
+ self.group_memberships.create(:group_id => group.id)
+ end
+ end
+ end
+ end
+
end