effective_roles 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fea6abc1820780d612a22a1c66c180cdc17f1ab6
4
- data.tar.gz: 37047ba965d662456396b4bd1aa1c7557b655677
3
+ metadata.gz: aa6baafe8b9e81b1baa9862c885beb88c8483735
4
+ data.tar.gz: b4beee32f21b88b94046c6a467b681f0c1142ce7
5
5
  SHA512:
6
- metadata.gz: d5214db952f1b44cf46f0d95239e27b938a97a3d27282a3be7a1863f530a1eba9c63d43a7c1f89669e8d42bdead1f57dd231fa59a2e33779b64e8b6db7c5ecc2
7
- data.tar.gz: 833ae9a7a654221bb1144b289628a8aeb35b2817d39fcee986ea3e9df62a7dd3fe0ed15c69f926e6a2e8173292b2df09228e7ebca9634cd757096c3676d07a19
6
+ metadata.gz: ebb6efc26b4c837b5c8ed50dbb3c277c146092c439f374ad42740cfbb3a8803650479e19dcd9e5a0b09eed19c5e2e2f7327383f69a1bf24e726c7b93e35b8355
7
+ data.tar.gz: bf6748ec5daa3e3560a8c3352bde647a12ebc4d3565ac6bd17c4071641ce22ed17855588ccc70566574be6fcbde93edfe3b37be5fd73c38104df48ca053d19f7
data/README.md CHANGED
@@ -103,39 +103,29 @@ post.roles
103
103
  Compare against another acts_as_role_restricted object:
104
104
 
105
105
  ```ruby
106
- user = User.new()
107
- user.roles = []
108
-
109
106
  post = Post.new()
110
107
  post.roles = [:admin]
111
108
 
112
- user.roles_match_with?(post)
113
- => false # User has no roles, but Post requires :admin
109
+ user = User.new()
110
+ user.roles = []
114
111
 
115
- post.roles_match_with?(user)
116
- => true # Post has the role of :admin, but User requires no roles
112
+ post.roles_permit?(user)
113
+ => false # Post requires the :admin role, but User has no admin role
117
114
  ```
118
115
 
119
116
  ```ruby
120
- user.roles = [:admin]
121
117
  post.roles = [:superadmin]
118
+ user.roles = [:admin]
122
119
 
123
- user.roles_match_with?(post)
120
+ post.roles_permit?(user)
124
121
  => false # User does not have the superadmin role
125
122
 
126
- post.roles_match_with?(user)
127
- => false # Post does not have the admin role
128
- ```
129
-
130
123
  ```ruby
131
- user.roles = [:superadmin, :admin]
132
124
  post.roles = [:admin]
125
+ user.roles = [:superadmin, :admin]
133
126
 
134
- user.roles_match_with?(post)
135
- => true # User has :admin and so does Post
136
-
137
- post.roles_match_with?(user)
138
- => true # Post has :admin and so does User
127
+ post.roles_permit?(user)
128
+ => true # User has required :admin role
139
129
  ```
140
130
 
141
131
  ### Finder Methods
@@ -74,19 +74,27 @@ module ActsAsRoleRestricted
74
74
  roles.include?(role.try(:to_sym))
75
75
  end
76
76
 
77
- # Does self have permission to view obj?
78
- def roles_match_with?(obj)
79
- if obj.respond_to?(:is_role_restricted?)
80
- obj.is_role_restricted? == false || (roles & obj.roles).any?
81
- elsif Integer(obj) > 0
82
- (roles & EffectiveRoles.roles_for_roles_mask(obj)).any?
83
- else
84
- raise 'unsupported object passed to roles_permit?(obj). Expecting an acts_as_role_restricted object or a roles_mask integer'
85
- end
77
+ # Are both objects unrestricted, or do any roles overlap?
78
+ def roles_overlap?(obj)
79
+ obj_roles = EffectiveRoles.roles_for(obj)
80
+ (roles.blank? && obj_roles.blank?) || (roles & obj_roles).any?
81
+ end
82
+
83
+ # Are both objects unrestricted, or are both roles identical?
84
+ def roles_match?(obj)
85
+ obj_roles = EffectiveRoles.roles_for(obj)
86
+ matching_roles = (roles & obj_roles)
87
+ matching_roles.length == roles.length && matching_roles.length == obj_roles.length
88
+ end
89
+
90
+ # Any I unrestricted, or do any roles overlap?
91
+ def roles_permit?(obj)
92
+ roles.blank? || roles_overlap?(obj)
86
93
  end
