diff options
author | Gemeinschaft Service Account <gs5@localhost.localdomain> | 2013-02-24 06:02:29 -0500 |
---|---|---|
committer | Gemeinschaft Service Account <gs5@localhost.localdomain> | 2013-02-24 06:02:29 -0500 |
commit | 966b8733a82f07f825ce07baff0d37e2a5863960 (patch) | |
tree | b7cb36d5d9e8015643870b452f960b80e226ac31 /app/models | |
parent | 6acbd878be8b88f784a9813a9114773b87ee065f (diff) |
add default groups
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/sip_account.rb | 31 | ||||
-rw-r--r-- | app/models/user.rb | 16 |
2 files changed, 46 insertions, 1 deletions
diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index 7df8e3b..2b8e95f 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -33,6 +33,9 @@ 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 + # Delegations: # delegate :host, :to => :sip_domain, :allow_nil => true @@ -67,6 +70,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)) } @@ -146,7 +150,7 @@ class SipAccount < ActiveRecord::Base true ); end - + private @@ -220,4 +224,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/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 |