Module: Authorization::AuthorizationInController
- Defined in:
- lib/declarative_authorization/in_controller.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEFAULT_DENY =
false- @@failed_auto_loading_is_not_found =
If attribute_check is set for filter_access_to, decl_auth_context will try to load the appropriate object from the current controller's model with the id from params. If that fails, a 404 Not Found is often the right way to handle the error. If you have additional measures in place that restricts the find scope, handling this error as a permission denied might be a better way. Set failed_auto_loading_is_not_found to false for the latter behavior.
true
Class Method Summary collapse
- .failed_auto_loading_is_not_found=(new_value) ⇒ Object
- .failed_auto_loading_is_not_found? ⇒ Boolean
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#authorization_engine ⇒ Object
Returns the Authorization::Engine for the current controller.
-
#has_any_role?(*roles) ⇒ Boolean
Intended to be used where you want to allow users with any single listed role to view the content in question.
-
#has_any_role_with_hierarchy?(*roles) ⇒ Boolean
As has_any_role? except checks all roles included in the role hierarchy.
-
#has_role?(*roles) ⇒ Boolean
While permitted_to? is used for authorization, in some cases content should only be shown to some users without being concerned with authorization.
-
#has_role_with_hierarchy?(*roles) ⇒ Boolean
As has_role? except checks all roles included in the role hierarchy.
-
#permitted_to!(privilege, object_or_sym = nil, options = {}) ⇒ Object
Works similar to the permitted_to? method, but throws the authorization exceptions, just like Engine#permit!.
-
#permitted_to?(privilege, object_or_sym = nil, options = {}) ⇒ Boolean
If the current user meets the given privilege, permitted_to? returns true and yields to the optional block.
Class Method Details
.failed_auto_loading_is_not_found=(new_value) ⇒ Object
27 28 29 |
# File 'lib/declarative_authorization/in_controller.rb', line 27 def self.failed_auto_loading_is_not_found=(new_value) @@failed_auto_loading_is_not_found = new_value end |
.failed_auto_loading_is_not_found? ⇒ Boolean
24 25 26 |
# File 'lib/declarative_authorization/in_controller.rb', line 24 def self.failed_auto_loading_is_not_found? @@failed_auto_loading_is_not_found end |
.included(base) ⇒ Object
:nodoc:
7 8 9 10 11 12 |
# File 'lib/declarative_authorization/in_controller.rb', line 7 def self.included(base) # :nodoc: base.extend(ClassMethods) base.module_eval do before_action(:filter_access_filter) if method_defined?(:filter_access_filter) end end |
Instance Method Details
#authorization_engine ⇒ Object
Returns the Authorization::Engine for the current controller.
32 33 34 |
# File 'lib/declarative_authorization/in_controller.rb', line 32 def @authorization_engine ||= Authorization::Engine.instance end |
#has_any_role?(*roles) ⇒ Boolean
Intended to be used where you want to allow users with any single listed role to view the content in question
76 77 78 79 80 81 82 83 |
# File 'lib/declarative_authorization/in_controller.rb', line 76 def has_any_role?(*roles) user_roles = .roles_for(current_user) result = roles.any? do |role| user_roles.include?(role) end yield if result and block_given? result end |
#has_any_role_with_hierarchy?(*roles) ⇒ Boolean
As has_any_role? except checks all roles included in the role hierarchy
96 97 98 99 100 101 102 103 |
# File 'lib/declarative_authorization/in_controller.rb', line 96 def has_any_role_with_hierarchy?(*roles) user_roles = .roles_with_hierarchy_for(current_user) result = roles.any? do |role| user_roles.include?(role) end yield if result and block_given? result end |
#has_role?(*roles) ⇒ Boolean
While permitted_to? is used for authorization, in some cases content should only be shown to some users without being concerned with authorization. E.g. to only show the most relevant menu options to a certain group of users. That is what has_role? should be used for.
65 66 67 68 69 70 71 72 |
# File 'lib/declarative_authorization/in_controller.rb', line 65 def has_role?(*roles) user_roles = .roles_for(current_user) result = roles.all? do |role| user_roles.include?(role) end yield if result and block_given? result end |
#has_role_with_hierarchy?(*roles) ⇒ Boolean
As has_role? except checks all roles included in the role hierarchy
86 87 88 89 90 91 92 93 |
# File 'lib/declarative_authorization/in_controller.rb', line 86 def has_role_with_hierarchy?(*roles) user_roles = .roles_with_hierarchy_for(current_user) result = roles.all? do |role| user_roles.include?(role) end yield if result and block_given? result end |
#permitted_to!(privilege, object_or_sym = nil, options = {}) ⇒ Object
Works similar to the permitted_to? method, but throws the authorization exceptions, just like Engine#permit!
57 58 59 |
# File 'lib/declarative_authorization/in_controller.rb', line 57 def permitted_to!(privilege, object_or_sym = nil, = {}) .permit!(privilege, (object_or_sym, , true)) end |
#permitted_to?(privilege, object_or_sym = nil, options = {}) ⇒ Boolean
If the current user meets the given privilege, permitted_to? returns true and yields to the optional block. The attribute checks that are defined in the authorization rules are only evaluated if an object is given for context.
See examples for Authorization::AuthorizationHelper #permitted_to?
If no object or context is specified, the controller_name is used as context.
46 47 48 49 50 51 52 53 |
# File 'lib/declarative_authorization/in_controller.rb', line 46 def permitted_to?(privilege, object_or_sym = nil, = {}) if .permit!(privilege, (object_or_sym, , false)) yield if block_given? true else false end end |