Skip to content

Commit 4112e5a

Browse files
style: Delegate helper methods to controller
Rather than defining them separately. Since the helper methods don't do anything different than the controller methods (method signature is also unchanged), they can just be forwarded. This will also help with the effort to include hiearchy be default since the keyword argument doesn't need to be passed manually (causing an expectation error in tests).
1 parent fbd0f31 commit 4112e5a

2 files changed

Lines changed: 54 additions & 90 deletions

File tree

lib/declarative_authorization/controller/runtime.rb

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Runtime
1414
def self.failed_auto_loading_is_not_found?
1515
@@failed_auto_loading_is_not_found
1616
end
17+
1718
def self.failed_auto_loading_is_not_found=(new_value)
1819
@@failed_auto_loading_is_not_found = new_value
1920
end
@@ -28,11 +29,27 @@ def authorization_engine
2829
# in the authorization rules are only evaluated if an object is given
2930
# for context.
3031
#
31-
# See examples for Authorization::AuthorizationHelper #permitted_to?
32-
#
3332
# If no object or context is specified, the controller_name is used as
3433
# context.
3534
#
35+
# Examples:
36+
# <% permitted_to? :create, :users do %>
37+
# <%= link_to 'New', new_user_path %>
38+
# <% end %>
39+
# ...
40+
# <% if permitted_to? :create, :users %>
41+
# <%= link_to 'New', new_user_path %>
42+
# <% else %>
43+
# You are not allowed to create new users!
44+
# <% end %>
45+
# ...
46+
# <% for user in @users %>
47+
# <%= link_to 'Edit', edit_user_path(user) if permitted_to? :update, user %>
48+
# <% end %>
49+
#
50+
# To pass in an object and override the context, you can use the optional
51+
# options:
52+
# permitted_to? :update, user, :context => :account
3653
def permitted_to?(privilege, object_or_sym = nil, options = {})
3754
if authorization_engine.permit!(privilege, options_for_permit(object_or_sym, options, false))
3855
yield if block_given?
@@ -48,16 +65,27 @@ def permitted_to!(privilege, object_or_sym = nil, options = {})
4865
authorization_engine.permit!(privilege, options_for_permit(object_or_sym, options, true))
4966
end
5067

51-
# While permitted_to? is used for authorization, in some cases
68+
# While permitted_to? is used for authorization in views, in some cases
5269
# content should only be shown to some users without being concerned
5370
# with authorization. E.g. to only show the most relevant menu options
5471
# to a certain group of users. That is what has_role? should be used for.
72+
#
73+
# Examples:
74+
# <% has_role?(:sales) do %>
75+
# <%= link_to 'All contacts', contacts_path %>
76+
# <% end %>
77+
# ...
78+
# <% if has_role?(:sales) %>
79+
# <%= link_to 'Customer contacts', contacts_path %>
80+
# <% else %>
81+
# ...
82+
# <% end %>
5583
def has_role?(*roles)
5684
user_roles = authorization_engine.roles_for(current_user)
5785
result = roles.all? do |role|
5886
user_roles.include?(role)
5987
end
60-
yield if result and block_given?
88+
yield if result && block_given?
6189
result
6290
end
6391

@@ -68,7 +96,7 @@ def has_any_role?(*roles)
6896
result = roles.any? do |role|
6997
user_roles.include?(role)
7098
end
71-
yield if result and block_given?
99+
yield if result && block_given?
72100
result
73101
end
74102

@@ -78,7 +106,7 @@ def has_role_with_hierarchy?(*roles)
78106
result = roles.all? do |role|
79107
user_roles.include?(role)
80108
end
81-
yield if result and block_given?
109+
yield if result && block_given?
82110
result
83111
end
84112

@@ -88,24 +116,26 @@ def has_any_role_with_hierarchy?(*roles)
88116
result = roles.any? do |role|
89117
user_roles.include?(role)
90118
end
91-
yield if result and block_given?
119+
yield if result && block_given?
92120
result
93121
end
94122

95123
def options_for_permit(object_or_sym = nil, options = {}, bang = true)
96124
context = object = nil
97125
if object_or_sym.nil?
98126
context = decl_auth_context
99-
elsif !Authorization.is_a_association_proxy?(object_or_sym) and object_or_sym.is_a?(Symbol)
127+
elsif !Authorization.is_a_association_proxy?(object_or_sym) && object_or_sym.is_a?(Symbol)
100128
context = object_or_sym
101129
else
102130
object = object_or_sym
103131
end
104132

