diff options
-rw-r--r-- | app/models/sip_account.rb | 31 | ||||
-rw-r--r-- | app/models/user.rb | 16 | ||||
-rw-r--r-- | db/migrate/20130224091700_add_initial_groups.rb | 22 |
3 files changed, 68 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 diff --git a/db/migrate/20130224091700_add_initial_groups.rb b/db/migrate/20130224091700_add_initial_groups.rb new file mode 100644 index 0000000..f680fb6 --- /dev/null +++ b/db/migrate/20130224091700_add_initial_groups.rb @@ -0,0 +1,22 @@ +class AddInitialGroups < ActiveRecord::Migration + def up + Group.create(:name => 'admins', :active => true, :comment => 'Administrator user accounts') + Group.create(:name => 'users', :active => true, :comment => 'Generic user accounts') + Group.create(:name => 'tenant_sip_accounts', :active => true, :comment => 'SIP accounts owned by tenants') + + user_sip_accounts = Group.create(:name => 'user_sip_accounts', :active => true, :comment => 'SIP accounts owned by user accounts') + user_sip_accounts.group_permissions.create(:permission => 'pickup', :target_group_id => user_sip_accounts.id) + + Group.create(:name => 'international_calls', :active => true, :comment => 'International calls permitted') + Group.create(:name => 'national_calls', :active => true, :comment => 'National calls permitted') + + GsParameter.create(:entity => 'group', :section => 'default', :name => 'User.admin', :value => '--- [admins]\n', :class_type => 'YAML') + GsParameter.create(:entity => 'group', :section => 'default', :name => 'User', :value => '--- [users]\n', :class_type => 'YAML') + GsParameter.create(:entity => 'group', :section => 'default', :name => 'SipAccount.user', :value => '--- [user_sip_accounts, international_calls, national_calls]\n', :class_type => 'YAML') + GsParameter.create(:entity => 'group', :section => 'default', :name => 'SipAccount.tenant', :value => '--- [tenant_sip_accounts]\n', :class_type => 'YAML') + end + + def down + Group.destroy_all + end +end |