zuul 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,11 @@ module Zuul
9
9
  module ClassMethods
10
10
  def self.extended(base)
11
11
  base.send :extend, RoleMethods
12
- base.send :extend, PermissionMethods if base.auth_scope.config.with_permissions
12
+ base.send :before_destroy, :destroy_zuul_roles
13
+ if base.auth_scope.config.with_permissions
14
+ base.send :extend, PermissionMethods
15
+ base.send :before_destroy, :destroy_zuul_permissions
16
+ end
13
17
  end
14
18
  end
15
19
 
@@ -25,6 +29,13 @@ module Zuul
25
29
  def allowed?(subject, role)
26
30
  subject.has_role?(role, self)
27
31
  end
32
+
33
+ def destroy_zuul_roles
34
+ auth_scopes.each do |name,scope|
35
+ scope.role_class.where(:context_type => self.class.name, :context_id => self.id).each(&:destroy)
36
+ scope.role_subject_class.where(:context_type => self.class.name, :context_id => self.id).each(&:destroy)
37
+ end
38
+ end
28
39
  end
29
40
 
30
41
  module PermissionMethods
@@ -32,6 +43,14 @@ module Zuul
32
43
  def allowed_to?(subject, permission)
33
44
  subject.has_permission?(permission, self)
34
45
  end
46
+
47
+ def destroy_zuul_permissions
48
+ auth_scopes.each do |name,scope|
49
+ scope.permission_class.where(:context_type => self.class.name, :context_id => self.id).each(&:destroy)
50
+ scope.permission_role_class.where(:context_type => self.class.name, :context_id => self.id).each(&:destroy)
51
+ scope.permission_subject_class.where(:context_type => self.class.name, :context_id => self.id).each(&:destroy)
52
+ end
53
+ end
35
54
  end
36
55
  end
37
56
  end
@@ -21,9 +21,9 @@ module Zuul
21
21
  end
22
22
 
23
23
  def self.add_associations(base)
24
- base.send :has_many, base.auth_scope.permission_roles_table_name.to_sym
24
+ base.send :has_many, base.auth_scope.permission_roles_table_name.to_sym, :dependent => :destroy
25
25
  base.send :has_many, base.auth_scope.roles_table_name.to_sym, :through => base.auth_scope.permission_roles_table_name.to_sym
26
- base.send :has_many, base.auth_scope.permission_subjects_table_name.to_sym
26
+ base.send :has_many, base.auth_scope.permission_subjects_table_name.to_sym, :dependent => :destroy
27
27
  base.send :has_many, base.auth_scope.subjects_table_name.to_sym, :through => base.auth_scope.permission_subjects_table_name.to_sym
28
28
  end
29
29
  end
@@ -24,10 +24,10 @@ module Zuul
24
24
  end
25
25
 
26
26
  def self.add_associations(base)
27
- base.send :has_many, base.auth_scope.role_subjects_table_name.to_sym
27
+ base.send :has_many, base.auth_scope.role_subjects_table_name.to_sym, :dependent => :destroy
28
28
  base.send :has_many, base.auth_scope.subjects_table_name.to_sym, :through => base.auth_scope.role_subjects_table_name.to_sym
29
29
  if base.auth_scope.config.with_permissions
30
- base.send :has_many, base.auth_scope.permission_roles_table_name.to_sym
30
+ base.send :has_many, base.auth_scope.permission_roles_table_name.to_sym, :dependent => :destroy
31
31
  base.send :has_many, base.auth_scope.permissions_table_name.to_sym, :through => base.auth_scope.permission_roles_table_name.to_sym
32
32
  end
33
33
  end
@@ -14,7 +14,7 @@ module Zuul
14
14
 
15
15
  module ClassMethods
16
16
  def self.extended(base)
17
- base.send :has_many, base.auth_scope.role_subjects_table_name.to_sym
17
+ base.send :has_many, base.auth_scope.role_subjects_table_name.to_sym, :dependent => :destroy
18
18
  base.send :has_many, base.auth_scope.roles_table_name.to_sym, :through => base.auth_scope.role_subjects_table_name.to_sym