105-
result = {:object => object,
106-
:context => context,
107-
:skip_attribute_test => object.nil?,
108-
:bang => bang}.merge(options)
133+
result = {
134+
object: object,
135+
context: context,
136+
skip_attribute_test: object.nil?,
137+
bang: bang
138+
}.merge(options)
109139
result[:user] = current_user unless result.key?(:user)
110140
result
111141
end
@@ -120,12 +150,12 @@ def allowed?(action_name)
120150

121151
begin
122152
allowed = if matching_permissions.any?
123-
matching_permissions.all? { |p| p.permit!(self, action_name) }
124-
elsif all_permissions.any?
125-
all_permissions.all? { |p| p.permit!(self, action_name) }
126-
else
127-
!DEFAULT_DENY
128-
end
153+
matching_permissions.all? { |p| p.permit!(self, action_name) }
154+
elsif all_permissions.any?
155+
all_permissions.all? { |p| p.permit!(self, action_name) }
156+
else
157+
!DEFAULT_DENY
158+
end
129159
rescue ::Authorization::NotAuthorized => e
130160
auth_exception = e
131161
end
Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,12 @@
11
# Authorization::AuthorizationHelper
2-
require File.dirname(__FILE__) + '/authorization.rb'
2+
require "#{File.dirname(__FILE__)}/authorization.rb"
33

44
module Authorization
5+
# Include this module in your views
56
module AuthorizationHelper
6-
7-
# If the current user meets the given privilege, permitted_to? returns true
8-
# and yields to the optional block. The attribute checks that are defined
9-
# in the authorization rules are only evaluated if an object is given
10-
# for context.
11-
#
12-
# Examples:
13-
# <% permitted_to? :create, :users do %>
14-
# <%= link_to 'New', new_user_path %>
15-
# <% end %>
16-
# ...
17-
# <% if permitted_to? :create, :users %>
18-
# <%= link_to 'New', new_user_path %>
19-
# <% else %>
20-
# You are not allowed to create new users!
21-
# <% end %>
22-
# ...
23-
# <% for user in @users %>
24-
# <%= link_to 'Edit', edit_user_path(user) if permitted_to? :update, user %>
25-
# <% end %>
26-
#
27-
# To pass in an object and override the context, you can use the optional
28-
# options:
29-
# permitted_to? :update, user, :context => :account
30-
#
31-
def permitted_to?(privilege, object_or_sym = nil, options = {})
32-
controller.permitted_to?(privilege, object_or_sym, options) do
33-
yield if block_given?
34-
end
35-
end
36-
37-
# While permitted_to? is used for authorization in views, in some cases
38-
# content should only be shown to some users without being concerned
39-
# with authorization. E.g. to only show the most relevant menu options
40-
# to a certain group of users. That is what has_role? should be used for.
41-
#
42-
# Examples:
43-
# <% has_role?(:sales) do %>
44-
# <%= link_to 'All contacts', contacts_path %>
45-
# <% end %>
46-
# ...
47-
# <% if has_role?(:sales) %>
48-
# <%= link_to 'Customer contacts', contacts_path %>
49-
# <% else %>
50-
# ...
51-
# <% end %>
52-
#
53-
def has_role?(*roles)
54-
controller.has_role?(*roles) do
55-
yield if block_given?
56-
end
57-
end
58-
59-
# As has_role? except checks all roles included in the role hierarchy
60-
def has_role_with_hierarchy?(*roles)
61-
controller.has_role_with_hierarchy?(*roles) do
62-
yield if block_given?
63-
end
64-
end
65-
66-
def has_any_role?(*roles)
67-
controller.has_any_role?(*roles) do
68-
yield if block_given?
69-
end
70-
end
71-
72-
def has_any_role_with_hierarchy?(*roles)
73-
controller.has_any_role_with_hierarchy?(*roles) do
74-
yield if block_given?
75-
end
76-
end
7+
delegate :has_role?, :has_role_with_hierarchy?,
8+
:has_any_role?, :has_any_role_with_hierarchy?,
9+
:permitted_to?,
10+
to: :controller
7711
end
7812
end

0 commit comments

Comments
 (0)