Skip to content

Commit cc32ca2

Browse files
committed
Fix conflicts
1 parent da5e390 commit cc32ca2

File tree

12 files changed

+81
-135
lines changed

12 files changed

+81
-135
lines changed

lib/acl9.rb

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,6 @@ def merge! h
3535
def self.configure
3636
yield config
3737
end
38-
39-
class ArgumentError < ArgumentError; end
40-
class RuntimeError < RuntimeError; end
41-
class NilObjectError < RuntimeError; end
42-
43-
##
44-
# This exception is raised whenever ACL block finds that the current user
45-
# is not authorized for the controller action he wants to execute.
46-
# @example How to catch this exception in ApplicationController
47-
# class ApplicationController < ActionController::Base
48-
# rescue_from 'Acl9::AccessDenied', :with => :access_denied
49-
#
50-
# # ...other stuff...
51-
# private
52-
#
53-
# def access_denied
54-
# if current_user
55-
# # It's presumed you have a template with words of pity and regret
56-
# # for unhappy user who is not authorized to do what he wanted
57-
# render :template => 'home/access_denied'
58-
# else
59-
# # In this case user has not even logged in. Might be OK after login.
60-
# flash[:notice] = 'Access denied. Try to log in first.'
61-
# redirect_to login_path
62-
# end
63-
# end
64-
# end
65-
#
66-
class AccessDenied < RuntimeError; end
67-
68-
##
69-
# This exception is raised when acl9 has generated invalid code for the
70-
# filtering method or block. Should never happen, and it's a bug when it
71-
# happens.
72-
class FilterSyntaxError < ArgumentError; end
73-
7438
end
7539

7640
ActiveRecord::Base.send(:include, Acl9::ModelExtensions)

lib/acl9/controller_extensions/dsl_base.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def initialize(*args)
1212
@allows = []
1313
@denys = []
1414
@original_args = args
15-
@action_clause = nil
1615
end
1716

1817
def acl_block!(&acl_block)
@@ -109,15 +108,15 @@ def _parse_and_add_rule(*args)
109108

110109
_set_action_clause( _retrieve_only(options), options.delete(:except))
111110

112-
object_s = _role_object_s(options)
111+
object = _role_object(options)
113112

114113
role_checks = args.map do |who|
115114
case who
116115
when anonymous then "#{_subject_ref}.nil?"
117116
when logged_in then "!#{_subject_ref}.nil?"
118117
when all then "true"
119118
else
120-
"!#{_subject_ref}.nil? && #{_subject_ref}.has_role?('#{who}'#{object_s})"
119+
"!#{_subject_ref}.nil? && #{_subject_ref}.has_role?('#{who}', #{object})"
121120
end
122121
end
123122

@@ -180,13 +179,13 @@ def _action_check_expression(action_list)
180179
end
181180
end
182181

183-
def _role_object_s(options)
182+
def _role_object(options)
184183
object = _by_preposition options
185184

186185
case object
187-
when Class then ", #{object}"
188-
when Symbol then ", #{_object_ref object}"
189-
when nil then ""
186+
when Class then object.to_s
187+
when Symbol then _object_ref object
188+
when nil then "nil"
190189
else
191190
raise ArgumentError, "object specified by preposition can only be a Class or a Symbol"
192191
end

lib/acl9/controller_extensions/generators.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
require_relative "dsl_base"
22

33
module Acl9
4+
##
5+
# This exception is raised whenever ACL block finds that the current user
6+
# is not authorized for the controller action he wants to execute.
7+
# @example How to catch this exception in ApplicationController
8+
# class ApplicationController < ActionController::Base
9+
# rescue_from 'Acl9::AccessDenied', :with => :access_denied
10+
#
11+
# # ...other stuff...
12+
# private
13+
#
14+
# def access_denied
15+
# if current_user
16+
# # It's presumed you have a template with words of pity and regret
17+
# # for unhappy user who is not authorized to do what he wanted
18+
# render :template => 'home/access_denied'
19+
# else
20+
# # In this case user has not even logged in. Might be OK after login.
21+
# flash[:notice] = 'Access denied. Try to log in first.'
22+
# redirect_to login_path
23+
# end
24+
# end
25+
# end
26+
#
27+
class AccessDenied < StandardError; end
28+
29+
##
30+
# This exception is raised when acl9 has generated invalid code for the
31+
# filtering method or block. Should never happen, and it's a bug when it
32+
# happens.
33+
class FilterSyntaxError < StandardError; end
34+
435
module Dsl
536
module Generators
637
class BaseGenerator < Acl9::Dsl::Base
@@ -140,7 +171,7 @@ def #{@method_name}(*args)
140171
raise ArgumentError, "call #{@method_name} with 0, 1 or 2 arguments"
141172
end
142173
143-
self.action_name = args.first.to_s if args.present?
174+
action_name = args.empty? ? self.action_name : args.first.to_s
144175
145176
return #{allowance_expression}
146177
end

lib/acl9/model_extensions/for_subject.rb

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ module ModelExtensions
55
module ForSubject
66
include Prepositions
77

