1- # Authorization::AuthorizationInController
21require File . dirname ( __FILE__ ) + '/authorization.rb'
2+ require File . dirname ( __FILE__ ) + '/in_controller_common.rb'
33
44module Authorization
55 module AuthorizationInPraxisController
6+ include AuthorizationInControllerCommon
67
78 def self . included ( base ) # :nodoc:
89 base . extend ( ClassMethods )
@@ -13,97 +14,6 @@ def self.included(base) # :nodoc:
1314 end
1415 end
1516
16- DEFAULT_DENY = false
17-
18- # If attribute_check is set for filter_access_to, decl_auth_context will try to
19- # load the appropriate object from the current controller's model with
20- # the id from params[:id]. If that fails, a 404 Not Found is often the
21- # right way to handle the error. If you have additional measures in place
22- # that restricts the find scope, handling this error as a permission denied
23- # might be a better way. Set failed_auto_loading_is_not_found to false
24- # for the latter behavior.
25- @@failed_auto_loading_is_not_found = true
26- def self . failed_auto_loading_is_not_found?
27- @@failed_auto_loading_is_not_found
28- end
29- def self . failed_auto_loading_is_not_found = ( new_value )
30- @@failed_auto_loading_is_not_found = new_value
31- end
32-
33- # Returns the Authorization::Engine for the current controller.
34- def authorization_engine
35- @authorization_engine ||= Authorization ::Engine . instance
36- end
37-
38- # If the current user meets the given privilege, permitted_to? returns true
39- # and yields to the optional block. The attribute checks that are defined
40- # in the authorization rules are only evaluated if an object is given
41- # for context.
42- #
43- # See examples for Authorization::AuthorizationHelper #permitted_to?
44- #
45- # If no object or context is specified, the controller_name is used as
46- # context.
47- #
48- def permitted_to? ( privilege , object_or_sym = nil , options = { } )
49- if authorization_engine . permit! ( privilege , options_for_permit ( object_or_sym , options , false ) )
50- yield if block_given?
51- true
52- else
53- false
54- end
55- end
56-
57- # Works similar to the permitted_to? method, but
58- # throws the authorization exceptions, just like Engine#permit!
59- def permitted_to! ( privilege , object_or_sym = nil , options = { } )
60- authorization_engine . permit! ( privilege , options_for_permit ( object_or_sym , options , true ) )
61- end
62-
63- # While permitted_to? is used for authorization, in some cases
64- # content should only be shown to some users without being concerned
65- # with authorization. E.g. to only show the most relevant menu options
66- # to a certain group of users. That is what has_role? should be used for.
67- def has_role? ( *roles )
68- user_roles = authorization_engine . roles_for ( current_user )
69- result = roles . all? do |role |
70- user_roles . include? ( role )
71- end
72- yield if result and block_given?
73- result
74- end
75-
76- # Intended to be used where you want to allow users with any single listed role to view
77- # the content in question
78- def has_any_role? ( *roles )
79- user_roles = authorization_engine . roles_for ( current_user )
80- result = roles . any? do |role |
81- user_roles . include? ( role )
82- end
83- yield if result and block_given?
84- result
85- end
86-
87- # As has_role? except checks all roles included in the role hierarchy
88- def has_role_with_hierarchy? ( *roles )
89- user_roles = authorization_engine . roles_with_hierarchy_for ( current_user )
90- result = roles . all? do |role |
91- user_roles . include? ( role )
92- end
93- yield if result and block_given?
94- result
95- end
96-
97- # As has_any_role? except checks all roles included in the role hierarchy
98- def has_any_role_with_hierarchy? ( *roles )
99- user_roles = authorization_engine . roles_with_hierarchy_for ( current_user )
100- result = roles . any? do |role |
101- user_roles . include? ( role )
102- end
103- yield if result and block_given?
104- result
105- end
106-
10717 def controller_name
10818 self . class . name . demodulize . underscore
10919 end
@@ -151,7 +61,6 @@ def filter_access_filter # :nodoc:
15161 send ( :permission_denied )
15262 else
15363 Praxis ::Responses ::Forbidden . new ( body : "You are not allowed to access this action." )
154- # send(:render, :plain => "You are not allowed to access this action.", :status => :forbidden)
15564 end
15665 end
15766 end
0 commit comments