ktopping_acl9 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'acl9')
4
+
5
+ module SomeHelper
6
+ include Acl9Helpers
7
+
8
+ access_control :the_question do
9
+ allow :hamlet, :to => :be
10
+ allow :hamlet, :except => :be
11
+ end
12
+ end
13
+
14
+ class HelperTest < Test::Unit::TestCase
15
+ module Hamlet
16
+ def current_user
17
+ user = Object.new
18
+
19
+ class <<user
20
+ def has_role?(role, object=nil)
21
+ if object
22
+ return (role == 'hamlet' && object.name == 'castle')
23
+ else
24
+ return role == 'hamlet'
25
+ end
26
+ end
27
+ end
28
+
29
+ user
30
+ end
31
+ end
32
+
33
+ module NotLoggedIn
34
+ def current_user; nil end
35
+ end
36
+
37
+ module Noone
38
+ def current_user
39
+ user = Object.new
40
+
41
+ class <<user
42
+ def has_role?(*_); false end
43
+ end
44
+
45
+ user
46
+ end
47
+ end
48
+
49
+ class Base
50
+ include SomeHelper
51
+
52
+ attr_accessor :action_name
53
+ def controller
54
+ self
55
+ end
56
+ end
57
+
58
+ class Klass1 < Base
59
+ include Hamlet
60
+ end
61
+
62
+ class Klass2 < Base
63
+ include NotLoggedIn
64
+ end
65
+
66
+ class Klass3 < Base
67
+ include Noone
68
+ end
69
+
70
+ it "has :the_question method" do
71
+ Base.new.should respond_to(:the_question)
72
+ end
73
+
74
+ it "role :hamlet is allowed to be" do
75
+ k = Klass1.new
76
+ k.action_name = 'be'
77
+ k.the_question.should be_true
78
+ end
79
+
80
+ it "role :hamlet is allowed to not_be" do
81
+ k = Klass1.new
82
+ k.action_name = 'not_be'
83
+ k.the_question.should be_true
84
+ end
85
+
86
+ it "not logged in is not allowed to be" do
87
+ k = Klass2.new
88
+ k.action_name = 'be'
89
+ k.the_question.should == false
90
+ end
91
+
92
+ it "noone is not allowed to be" do
93
+ k = Klass3.new
94
+ k.action_name = 'be'
95
+ k.the_question.should == false
96
+ end
97
+
98
+ it "has :show_to method" do
99
+ Base.new.should respond_to(:show_to)
100
+ end
101
+
102
+ it "has :show_to hamlet 'hello hamlet' message" do
103
+ k = Klass1.new
104
+ message = 'hello hamlet'
105
+ k.show_to('hamlet') { message }.should == message
106
+ end
107
+
108
+ it "has to show message if user has hamlet role on object" do
109
+ k = Klass1.new
110
+ message = 'hello hamlet'
111
+
112
+ obj = Object.new
113
+ def obj.name; 'castle'; end
114
+
115
+ k.show_to('hamlet', :of => obj) { message }.should == message
116
+ end
117
+
118
+ it "has not to show message if user has no hamlet role on object" do
119
+ k = Klass1.new
120
+
121
+ obj = Object.new
122
+ def obj.name; 'persia'; end
123
+
124
+ k.show_to('hamlet', :of => obj) { 'hello my prince' }.should == ''
125
+ end
126
+
127
+ it "has :show_to nothing to NotLoggedIn" do
128
+ k = Klass2.new
129
+ k.action_name = 'be'
130
+ message = 'hello hamlet'
131
+ k.show_to(:hamlet) { message }.should == ''
132
+ end
133
+
134
+ end
@@ -0,0 +1,355 @@
1
+ require 'test_helper'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'acl9')
3
+ require 'support/models'
4
+
5
+ #Logger = ActiveRecord::Base.logger
6
+ load 'support/schema.rb'
7
+
8
+ class RolesTest < Test::Unit::TestCase
9
+ before do
10
+ Role.destroy_all
11
+ [User, Foo, Bar].each { |model| model.delete_all }
12
+
13
+ @user = User.create!
14
+ @user2 = User.create!
15
+ @foo = Foo.create!
16
+ @bar = Bar.create!
17
+ #create authorized object that has a string primary key
18
+ @uuid = Uuid.new
19
+ @uuid.uuid = "C41642EE-2780-0001-189F-17F3101B26E0"
20
+ @uuid.save
21
+ end
22
+
23
+ it "should not have any roles by default" do
24
+ %w(user manager admin owner).each do |role|
25
+ @user.has_role?(role).should be_false
26
+ end
27
+ end
28
+
29
+ it "#has_role! without object (global role)" do
30
+ lambda do
31
+ @user.has_role!('admin')
32
+ end.should change { Role.count }.from(0).to(1)
33
+
34
+ @user.has_role?('admin').should be_true
35
+ @user2.has_role?('admin').should be_false
36
+ end
37
+
38
+ it "should not count global role as object role" do
39
+ @user.has_role!('admin')
40
+
41
+ [@foo, @bar, Foo, Bar, @user].each do |obj|
42
+ @user.has_role?('admin', obj).should be_false
43
+ @user.has_roles_for?(obj).should be_false
44
+ @user.roles_for(obj).should == []
45
+ end
46
+
47
+ [@foo, @bar].each do |obj|
48
+ obj.accepts_role?('admin', @user).should be_false
49
+ end
50
+ end
51
+
52
+ it "#has_role! with object (object role)" do
53
+ @user.has_role!('manager', @foo)
54
+
55
+ @user.has_role?('manager', @foo).should be_true
56
+ @user.has_roles_for?(@foo).should be_true
57
+ @user.has_role_for?(@foo).should be_true
58
+
59
+ roles = @user.roles_for(@foo)
60
+ roles.should == @foo.accepted_roles_by(@user)
61
+ roles.size.should == 1
62
+ roles.first.name.should == "manager"
63
+
64
+ @user.has_role?('manager', @bar).should be_false
65
+ @user2.has_role?('manager', @foo).should be_false
66
+
67
+ @foo.accepts_role?('manager', @user).should be_true
68
+ @foo.accepts_role_by?(@user).should be_true
69
+ @foo.accepts_roles_by?(@user).should be_true
70
+ end
71
+
72
+ it "should count object role also as global role" do
73
+ @user.has_role!('manager', @foo)
74
+
75
+ @user.has_role?('manager').should be_true
76
+ end
77
+
78
+ it "should not count object role as object class role" do
79
+ @user.has_role!('manager', @foo)
80
+ @user.has_role?('manager', Foo).should be_false
81
+ end
82
+
83
+ context "protect_global_roles is true" do
84
+ before do
85
+ @saved_option = Acl9.config[:protect_global_roles]
86
+ Acl9.config[:protect_global_roles] = true
87
+ end
88
+
89
+ it "should not count object role also as global role" do
90
+ @user.has_role!('manager', @foo)
91
+
92
+ @user.has_role?('manager').should be_false
93
+ end
94
+
95
+ after do
96
+ Acl9.config[:protect_global_roles] = @saved_option
97
+ end
98
+ end
99
+
100
+ it "#has_role! with class" do
101
+ @user.has_role!('user', Bar)
102
+
103
+ @user.has_role?('user', Bar).should be_true
104
+ @user.has_roles_for?(Bar).should be_true
105
+ @user.has_role_for?(Bar).should be_true
106
+
107
+ roles = @user.roles_for(Bar)
108
+ roles.size.should == 1
109
+ roles.first.name.should == "user"
110
+
111
+ @user.has_role?('user', Foo).should be_false
112
+ @user2.has_role?('user', Bar).should be_false
113
+ end
114
+
115
+ it "should not count class role as object role" do
116
+ @user.has_role!('manager', Foo)
117
+ @user.has_role?('manager', @foo).should be_false
118
+ end
119
+
120
+ it "should be able to have several roles on the same object" do
121
+ @user.has_role!('manager', @foo)
122
+ @user.has_role!('user', @foo)
123
+ @user.has_role!('admin', @foo)
124
+
125
+ @user.has_role!('owner', @bar)
126
+
127
+ @user.roles_for(@foo) .map(&:name).sort.should == %w(admin manager user)
128
+ @foo.accepted_roles_by(@user).map(&:name).sort.should == %w(admin manager user)
129
+ end
130
+
131
+ it "should reuse existing roles" do
132
+ @user.has_role!('owner', @bar)
133
+ @user2.has_role!('owner', @bar)
134
+
135
+ @user.role_objects.should == @user2.role_objects
136
+ end
137
+
138
+ it "#has_no_role! should unassign a global role from user" do
139
+ set_some_roles
140
+
141
+ lambda do
142
+ @user.has_no_role!('3133t')
143
+ end.should change { @user.role_objects.count }.by(-1)
144
+
145
+ @user.has_role?('3133t').should be_false
146
+ end
147
+
148
+ it "#has_no_role! should unassign an object role from user" do
149
+ set_some_roles
150
+
151
+ lambda do
152
+ @user.has_no_role!('manager', @foo)
153
+ end.should change { @user.role_objects.count }.by(-1)
154
+
155
+ @user.has_role?('manager', @foo).should be_false
156
+ @user.has_role?('user', @foo).should be_true # another role on the same object
157
+ end
158
+
159
+ it "#has_no_role! should unassign a class role from user" do
160
+ set_some_roles
161
+
162
+ lambda do
163
+ @user.has_no_role!('admin', Foo)
164
+ end.should change { @user.role_objects.count }.by(-1)
165
+
166
+ @user.has_role?('admin', Foo).should be_false
167
+ @user.has_role?('admin').should be_true # global role
168
+ end
169
+
170
+ it "#has_no_roles_for! should unassign global and class roles with nil object" do
171
+ set_some_roles
172
+
173
+ lambda do
174
+ @user.has_no_roles_for!
175
+ end.should change { @user.role_objects.count }.by(-4)
176
+
177
+ @user.has_role?('admin').should be_false
178
+ @user.has_role?('3133t').should be_false
179
+ @user.has_role?('admin', Foo).should be_false
180
+ @user.has_role?('manager', Foo).should be_false
181
+ end
182
+
183
+ it "#has_no_roles_for! should unassign object roles" do
184
+ set_some_roles
185
+
186
+ lambda do
187
+ @user.has_no_roles_for! @foo
188
+ end.should change { @user.role_objects.count }.by(-2)
189
+
190
+ @user.has_role?('user', @foo).should be_false
191
+ @user.has_role?('manager', @foo).should be_false
192
+ end
193
+
194
+ it "#has_no_roles_for! should unassign both class roles and object roles for objects of that class" do
195
+ set_some_roles
196
+
197
+ lambda do
198
+ @user.has_no_roles_for! Foo
199
+ end.should change { @user.role_objects.count }.by(-4)
200
+
201
+ @user.has_role?('admin', Foo).should be_false
202
+ @user.has_role?('manager', Foo).should be_false
203
+ @user.has_role?('user', @foo).should be_false
204
+ @user.has_role?('manager', @foo).should be_false
205
+ end
206
+
207
+ it "#has_no_roles! should unassign all roles" do
208
+ set_some_roles
209
+
210
+ @user.has_no_roles!
211
+ @user.role_objects.count.should == 0
212
+ end
213
+
214
+ it "should delete unused roles from table" do
215
+ @user.has_role!('owner', @bar)
216
+ @user2.has_role!('owner', @bar)
217
+
218
+ Role.count.should == 1
219
+
220
+ @bar.accepts_no_role!('owner', @user2)
221
+ Role.count.should == 1
222
+
223
+ @bar.accepts_no_role!('owner', @user)
224
+
225
+ Role.count.should == 0
226
+ end
227
+
228
+ it "should be able to get users that have a role on a authorized object" do
229
+ @user.has_role!('owner', @bar)
230
+ @user2.has_role!('owner', @bar)
231
+
232
+ @bar.users.count.should == 2
233
+ end
234
+
235
+ it "should be able to get users that have a role on a authorized object with text primary key" do
236
+ @user.has_role!('owner', @uuid)
237
+ @user2.has_role!('owner', @uuid)
238
+
239
+ @uuid.users.count.should == 2
240
+ end
241
+
242
+ it "should accept :symbols as role names" do
243
+ @user.has_role! :admin
244
+ @user.has_role! :_3133t
245
+
246
+ @user.has_role! :admin, Foo
247
+ @user.has_role! :manager, Foo
248
+ @user.has_role! :user, @foo
249
+ @foo.accepts_role! :manager, @user
250
+ @bar.accepts_role! :owner, @user
251
+
252
+ @user.has_role?(:admin).should be_true
253
+ @user.has_role?(:_3133t).should be_true
254
+ @user.has_role?(:admin, Foo).should be_true
255
+ @user.has_role?(:manager, @foo).should be_true
256
+ end
257
+
258
+ private
259
+
260
+ def set_some_roles
261
+ @user.has_role!('admin')
262
+ @user.has_role!('3133t')
263
+
264
+ @user.has_role!('admin', Foo)
265
+ @user.has_role!('manager', Foo)
266
+ @user.has_role!('user', @foo)
267
+ @foo.accepts_role!('manager', @user)
268
+ @bar.accepts_role!('owner', @user)
269
+ end
270
+ end
271
+
272
+ class RolesWithCustomClassNamesTest < Test::Unit::TestCase
273
+ before do
274
+ AnotherRole.destroy_all
275
+ [AnotherSubject, FooBar].each { |model| model.delete_all }
276
+
277
+ @subj = AnotherSubject.create!
278
+ @subj2 = AnotherSubject.create!
279
+ @foobar = FooBar.create!
280
+ end
281
+
282
+ it "should basically work" do
283
+ lambda do
284
+ @subj.has_role!('admin')
285
+ @subj.has_role!('user', @foobar)
286
+ end.should change { AnotherRole.count }.from(0).to(2)
287
+
288
+ @subj.has_role?('admin').should be_true
289
+ @subj2.has_role?('admin').should be_false
290
+
291
+ @subj.has_role?(:user, @foobar).should be_true
292
+ @subj2.has_role?(:user, @foobar).should be_false
293
+
294
+ @subj.has_no_roles!
295
+ @subj2.has_no_roles!
296
+ end
297
+ end
298
+
299
+ class RolesWithCustomAssociationNamesTest < Test::Unit::TestCase
300
+ before do
301
+ DifferentAssociationNameRole.destroy_all
302
+ [DifferentAssociationNameSubject, FooBar].each { |model| model.delete_all }
303
+
304
+ @subj = DifferentAssociationNameSubject.create!
305
+ @subj2 = DifferentAssociationNameSubject.create!
306
+ @foobar = FooBar.create!
307
+ end
308
+
309
+ it "should basically work" do
310
+ lambda do
311
+ @subj.has_role!('admin')
312
+ @subj.has_role!('user', @foobar)
313
+ end.should change { DifferentAssociationNameRole.count }.from(0).to(2)
314
+
315
+ @subj.has_role?('admin').should be_true
316
+ @subj2.has_role?('admin').should be_false
317
+
318
+ @subj.has_role?(:user, @foobar).should be_true
319
+ @subj2.has_role?(:user, @foobar).should be_false
320
+
321
+ @subj.has_no_roles!
322
+ @subj2.has_no_roles!
323
+ end
324
+ end
325
+
326
+ class UsersRolesAndSubjectsWithNamespacedClassNamesTest < Test::Unit::TestCase
327
+ before do
328
+ Other::Role.destroy_all
329
+ [Other::User, Other::FooBar].each { |model| model.delete_all }
330
+
331
+ @user = Other::User.create!
332
+ @user2 = Other::User.create!
333
+ @foobar = Other::FooBar.create!
334
+
335
+ end
336
+
337
+ it "should basically work" do
338
+ lambda do
339
+ @user.has_role!('admin')
340
+ @user.has_role!('user', @foobar)
341
+ end.should change { Other::Role.count }.from(0).to(2)
342
+
343
+ @user.has_role?('admin').should be_true
344
+ @user2.has_role?('admin').should be_false
345
+
346
+ @user.has_role?(:user, @foobar).should be_true
347
+ @user2.has_role?(:user, @foobar).should be_false
348
+
349
+ @foobar.accepted_roles.count.should == 1
350
+
351
+ @user.has_no_roles!
352
+ @user2.has_no_roles!
353
+ end
354
+ end
355
+
@@ -0,0 +1,207 @@
1
+ class ApplicationController < ActionController::Base
2
+ rescue_from Acl9::AccessDenied do |e|
3
+ render :text => 'AccessDenied'
4
+ end
5
+ end
6
+
7
+ class EmptyController < ApplicationController
8
+ attr_accessor :current_user
9
+ before_filter :set_current_user
10
+
11
+ [:index, :show, :new, :edit, :update, :delete, :destroy].each do |act|
12
+ define_method(act) { render :text => 'OK' }
13
+ end
14
+
15
+ private
16
+
17
+ def set_current_user
18
+ if params[:user]
19
+ self.current_user = params[:user]
20
+ end
21
+ end
22
+ end
23
+
24
+ module TrueFalse
25
+ private
26
+
27
+ def true_meth; true end
28
+ def false_meth; false end
29
+ end
30
+
31
+ # all these controllers behave the same way
32
+
33
+ class ACLBlock < EmptyController
34
+ access_control :debug => true do
35
+ allow all, :to => [:index, :show]
36
+ allow :admin
37
+ end
38
+ end
39
+
40
+ class ACLMethod < EmptyController
41
+ access_control :as_method => :acl do
42
+ allow all, :to => [:index, :show]
43
+ allow :admin, :except => [:index, :show]
44
+ end
45
+ end
46
+
47
+ class ACLMethod2 < EmptyController
48
+ access_control :acl do
49
+ allow all, :to => [:index, :show]
50
+ allow :admin, :except => [:index, :show]
51
+ end
52
+ end
53
+
54
+ class ACLArguments < EmptyController
55
+ access_control :except => [:index, :show] do
56
+ allow :admin, :if => :true_meth, :unless => :false_meth
57
+ end
58
+
59
+ include TrueFalse
60
+ end
61
+
62
+ class ACLBooleanMethod < EmptyController
63
+ access_control :acl, :filter => false do
64
+ allow all, :to => [:index, :show], :if => :true_meth
65
+ allow :admin, :unless => :false_meth
66
+ allow all, :if => :false_meth
67
+ allow all, :unless => :true_meth
68
+ end
69
+
70
+ before_filter :check_acl
71
+
72
+ def check_acl
73
+ if self.acl
74
+ true
75
+ else
76
+ raise Acl9::AccessDenied
77
+ end
78
+ end
79
+
80
+ include TrueFalse
81
+ end
82
+
83
+ ###########################################
84
+ class MyDearFoo
85
+ include Singleton
86
+ end
87
+
88
+ class ACLIvars < EmptyController
89
+ class VenerableBar; end
90
+
91
+ before_filter :set_ivars
92
+
93
+ access_control do
94
+ action :destroy do
95
+ allow :owner, :of => :foo
96
+ allow :bartender, :at => VenerableBar
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def set_ivars
103
+ @foo = MyDearFoo.instance
104
+ end
105
+ end
106
+
107
+ class ACLSubjectMethod < ApplicationController
108
+ access_control :subject_method => :the_only_user do
109
+ allow :the_only_one
110
+ end
111
+
112
+ def index
113
+ render :text => 'OK'
114
+ end
115
+
116
+ private
117
+
118
+ def the_only_user
119
+ params[:user]
120
+ end
121
+ end
122
+
123
+ class ACLObjectsHash < ApplicationController
124
+ access_control :allowed?, :filter => false do
125
+ allow :owner, :of => :foo
126
+ end
127
+
128
+ def allow
129
+ @foo = nil
130
+ render :text => (allowed?(:foo => MyDearFoo.instance) ? 'OK' : 'AccessDenied')
131
+ end
132
+
133
+ def current_user
134
+ params[:user]
135
+ end
136
+ end
137
+
138
+ class ACLActionOverride < ApplicationController
139
+ access_control :allowed?, :filter => false do
140
+ allow all, :to => :index
141
+ deny all, :to => :show
142
+ allow :owner, :of => :foo, :to => :edit
143
+ end
144
+
145
+ def check_allow
146
+ render :text => (allowed?(params[:_action]) ? 'OK' : 'AccessDenied')
147
+ end
148
+
149
+ def check_allow_with_foo
150
+ render :text => (allowed?(params[:_action], :foo => MyDearFoo.instance) ? 'OK' : 'AccessDenied')
151
+ end
152
+
153
+ def current_user
154
+ params[:user]
155
+ end
156
+ end
157
+
158
+
159
+ class ACLHelperMethod < ApplicationController
160
+ access_control :helper => :foo? do
161
+ allow :owner, :of => :foo
162
+ end
163
+
164
+ def allow
165
+ @foo = MyDearFoo.instance
166
+
167
+ render :inline => "<%= foo? ? 'OK' : 'AccessDenied' %>"
168
+ end
169
+
170
+ def current_user
171
+ params[:user]
172
+ end
173
+ end
174
+
175
+ class ACLQueryMethod < ApplicationController
176
+ attr_accessor :current_user
177
+
178
+ access_control :acl, :query_method => true do
179
+ allow :editor, :to => [:edit, :update, :destroy]
180
+ allow :viewer, :to => [:index, :show]
181
+ allow :owner, :of => :foo, :to => :fooize
182
+ end
183
+ end
184
+
185
+ class ACLQueryMethodWithLambda < ApplicationController
186
+ attr_accessor :current_user
187
+
188
+ access_control :query_method => :acl? do
189
+ allow :editor, :to => [:edit, :update, :destroy]
190
+ allow :viewer, :to => [:index, :show]
191
+ allow :owner, :of => :foo, :to => :fooize
192
+ end
193
+ end
194
+
195
+ class ACLNamedQueryMethod < ApplicationController
196
+ attr_accessor :current_user
197
+
198
+ access_control :acl, :query_method => 'allow_ay' do
199
+ allow :editor, :to => [:edit, :update, :destroy]
200
+ allow :viewer, :to => [:index, :show]
201
+ allow :owner, :of => :foo, :to => :fooize
202
+ end
203
+
204
+ def acl?(*args)
205
+ allow_ay(*args)
206
+ end
207
+ end