8-
DEFAULT = Class.new do
9-
def default?
10-
true
11-
end
12-
end.new.freeze
13-
148
##
159
# Role check.
1610
#
@@ -44,17 +38,16 @@ def default?
4438
# @param [Object] object Object to query a role on
4539
#
4640
# @see Acl9::ModelExtensions::Object#accepts_role?
47-
def has_role?(role_name, object = default)
48-
check! object
41+
def has_role?(role_name, object = nil)
4942
role_name = normalize role_name
5043
object = _by_preposition object
5144

52-
!! if object == default && !::Acl9.config[:protect_global_roles]
53-
_role_objects.find_by_name(role_name.to_s) ||
54-
_role_objects.member?(get_role(role_name, object))
45+
!! if object.nil? && !::Acl9.config[:protect_global_roles]
46+
self._role_objects.find_by_name(role_name.to_s) ||
47+
self._role_objects.member?(get_role(role_name, nil))
5548
else
5649
role = get_role(role_name, object)
57-
role && _role_objects.exists?(role.id)
50+
role && self._role_objects.exists?(role.id)
5851
end
5952
end
6053

@@ -64,24 +57,23 @@ def has_role?(role_name, object = default)
6457
# @param [Symbol,String] role_name Role name
6558
# @param [Object] object Object to add a role for
6659
# @see Acl9::ModelExtensions::Object#accepts_role!
67-
def has_role!(role_name, object = default)
68-
check! object
60+
def has_role!(role_name, object = nil)
6961
role_name = normalize role_name
7062
object = _by_preposition object
7163

7264
role = get_role(role_name, object)
7365

7466
if role.nil?
7567
role_attrs = case object
76-
when Class then { :authorizable_type => object.to_s }
77-
when default then {}
78-
else { :authorizable => object }
79-
end.merge({ :name => role_name.to_s })
68+
when Class then { :authorizable_type => object.to_s }
69+
when nil then {}
70+
else { :authorizable => object }
71+
end.merge( { :name => role_name.to_s })
8072

81-
role = _auth_role_class.create(role_attrs)
73+
role = self._auth_role_class.create(role_attrs)
8274
end
8375

84-
_role_objects << role if role && !_role_objects.exists?(role.id)
76+
self._role_objects << role if role && !self._role_objects.exists?(role.id)
8577
end
8678

8779
##
@@ -90,8 +82,7 @@ def has_role!(role_name, object = default)
9082
# @param [Symbol,String] role_name Role name
9183
# @param [Object] object Object to remove a role on
9284
# @see Acl9::ModelExtensions::Object#accepts_no_role!
93-
def has_no_role!(role_name, object = default)
94-
check! object
85+
def has_no_role!(role_name, object = nil)
9586
role_name = normalize role_name
9687
object = _by_preposition object
9788
delete_role(get_role(role_name, object))
@@ -104,8 +95,7 @@ def has_no_role!(role_name, object = default)
10495
# @return [Boolean] Returns true if +self+ has any roles on +object+.
10596
# @see Acl9::ModelExtensions::Object#accepts_roles_by?
10697
def has_roles_for?(object)
107-
check! object
108-
!!_role_objects.detect(&role_selecting_lambda(object))
98+
!!self._role_objects.detect(&role_selecting_lambda(object))
10999
end
110100

111101
alias :has_role_for? :has_roles_for?
@@ -122,16 +112,14 @@ def has_roles_for?(object)
122112
#
123113
# user.roles_for(product).map(&:name).sort #=> role names in alphabetical order
124114
def roles_for(object)
125-
check! object
126-
_role_objects.select(&role_selecting_lambda(object))
115+
self._role_objects.select(&role_selecting_lambda(object))
127116
end
128117

129118
##
130119
# Unassign any roles on +object+ from +self+.
131120
#
132-
# @param [Object,default] object Object to unassign roles for. Empty args means unassign global roles.
133-
def has_no_roles_for!(object = default)
134-
check! object
121+
# @param [Object,nil] object Object to unassign roles for. +nil+ means unassign global roles.
122+
def has_no_roles_for!(object = nil)
135123
roles_for(object).each { |role| delete_role(role) }
136124
end
137125

@@ -140,11 +128,11 @@ def has_no_roles_for!(object = default)
140128
def has_no_roles!
141129
# for some reason simple
142130
#
143-
# roles.each { |role| delete_role(role) }
131+
# self.roles.each { |role| delete_role(role) }
144132
#
145133
# doesn't work. seems like a bug in ActiveRecord
146-
_role_objects.map(&:id).each do |role_id|
147-
delete_role _auth_role_class.find(role_id)
134+
self._role_objects.map(&:id).each do |role_id|
135+
delete_role self._auth_role_class.find(role_id)
148136
end
149137
end
150138

@@ -154,7 +142,7 @@ def role_selecting_lambda(object)
154142
case object
155143
when Class
156144
lambda { |role| role.authorizable_type == object.to_s }
157-
when default
145+
when nil
158146
lambda { |role| role.authorizable.nil? }
159147
else
160148
lambda do |role|
@@ -164,14 +152,13 @@ def role_selecting_lambda(object)
164152
end
165153
end
166154