87
94
 
88
95
  def is_role_restricted?
89
- (roles_mask || 0) > 0
96
+ roles.present?
90
97
  end
98
+
91
99
  end
92
100
 
@@ -12,9 +12,16 @@ module EffectiveRoles
12
12
  yield self
13
13
  end
14
14
 
15
- def self.roles_for_roles_mask(roles_mask)
16
- roles_mask = Integer(roles_mask || 0)
17
- roles.reject { |r| (roles_mask & 2**roles.index(r)).zero? }
15
+ def self.roles_for(obj)
16
+ if obj.respond_to?(:is_role_restricted?)
17
+ obj.roles
18
+ elsif obj.kind_of?(Integer)
19
+ roles.reject { |r| (obj & 2**roles.index(r)).zero? }
20
+ elsif obj.nil?
21
+ []
22
+ else
23
+ raise 'unsupported object passed to EffectiveRoles.roles_for method. Expecting an acts_as_role_restricted object or a roles_mask integer'
24
+ end
18
25
  end
19
26
 
20
27
  def self.roles_collection(obj = nil, user = nil)
@@ -1,3 +1,3 @@
1
1
  module EffectiveRoles
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -90,3 +90,117 @@
90
90
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
91
91
  ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
92
92
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
93
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
94
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
95
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
96
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
97
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
98
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
99
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
100
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
101
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
102
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
103
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
104
+ ActiveRecord::SchemaMigration Load (0.8ms) SELECT "schema_migrations".* FROM "schema_migrations"
105
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
106
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
107
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
108
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
109
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
110
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
111
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
112
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
113
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
114
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
115
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
116
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
117
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
118
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
119
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
120
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
121
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
122
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
123
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
124
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
125
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
126
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
127
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
128
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
129
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
130
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
131
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
132
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
133
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
134
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
135
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
136
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
137
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
138
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
139
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
140
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
141
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
142
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
143
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
144
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
145
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
146
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
147
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
148
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
149
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
150
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
151
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
152
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
153
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
154
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
155
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
156
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
157
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
158
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
159
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
160
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
161
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
162
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
163
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
164
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
165
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
166
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
167
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
168
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
169
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
170
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
171
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
172
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
173
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
174
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
175
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
176
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
177
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
178
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
179
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
180
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
181
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
182
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
183
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
184
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
185
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
186
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
187
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
188
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
189
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
190
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
191
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
192
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
193
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
194
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
195
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
196
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
197
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
198
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
199
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
200
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
201
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
202
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
203
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
204
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
205
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
206
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
@@ -16,16 +16,16 @@ describe EffectiveRoles do
16
16
 
17
17
  describe '#roles_for_roles_mask' do
18
18
  it 'computes the appropriate roles for the given mask' do
19
- EffectiveRoles.roles_for_roles_mask(nil).should eq []
20
- EffectiveRoles.roles_for_roles_mask(0).should eq []
21
- EffectiveRoles.roles_for_roles_mask(1).should eq [:superadmin]
22
- EffectiveRoles.roles_for_roles_mask(2).should eq [:admin]
23
- EffectiveRoles.roles_for_roles_mask(3).should eq [:superadmin, :admin]
24
- EffectiveRoles.roles_for_roles_mask(4).should eq [:member]
25
- EffectiveRoles.roles_for_roles_mask(5).should eq [:superadmin, :member]
26
- EffectiveRoles.roles_for_roles_mask(6).should eq [:admin, :member]
27
- EffectiveRoles.roles_for_roles_mask(7).should eq [:superadmin, :admin, :member]
28
- EffectiveRoles.roles_for_roles_mask(8).should eq []
19
+ EffectiveRoles.roles_for(nil).should eq []
20
+ EffectiveRoles.roles_for(0).should eq []
21
+ EffectiveRoles.roles_for(1).should eq [:superadmin]
22
+ EffectiveRoles.roles_for(2).should eq [:admin]
23
+ EffectiveRoles.roles_for(3).should eq [:superadmin, :admin]
24
+ EffectiveRoles.roles_for(4).should eq [:member]
25
+ EffectiveRoles.roles_for(5).should eq [:superadmin, :member]
26
+ EffectiveRoles.roles_for(6).should eq [:admin, :member]
27
+ EffectiveRoles.roles_for(7).should eq [:superadmin, :admin, :member]
28
+ EffectiveRoles.roles_for(8).should eq []
29
29
  end
