@@ -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
0 commit comments