167-
def get_role(role_name, object = default)
168-
check! object
155+
def get_role(role_name, object)
169156
role_name = normalize role_name
170157

171158
cond = case object
172159
when Class
173160
[ 'name = ? and authorizable_type = ? and authorizable_id IS NULL', role_name, object.to_s ]
174-
when default
161+
when nil
175162
[ 'name = ? and authorizable_type IS NULL and authorizable_id IS NULL', role_name ]
176163
else
177164
[
@@ -180,17 +167,17 @@ def get_role(role_name, object = default)
180167
]
181168
end
182169

183-
if _auth_role_class.respond_to?(:where)
184-
_auth_role_class.where(cond).first
170+
if self._auth_role_class.respond_to?(:where)
171+
self._auth_role_class.where(cond).first
185172
else
186-
_auth_role_class.find(:first, :conditions => cond)
173+
self._auth_role_class.find(:first, :conditions => cond)
187174
end
188175
end
189176

190177
def delete_role(role)
191178
if role
192-
if ret = _role_objects.delete(role)
193-
if role.send(_auth_subject_class_name.demodulize.tableize).empty?
179+
if ret = self._role_objects.delete(role)
180+
if role.send(self._auth_subject_class_name.demodulize.tableize).empty?
194181
ret &&= role.destroy unless role.respond_to?(:system?) && role.system?
195182
end
196183
end
@@ -202,11 +189,7 @@ def normalize role_name
202189
Acl9.config[:normalize_role_names] ? role_name.to_s.underscore.singularize : role_name.to_s
203190
end
204191

205-
private
206-
207-
def check! object
208-
raise NilObjectError if object.nil?
209-
end
192+
protected
210193

211194
def _by_preposition object
212195
object.is_a?(Hash) ? super : object
@@ -221,11 +204,7 @@ def _auth_role_assoc
221204
end
222205

223206
def _role_objects
224-
send(_auth_role_assoc)
225-
end
226-
227-
def default
228-
DEFAULT
207+
send(self._auth_role_assoc)
229208
end
230209
end
231210
end

test/controller_extensions/actions_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class ActionTest < Base
121121

122122
assert set_all_actions
123123
permit_some owner, @all_actions, :foo => foo
124-
permit_some hacker, %w(show index destroy), foo: foo
124+
permit_some hacker, %w(show index destroy)
125125
permit_some another_owner, %w(show index destroy), :foo => foo
126126
end
127127

test/controller_extensions/multiple_role_arguments_test.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,29 @@ class MultipleRoleArgumentsTest < Base
107107

108108
test "should also respect :to and :except" do
109109
assert foo = Foo.create
110-
assert too = Foo.create
111110

112-
assert ( goo = User.create ).has_role! :goo
111+
assert ( foo = User.create ).has_role! :foo
113112
assert ( joo = User.create ).has_role! :joo, foo
114113
assert ( qoo = User.create ).has_role! :qoo, Bar
115114

116115
@tester.acl_block! do
117-
allow :goo, :boo, :to => [:index, :show]
116+
allow :foo, :boo, :to => [:index, :show]
118117
allow :zoo, :joo, :by => :foo, :to => [:edit, :update]
119118
allow :qoo, :woo, :of => Bar
120119
deny :qoo, :woo, :of => Bar, :except => [:delete, :destroy]
121120
end
122121

123-
assert_permitted goo, 'index'
124-
assert_permitted goo, 'show'
125-
assert_forbidden goo, 'edit', foo: too
122+
assert_permitted foo, 'index'
123+
assert_permitted foo, 'show'
124+
assert_forbidden foo, 'edit'
126125
assert_permitted joo, 'edit', :foo => foo
127126
assert_permitted joo, 'update', :foo => foo
128127
assert_forbidden joo, 'show', :foo => foo
129-
assert_forbidden joo, 'show', foo: foo
130-
assert_permitted qoo, 'delete', foo: too
131-
assert_permitted qoo, 'destroy', foo: too
132-
assert_forbidden qoo, 'edit', foo: too
133-
assert_forbidden qoo, 'show', foo: too
128+
assert_forbidden joo, 'show'
129+
assert_permitted qoo, 'delete'
130+
assert_permitted qoo, 'destroy'
131+
assert_forbidden qoo, 'edit'
132+
assert_forbidden qoo, 'show'
134133
end
135134
end
136135
end

test/controllers/acl_helper_method_test.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ class ACLHelperMethodTest < ActionController::TestCase
1313
end
1414

1515
test "another user denied" do
16-
assert @another = User.create
17-
assert @another.has_role! :owner, Foo.first_or_create
18-
1916
assert @user.has_role! :owner
2017

2118
assert get :allow, params: { user_id: @user.id }
22-
assert_select 'div', 'AccessDenied'
19+
assert_select 'div', 'OK'
2320
end
2421

2522
test "anon denied" do

0 commit comments

Comments
 (0)