30
30
  end
31
31
 
@@ -0,0 +1,192 @@
1
+ describe 'Acts As Role Restricted' do
2
+ let(:roles) { [:superadmin, :admin, :member] }
3
+
4
+ let(:user) { User.new.tap { |user| user.roles = [] } }
5
+ let(:member) { User.new.tap { |user| user.roles = [:member] } }
6
+ let(:admin) { User.new.tap { |user| user.roles = [:admin] } }
7
+ let(:superadmin) { User.new.tap { |user| user.roles = [:superadmin] } }
8
+ let(:member_and_admin) { User.new.tap { |user| user.roles = [:member, :admin] } }
9
+
10
+ before(:each) do
11
+ EffectiveRoles.setup { |config| config.roles = roles }
12
+ end
13
+
14
+ describe '#roles_permit?(obj)' do
15
+ describe 'when subject has no roles' do
16
+ let(:post) { Post.new }
17
+
18
+ it 'should be true when passed nil' do
19
+ post.roles_permit?(nil).should eq true
20
+ end
21
+
22
+ it 'should be true for any user' do
23
+ post.roles_permit?(user).should eq true
24
+ post.roles_permit?(member).should eq true
25
+ post.roles_permit?(admin).should eq true
26
+ post.roles_permit?(superadmin).should eq true
27
+ end
28
+ end
29
+
30
+ describe 'when subject has one role' do
31
+ let(:post) { Post.new.tap { |post| post.roles = [:member] } }
32
+
33
+ it 'should be false when passed nil' do
34
+ post.roles_permit?(nil).should eq false
35
+ end
36
+
37
+ it 'should be false when passed object doesnt share roles' do
38
+ post.roles_permit?(user).should eq false
39
+ post.roles_permit?(admin).should eq false
40
+ post.roles_permit?(superadmin).should eq false
41
+ end
42
+
43
+ it 'should be true for a user with all the same roles' do
44
+ post.roles_permit?(member).should eq true
45
+ post.roles_permit?(member_and_admin).should eq true
46
+ end
47
+ end
48
+
49
+ describe 'when subject has multiple roles' do
50
+ let(:post) { Post.new.tap { |post| post.roles = [:member, :admin] } }
51
+
52
+ it 'should be false when passed nil' do
53
+ post.roles_permit?(nil).should eq false
54
+ end
55
+
56
+ it 'should be false when passed object doesnt share all roles' do
57
+ post.roles_permit?(user).should eq false
58
+ post.roles_permit?(superadmin).should eq false
59
+ end
60
+
61
+ it 'should be true for a user with overlapping roles' do
62
+ post.roles_permit?(member).should eq true
63
+ post.roles_permit?(admin).should eq true
64
+ post.roles_permit?(member_and_admin).should eq true
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#roles_overlap?(obj)' do
70
+ describe 'when subject has no roles' do
71
+ let(:post) { Post.new }
72
+
73
+ it 'should be true when passed nil' do
74
+ post.roles_overlap?(nil).should eq true
75
+ end
76
+
77
+ it 'should be true when user has no roles either' do
78
+ post.roles_overlap?(user).should eq true
79
+ end
80
+
81
+ it 'should be false for any user with roles' do
82
+ post.roles_overlap?(member).should eq false
83
+ post.roles_overlap?(admin).should eq false
84
+ post.roles_overlap?(superadmin).should eq false
85
+ end
86
+ end
87
+
88
+ describe 'when subject has one role' do
89
+ let(:post) { Post.new.tap { |post| post.roles = [:member] } }
90
+
91
+ it 'should be false when passed nil' do
92
+ post.roles_overlap?(nil).should eq false
93
+ end
94
+
95
+ it 'should be false when passed object doesnt share roles' do
96
+ post.roles_overlap?(user).should eq false
97
+ post.roles_overlap?(admin).should eq false
98
+ post.roles_overlap?(superadmin).should eq false
99
+ end
100
+
101
+ it 'should be true for a user with all the same roles' do
102
+ post.roles_overlap?(member).should eq true
103
+ post.roles_overlap?(member_and_admin).should eq true
104
+ end
105
+ end
106
+
107
+ describe 'when subject has multiple roles' do
108
+ let(:post) { Post.new.tap { |post| post.roles = [:member, :admin] } }
109
+
110
+ it 'should be false when passed nil' do
111
+ post.roles_overlap?(nil).should eq false
112
+ end
113
+
114
+ it 'should be false when passed object doesnt share all roles' do
115
+ post.roles_overlap?(user).should eq false
116
+ post.roles_overlap?(superadmin).should eq false
117
+ end
118
+
119
+ it 'should be true for a user with overlapping roles' do
120
+ post.roles_overlap?(member).should eq true
121
+ post.roles_overlap?(admin).should eq true
122
+ post.roles_overlap?(member_and_admin).should eq true
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ describe '#roles_match?(obj)' do
129
+ describe 'when subject has no roles' do
130
+ let(:post) { Post.new }
131
+
132
+ it 'should be true when passed nil' do
133
+ post.roles_match?(nil).should eq true
134
+ end
135
+
136
+ it 'should be true when user has no roles either' do
137
+ post.roles_match?(user).should eq true
138
+ end
139
+
140
+ it 'should be false for any user with roles' do
141
+ post.roles_match?(member).should eq false
142
+ post.roles_match?(admin).should eq false
143
+ post.roles_match?(superadmin).should eq false
144
+ end
145
+ end
146
+
147
+ describe 'when subject has one role' do
148
+ let(:post) { Post.new.tap { |post| post.roles = [:member] } }
149
+
150
+ it 'should be false when passed nil' do
151
+ post.roles_match?(nil).should eq false
152
+ end
153
+
154
+ it 'should be false when passed object doesnt share roles' do
155
+ post.roles_match?(user).should eq false
156
+ post.roles_match?(admin).should eq false
157
+ post.roles_match?(superadmin).should eq false
158
+ end
159
+
160
+ it 'should be true for a user with all the same roles' do
161
+ post.roles_match?(member).should eq true
162
+ end
163
+
164
+ it 'should be false when the user has more roles' do
165
+ post.roles_match?(member_and_admin).should eq false
166
+ end
167
+ end
168
+
169
+ describe 'when subject has multiple roles' do
170
+ let(:post) { Post.new.tap { |post| post.roles = [:member, :admin] } }
171
+
172
+ it 'should be false when passed nil' do
173
+ post.roles_match?(nil).should eq false
174
+ end
175
+
176
+ it 'should be false when passed object doesnt share all roles' do
177
+ post.roles_match?(user).should eq false
178
+ post.roles_match?(superadmin).should eq false
179
+ post.roles_match?(member).should eq false
180
+ post.roles_match?(admin).should eq false
181
+ end
182
+
183
+ it 'should be true for a user with same roles' do
184
+ post.roles_match?(member_and_admin).should eq true
185
+ end
186
+ end
187
+ end
188
+
189
+
190
+
191
+
192
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_roles
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-23 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -200,6 +200,7 @@ files:
200
200
  - spec/dummy/db/test.sqlite3
201
201
  - spec/dummy/log/test.log
202
202
  - spec/effective_roles_spec.rb
203
+ - spec/models/acts_as_role_restricted_spec.rb
203
204
  - spec/spec_helper.rb
204
205
  - spec/support/factories.rb
205
206
  homepage: https://github.com/code-and-effect/effective_roles
@@ -257,5 +258,6 @@ test_files:
257
258
  - spec/dummy/Rakefile
258
259
  - spec/dummy/README.rdoc
259
260
  - spec/effective_roles_spec.rb
261
+ - spec/models/acts_as_role_restricted_spec.rb
260
262
  - spec/spec_helper.rb
261
263
  - spec/support/factories.rb