acl9 0.12.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +21 -7
- data/.travis.yml +19 -0
- data/Appraisals +8 -0
- data/CONTRIBUTING.md +58 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +88 -32
- data/LICENSE +9 -0
- data/README.md +156 -0
- data/Rakefile +6 -3
- data/acl9.gemspec +10 -13
- data/gemfiles/rails_4.0.gemfile +8 -0
- data/gemfiles/rails_4.1.gemfile +8 -0
- data/lib/acl9/model_extensions/for_subject.rb +5 -1
- data/lib/acl9/model_extensions.rb +3 -24
- data/lib/acl9/version.rb +1 -1
- data/lib/acl9.rb +1 -1
- data/test/controller_extensions/actions_test.rb +167 -0
- data/test/controller_extensions/anon_test.rb +39 -0
- data/test/controller_extensions/base.rb +96 -0
- data/test/controller_extensions/basics_test.rb +44 -0
- data/test/controller_extensions/conditions_test.rb +48 -0
- data/test/controller_extensions/method_test.rb +50 -0
- data/test/controller_extensions/multi_match_test.rb +142 -0
- data/test/controller_extensions/multiple_role_arguments_test.rb +135 -0
- data/test/controller_extensions/prepositions_test.rb +99 -0
- data/test/controller_extensions/pseudo_role_test.rb +26 -0
- data/test/controller_extensions/role_test.rb +75 -0
- data/test/controllers/acl_action_override_test.rb +24 -0
- data/test/controllers/acl_arguments_test.rb +5 -0
- data/test/controllers/acl_block_test.rb +5 -0
- data/test/controllers/acl_boolean_method_test.rb +5 -0
- data/test/controllers/acl_helper_method_test.rb +26 -0
- data/test/controllers/acl_ivars_test.rb +15 -0
- data/test/controllers/acl_method2_test.rb +6 -0
- data/test/controllers/acl_method_test.rb +6 -0
- data/test/controllers/acl_object_hash_test.rb +18 -0
- data/test/controllers/acl_query_method_named_test.rb +9 -0
- data/test/controllers/acl_query_method_test.rb +9 -0
- data/test/controllers/acl_query_method_with_lambda_test.rb +9 -0
- data/test/controllers/acl_query_mixin.rb +51 -0
- data/test/controllers/acl_subject_method_test.rb +15 -0
- data/test/controllers/arguments_checking_test.rb +43 -0
- data/test/dummy/app/controllers/acl_action_override.rb +15 -0
- data/test/dummy/app/controllers/acl_arguments.rb +10 -0
- data/test/dummy/app/controllers/acl_block.rb +6 -0
- data/test/dummy/app/controllers/acl_boolean_method.rb +23 -0
- data/test/dummy/app/controllers/acl_helper_method.rb +11 -0
- data/test/dummy/app/controllers/acl_ivars.rb +17 -0
- data/test/dummy/app/controllers/acl_method.rb +6 -0
- data/test/dummy/app/controllers/acl_method2.rb +6 -0
- data/test/dummy/app/controllers/acl_objects_hash.rb +10 -0
- data/test/dummy/app/controllers/acl_query_method.rb +9 -0
- data/test/dummy/app/controllers/acl_query_method_named.rb +13 -0
- data/test/dummy/app/controllers/acl_query_method_with_lambda.rb +9 -0
- data/test/dummy/app/controllers/acl_subject_method.rb +16 -0
- data/test/dummy/app/controllers/application_controller.rb +7 -0
- data/test/dummy/app/controllers/empty_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/some_helper.rb +8 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/access.rb +3 -0
- data/test/dummy/app/models/account.rb +3 -0
- data/test/dummy/app/models/bar.rb +3 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/models/foo.rb +3 -0
- data/test/dummy/app/models/foo_bar.rb +3 -0
- data/test/dummy/app/models/other/foo.rb +5 -0
- data/test/dummy/app/models/other/role.rb +5 -0
- data/test/dummy/app/models/other/user.rb +5 -0
- data/test/dummy/app/models/role.rb +3 -0
- data/test/dummy/app/models/user.rb +3 -0
- data/test/dummy/app/models/uuid.rb +4 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +4 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +78 -0
- data/test/dummy/config/environments/test.rb +39 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/secrets.rb +1 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20141117132218_create_tables.rb +102 -0
- data/test/helpers/helper_test.rb +89 -0
- data/test/models/roles_test.rb +251 -0
- data/test/models/roles_with_custom_association_names_test.rb +28 -0
- data/test/models/roles_with_custom_class_names_test.rb +28 -0
- data/test/models/system_roles_test.rb +16 -0
- data/test/models/users_roles_and_subjects_with_namespaced_class_names_test.rb +30 -0
- data/test/test_helper.rb +76 -23
- data/test/version_test.rb +2 -2
- metadata +190 -74
- data/README.textile +0 -921
- data/VERSION.yml +0 -5
- data/init.rb +0 -1
- data/test/access_control_test.rb +0 -338
- data/test/dsl_base_test.rb +0 -795
- data/test/helpers_test.rb +0 -133
- data/test/roles_test.rb +0 -370
- data/test/support/controllers.rb +0 -207
- data/test/support/models.rb +0 -59
- data/test/support/schema.rb +0 -93
data/test/helpers_test.rb
DELETED
@@ -1,133 +0,0 @@
|
|
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 == nil
|
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 == nil
|
132
|
-
end
|
133
|
-
end
|
data/test/roles_test.rb
DELETED
@@ -1,370 +0,0 @@
|
|
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
|
-
|
9
|
-
class SystemRolesTest < Test::Unit::TestCase
|
10
|
-
it "should not delete a system role" do
|
11
|
-
Role.destroy_all
|
12
|
-
@role=Role.create(:name=>"admin", :system=>true)
|
13
|
-
@role.system.should be_true
|
14
|
-
Role.count.should==1
|
15
|
-
@user = User.create!
|
16
|
-
@user.has_role!(:admin)
|
17
|
-
Role.count.should==1
|
18
|
-
@user.has_no_role!(:admin)
|
19
|
-
Role.count.should==1
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class RolesTest < Test::Unit::TestCase
|
24
|
-
before do
|
25
|
-
Role.destroy_all
|
26
|
-
[User, Foo, Bar].each { |model| model.delete_all }
|
27
|
-
|
28
|
-
@user = User.create!
|
29
|
-
@user2 = User.create!
|
30
|
-
@foo = Foo.create!
|
31
|
-
@bar = Bar.create!
|
32
|
-
#create authorized object that has a string primary key
|
33
|
-
@uuid = Uuid.new
|
34
|
-
@uuid.uuid = "C41642EE-2780-0001-189F-17F3101B26E0"
|
35
|
-
@uuid.save
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should not have any roles by default" do
|
39
|
-
%w(user manager admin owner).each do |role|
|
40
|
-
@user.has_role?(role).should be_false
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "#has_role! without object (global role)" do
|
45
|
-
lambda do
|
46
|
-
@user.has_role!('admin')
|
47
|
-
end.should change { Role.count }.from(0).to(1)
|
48
|
-
|
49
|
-
@user.has_role?('admin').should be_true
|
50
|
-
@user2.has_role?('admin').should be_false
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should not count global role as object role" do
|
54
|
-
@user.has_role!('admin')
|
55
|
-
|
56
|
-
[@foo, @bar, Foo, Bar, @user].each do |obj|
|
57
|
-
@user.has_role?('admin', obj).should be_false
|
58
|
-
@user.has_roles_for?(obj).should be_false
|
59
|
-
@user.roles_for(obj).should == []
|
60
|
-
end
|
61
|
-
|
62
|
-
[@foo, @bar].each do |obj|
|
63
|
-
obj.accepts_role?('admin', @user).should be_false
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
it "#has_role! with object (object role)" do
|
68
|
-
@user.has_role!('manager', @foo)
|
69
|
-
|
70
|
-
@user.has_role?('manager', @foo).should be_true
|
71
|
-
@user.has_roles_for?(@foo).should be_true
|
72
|
-
@user.has_role_for?(@foo).should be_true
|
73
|
-
|
74
|
-
roles = @user.roles_for(@foo)
|
75
|
-
roles.should == @foo.accepted_roles_by(@user)
|
76
|
-
roles.size.should == 1
|
77
|
-
roles.first.name.should == "manager"
|
78
|
-
|
79
|
-
@user.has_role?('manager', @bar).should be_false
|
80
|
-
@user2.has_role?('manager', @foo).should be_false
|
81
|
-
|
82
|
-
@foo.accepts_role?('manager', @user).should be_true
|
83
|
-
@foo.accepts_role_by?(@user).should be_true
|
84
|
-
@foo.accepts_roles_by?(@user).should be_true
|
85
|
-
end
|
86
|
-
|
87
|
-
it "should count object role also as global role" do
|
88
|
-
@user.has_role!('manager', @foo)
|
89
|
-
|
90
|
-
@user.has_role?('manager').should be_true
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should not count object role as object class role" do
|
94
|
-
@user.has_role!('manager', @foo)
|
95
|
-
@user.has_role?('manager', Foo).should be_false
|
96
|
-
end
|
97
|
-
|
98
|
-
context "protect_global_roles is true" do
|
99
|
-
before do
|
100
|
-
@saved_option = Acl9.config[:protect_global_roles]
|
101
|
-
Acl9.config[:protect_global_roles] = true
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should not count object role also as global role" do
|
105
|
-
@user.has_role!('manager', @foo)
|
106
|
-
|
107
|
-
@user.has_role?('manager').should be_false
|
108
|
-
end
|
109
|
-
|
110
|
-
after do
|
111
|
-
Acl9.config[:protect_global_roles] = @saved_option
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
it "#has_role! with class" do
|
116
|
-
@user.has_role!('user', Bar)
|
117
|
-
|
118
|
-
@user.has_role?('user', Bar).should be_true
|
119
|
-
@user.has_roles_for?(Bar).should be_true
|
120
|
-
@user.has_role_for?(Bar).should be_true
|
121
|
-
|
122
|
-
roles = @user.roles_for(Bar)
|
123
|
-
roles.size.should == 1
|
124
|
-
roles.first.name.should == "user"
|
125
|
-
|
126
|
-
@user.has_role?('user', Foo).should be_false
|
127
|
-
@user2.has_role?('user', Bar).should be_false
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should not count class role as object role" do
|
131
|
-
@user.has_role!('manager', Foo)
|
132
|
-
@user.has_role?('manager', @foo).should be_false
|
133
|
-
end
|
134
|
-
|
135
|
-
it "should be able to have several roles on the same object" do
|
136
|
-
@user.has_role!('manager', @foo)
|
137
|
-
@user.has_role!('user', @foo)
|
138
|
-
@user.has_role!('admin', @foo)
|
139
|
-
|
140
|
-
@user.has_role!('owner', @bar)
|
141
|
-
|
142
|
-
@user.roles_for(@foo) .map(&:name).sort.should == %w(admin manager user)
|
143
|
-
@foo.accepted_roles_by(@user).map(&:name).sort.should == %w(admin manager user)
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should reuse existing roles" do
|
147
|
-
@user.has_role!('owner', @bar)
|
148
|
-
@user2.has_role!('owner', @bar)
|
149
|
-
|
150
|
-
@user.role_objects.should == @user2.role_objects
|
151
|
-
end
|
152
|
-
|
153
|
-
it "#has_no_role! should unassign a global role from user" do
|
154
|
-
set_some_roles
|
155
|
-
|
156
|
-
lambda do
|
157
|
-
@user.has_no_role!('3133t')
|
158
|
-
end.should change { @user.role_objects.count }.by(-1)
|
159
|
-
|
160
|
-
@user.has_role?('3133t').should be_false
|
161
|
-
end
|
162
|
-
|
163
|
-
it "#has_no_role! should unassign an object role from user" do
|
164
|
-
set_some_roles
|
165
|
-
|
166
|
-
lambda do
|
167
|
-
@user.has_no_role!('manager', @foo)
|
168
|
-
end.should change { @user.role_objects.count }.by(-1)
|
169
|
-
|
170
|
-
@user.has_role?('manager', @foo).should be_false
|
171
|
-
@user.has_role?('user', @foo).should be_true # another role on the same object
|
172
|
-
end
|
173
|
-
|
174
|
-
it "#has_no_role! should unassign a class role from user" do
|
175
|
-
set_some_roles
|
176
|
-
|
177
|
-
lambda do
|
178
|
-
@user.has_no_role!('admin', Foo)
|
179
|
-
end.should change { @user.role_objects.count }.by(-1)
|
180
|
-
|
181
|
-
@user.has_role?('admin', Foo).should be_false
|
182
|
-
@user.has_role?('admin').should be_true # global role
|
183
|
-
end
|
184
|
-
|
185
|
-
it "#has_no_roles_for! should unassign global and class roles with nil object" do
|
186
|
-
set_some_roles
|
187
|
-
|
188
|
-
lambda do
|
189
|
-
@user.has_no_roles_for!
|
190
|
-
end.should change { @user.role_objects.count }.by(-4)
|
191
|
-
|
192
|
-
@user.has_role?('admin').should be_false
|
193
|
-
@user.has_role?('3133t').should be_false
|
194
|
-
@user.has_role?('admin', Foo).should be_false
|
195
|
-
@user.has_role?('manager', Foo).should be_false
|
196
|
-
end
|
197
|
-
|
198
|
-
it "#has_no_roles_for! should unassign object roles" do
|
199
|
-
set_some_roles
|
200
|
-
|
201
|
-
lambda do
|
202
|
-
@user.has_no_roles_for! @foo
|
203
|
-
end.should change { @user.role_objects.count }.by(-2)
|
204
|
-
|
205
|
-
@user.has_role?('user', @foo).should be_false
|
206
|
-
@user.has_role?('manager', @foo).should be_false
|
207
|
-
end
|
208
|
-
|
209
|
-
it "#has_no_roles_for! should unassign both class roles and object roles for objects of that class" do
|
210
|
-
set_some_roles
|
211
|
-
|
212
|
-
lambda do
|
213
|
-
@user.has_no_roles_for! Foo
|
214
|
-
end.should change { @user.role_objects.count }.by(-4)
|
215
|
-
|
216
|
-
@user.has_role?('admin', Foo).should be_false
|
217
|
-
@user.has_role?('manager', Foo).should be_false
|
218
|
-
@user.has_role?('user', @foo).should be_false
|
219
|
-
@user.has_role?('manager', @foo).should be_false
|
220
|
-
end
|
221
|
-
|
222
|
-
it "#has_no_roles! should unassign all roles" do
|
223
|
-
set_some_roles
|
224
|
-
|
225
|
-
@user.has_no_roles!
|
226
|
-
@user.role_objects.count.should == 0
|
227
|
-
end
|
228
|
-
|
229
|
-
it "should delete unused roles from table" do
|
230
|
-
@user.has_role!('owner', @bar)
|
231
|
-
@user2.has_role!('owner', @bar)
|
232
|
-
|
233
|
-
Role.count.should == 1
|
234
|
-
|
235
|
-
@bar.accepts_no_role!('owner', @user2)
|
236
|
-
Role.count.should == 1
|
237
|
-
|
238
|
-
@bar.accepts_no_role!('owner', @user)
|
239
|
-
|
240
|
-
Role.count.should == 0
|
241
|
-
end
|
242
|
-
|
243
|
-
it "should be able to get users that have a role on a authorized object" do
|
244
|
-
@user.has_role!('owner', @bar)
|
245
|
-
@user2.has_role!('owner', @bar)
|
246
|
-
|
247
|
-
@bar.users.count.should == 2
|
248
|
-
end
|
249
|
-
|
250
|
-
it "should be able to get users that have a role on a authorized object with text primary key" do
|
251
|
-
@user.has_role!('owner', @uuid)
|
252
|
-
@user2.has_role!('owner', @uuid)
|
253
|
-
|
254
|
-
@uuid.users.count.should == 2
|
255
|
-
end
|
256
|
-
|
257
|
-
it "should accept :symbols as role names" do
|
258
|
-
@user.has_role! :admin
|
259
|
-
@user.has_role! :_3133t
|
260
|
-
|
261
|
-
@user.has_role! :admin, Foo
|
262
|
-
@user.has_role! :manager, Foo
|
263
|
-
@user.has_role! :user, @foo
|
264
|
-
@foo.accepts_role! :manager, @user
|
265
|
-
@bar.accepts_role! :owner, @user
|
266
|
-
|
267
|
-
@user.has_role?(:admin).should be_true
|
268
|
-
@user.has_role?(:_3133t).should be_true
|
269
|
-
@user.has_role?(:admin, Foo).should be_true
|
270
|
-
@user.has_role?(:manager, @foo).should be_true
|
271
|
-
end
|
272
|
-
|
273
|
-
private
|
274
|
-
|
275
|
-
def set_some_roles
|
276
|
-
@user.has_role!('admin')
|
277
|
-
@user.has_role!('3133t')
|
278
|
-
|
279
|
-
@user.has_role!('admin', Foo)
|
280
|
-
@user.has_role!('manager', Foo)
|
281
|
-
@user.has_role!('user', @foo)
|
282
|
-
@foo.accepts_role!('manager', @user)
|
283
|
-
@bar.accepts_role!('owner', @user)
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
|
288
|
-
class RolesWithCustomClassNamesTest < Test::Unit::TestCase
|
289
|
-
before do
|
290
|
-
AnotherRole.destroy_all
|
291
|
-
[AnotherSubject, FooBar].each { |model| model.delete_all }
|
292
|
-
|
293
|
-
@subj = AnotherSubject.create!
|
294
|
-
@subj2 = AnotherSubject.create!
|
295
|
-
@foobar = FooBar.create!
|
296
|
-
end
|
297
|
-
|
298
|
-
it "should basically work" do
|
299
|
-
lambda do
|
300
|
-
@subj.has_role!('admin')
|
301
|
-
@subj.has_role!('user', @foobar)
|
302
|
-
end.should change { AnotherRole.count }.from(0).to(2)
|
303
|
-
|
304
|
-
@subj.has_role?('admin').should be_true
|
305
|
-
@subj2.has_role?('admin').should be_false
|
306
|
-
|
307
|
-
@subj.has_role?(:user, @foobar).should be_true
|
308
|
-
@subj2.has_role?(:user, @foobar).should be_false
|
309
|
-
|
310
|
-
@subj.has_no_roles!
|
311
|
-
@subj2.has_no_roles!
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
class RolesWithCustomAssociationNamesTest < Test::Unit::TestCase
|
316
|
-
before do
|
317
|
-
DifferentAssociationNameRole.destroy_all
|
318
|
-
[DifferentAssociationNameSubject, FooBar].each { |model| model.delete_all }
|
319
|
-
|
320
|
-
@subj = DifferentAssociationNameSubject.create!
|
321
|
-
@subj2 = DifferentAssociationNameSubject.create!
|
322
|
-
@foobar = FooBar.create!
|
323
|
-
end
|
324
|
-
|
325
|
-
it "should basically work" do
|
326
|
-
lambda do
|
327
|
-
@subj.has_role!('admin')
|
328
|
-
@subj.has_role!('user', @foobar)
|
329
|
-
end.should change { DifferentAssociationNameRole.count }.from(0).to(2)
|
330
|
-
|
331
|
-
@subj.has_role?('admin').should be_true
|
332
|
-
@subj2.has_role?('admin').should be_false
|
333
|
-
|
334
|
-
@subj.has_role?(:user, @foobar).should be_true
|
335
|
-
@subj2.has_role?(:user, @foobar).should be_false
|
336
|
-
|
337
|
-
@subj.has_no_roles!
|
338
|
-
@subj2.has_no_roles!
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
class UsersRolesAndSubjectsWithNamespacedClassNamesTest < Test::Unit::TestCase
|
343
|
-
before do
|
344
|
-
Other::Role.destroy_all
|
345
|
-
[Other::User, Other::FooBar].each { |model| model.delete_all }
|
346
|
-
|
347
|
-
@user = Other::User.create!
|
348
|
-
@user2 = Other::User.create!
|
349
|
-
@foobar = Other::FooBar.create!
|
350
|
-
|
351
|
-
end
|
352
|
-
|
353
|
-
it "should basically work" do
|
354
|
-
lambda do
|
355
|
-
@user.has_role!('admin')
|
356
|
-
@user.has_role!('user', @foobar)
|
357
|
-
end.should change { Other::Role.count }.from(0).to(2)
|
358
|
-
|
359
|
-
@user.has_role?('admin').should be_true
|
360
|
-
@user2.has_role?('admin').should be_false
|
361
|
-
|
362
|
-
@user.has_role?(:user, @foobar).should be_true
|
363
|
-
@user2.has_role?(:user, @foobar).should be_false
|
364
|
-
|
365
|
-
@foobar.accepted_roles.count.should == 1
|
366
|
-
|
367
|
-
@user.has_no_roles!
|
368
|
-
@user2.has_no_roles!
|
369
|
-
end
|
370
|
-
end
|
data/test/support/controllers.rb
DELETED
@@ -1,207 +0,0 @@
|
|
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
|