19
19
  end
20
20
  end
@@ -137,7 +137,7 @@ module Zuul
137
137
 
138
138
  module ClassMethods
139
139
  def self.extended(base)
140
- base.send :has_many, base.auth_scope.permission_subjects_table_name.to_sym
140
+ base.send :has_many, base.auth_scope.permission_subjects_table_name.to_sym, :dependent => :destroy
141
141
  base.send :has_many, base.auth_scope.permissions_table_name.to_sym, :through => base.auth_scope.permission_subjects_table_name.to_sym
142
142
  end
143
143
  end
data/lib/zuul/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zuul
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -52,4 +52,48 @@ describe "Zuul::ActiveRecord::Context" do
52
52
  context.allowed_to?(user, permission).should == user.has_permission?(permission, context)
53
53
  end
54
54
  end
55
+
56
+ describe "destroy_zuul_roles" do
57
+ it "should destroy all role_subjects and roles that use this context" do
58
+ context = Context.create(:name => 'Test Context')
59
+ user = User.create(:name => 'Tester')
60
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
61
+ ctxtrole = Role.create(:name => 'Context Admin', :slug => 'ctxtadmin', :level => 100, :context => context)
62
+ user.assign_role(:admin, context)
63
+ user.assign_role(:ctxtadmin, context)
64
+ Role.count.should == 2
65
+ RoleUser.where(:context_type => context.class.name, :context_id => context.id).count.should == 2
66
+ context.destroy
67
+ Role.count.should == 1
68
+ RoleUser.where(:context_type => context.class.name, :context_id => context.id).count.should == 0
69
+ end
70
+ end
71
+
72
+ describe "destroy_zuul_permissions" do
73
+ it "should not be available if permissions are disabled" do
74
+ Weapon.acts_as_authorization_context :with_permissions => false
75
+ Weapon.new.should_not respond_to(:destroy_zuul_permissions)
76
+ end
77
+
78
+ it "should destroy all permission_subjects, permission_roles and permissions that use this context" do
79
+ context = Context.create(:name => 'Test Context')
80
+ user = User.create(:name => 'Tester')
81
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
82
+ perm = Permission.create(:name => 'Edit', :slug => 'edit')
83
+ ctxtperm = Permission.create(:name => 'Context Edit', :slug => 'ctxtedit', :context => context)
84
+ user.assign_permission(:edit, context)
85
+ user.assign_permission(:ctxtedit, context)
86
+ role.assign_permission(:edit, context)
87
+
88
+ Permission.count.should == 2
89
+ Permission.where(:context_type => context.class.name, :context_id => context.id).count.should == 1
90
+ PermissionRole.where(:context_type => context.class.name, :context_id => context.id).count.should == 1
91
+ PermissionUser.where(:context_type => context.class.name, :context_id => context.id).count.should == 2
92
+ context.destroy
93
+ Permission.count.should == 1
94
+ Permission.where(:context_type => context.class.name, :context_id => context.id).count.should == 0
95
+ PermissionRole.where(:context_type => context.class.name, :context_id => context.id).count.should == 0
96
+ PermissionUser.where(:context_type => context.class.name, :context_id => context.id).count.should == 0
97
+ end
98
+ end
55
99
  end
@@ -87,6 +87,17 @@ describe "Zuul::ActiveRecord::Permission" do
87
87
  permission.should respond_to(:users)
88
88
  end
89
89
 
90
+ it "should use :dependent => :destroy for the permission_subjects association" do
91
+ Permission.acts_as_authorization_permission
92
+ User.acts_as_authorization_subject
93
+ permission = Permission.create(:name => 'Edit', :slug => 'edit')
94
+ user = User.create(:name => 'Tester')
95
+ user.assign_permission(:edit)
96
+ PermissionUser.count.should == 1
97
+ permission.destroy
98
+ PermissionUser.count.should == 0
99
+ end
100
+
90
101
  it "should use the reflection classes to create the has_many associations" do
91
102
  Skill.acts_as_authorization_permission :subject_class => :soldier, :role_class => :rank
92
103
  Skill.reflections.keys.should include(:skill_soldiers)
@@ -104,6 +115,17 @@ describe "Zuul::ActiveRecord::Permission" do
104
115
  permission.should respond_to(:permission_roles)
105
116
  permission.should respond_to(:roles)
106
117
  end
118
+
119
+ it "should use :dependent => :destroy for the permission_roles association" do
120
+ Permission.acts_as_authorization_permission
121
+ Role.acts_as_authorization_role
122
+ permission = Permission.create(:name => 'Edit', :slug => 'edit')
123
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
124
+ role.assign_permission(:edit)
125
+ PermissionRole.count.should == 1
126
+ permission.destroy
127
+ PermissionRole.count.should == 0
128
+ end
107
129
 
108
130
  it "should use the reflection classes to create the has_many associations" do
109
131
  Skill.acts_as_authorization_permission :subject_class => :soldier, :role_class => :rank
@@ -123,6 +123,17 @@ describe "Zuul::ActiveRecord::Role" do
123
123
  role.should respond_to(:role_users)
124
124
  role.should respond_to(:users)
125
125
  end
126
+
127
+ it "should use :dependent => :destroy for the role_subjects association" do
128
+ User.acts_as_authorization_subject
129
+ Role.acts_as_authorization_role
130
+ user = User.create(:name => 'Tester')
131
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
132
+ user.assign_role(:admin)
133
+ RoleUser.count.should == 1
134
+ role.destroy
135
+ RoleUser.count.should == 0
136
+ end
126
137
 
127
138
  it "should use the reflection classes to create the has_many associations" do
128
139
  Rank.acts_as_authorization_role :subject_class => :soldier, :with_permissions => false
@@ -262,6 +273,15 @@ describe "Zuul::ActiveRecord::Role" do
262
273
  role.should respond_to(:permission_roles)
263
274
  role.should respond_to(:permissions)
264
275
  end
276
+
277
+ it "should use :dependent => :destroy for the permission_roles association" do
278
+ permission = Permission.create(:name => 'Edit', :slug => 'edit')
279
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
280
+ role.assign_permission(:edit)
281
+ PermissionRole.count.should == 1
282
+ role.destroy
283
+ PermissionRole.count.should == 0
284
+ end
265
285
 
266
286
  it "should use the reflection classes to create the has_many associations" do
267
287
  Rank.acts_as_authorization_role :subject_class => :soldier, :permission_class => :skill
@@ -29,6 +29,15 @@ describe "Zuul::ActiveRecord::Subject" do
29
29
  user.should respond_to(:role_users)
30
30
  user.should respond_to(:roles)
31
31
  end
32
+
33
+ it "should use :dependent => :destroy for the role_subjects association" do
34
+ user = User.create(:name => 'Tester')
35
+ role = Role.create(:name => 'Admin', :slug => 'admin', :level => 100)
36
+ user.assign_role(:admin)
37
+ RoleUser.count.should == 1
38
+ user.destroy
39
+ RoleUser.count.should == 0
40
+ end
32
41
 
33
42
  it "should use the reflection classes to create the has_many associations" do
34
43
  Soldier.acts_as_authorization_subject :role_class => :rank, :with_permissions => false
@@ -717,6 +726,15 @@ describe "Zuul::ActiveRecord::Subject" do
717
726
  user.should respond_to(:permission_users)
718
727
  user.should respond_to(:permissions)
719
728
  end
729
+
730
+ it "should use :dependent => :destroy for the permission_subjects association" do
731
+ permission = Permission.create(:name => 'Edit', :slug => 'edit')
732
+ user = User.create(:name => 'Tester')
733
+ user.assign_permission(:edit)
734
+ PermissionUser.count.should == 1
735
+ user.destroy
736
+ PermissionUser.count.should == 0
737
+ end
720
738
 
721
739
  it "should use the reflection classes to create the has_many associations" do
722
740
  Soldier.acts_as_authorization_subject :permission_class => :skill, :role_class => :rank
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zuul
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-30 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport