Module: Tasker::Concerns::Authorizable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/tasker/concerns/authorizable.rb
Overview
Authorizable concern for user models to integrate with Tasker authorization.
This concern provides a standard interface that user models can include to work with the Tasker authorization system. It defines conventional methods for checking permissions, roles, and admin status.
The concern is designed to be flexible and work with different authorization systems by providing sensible defaults that can be overridden as needed.
Defined Under Namespace
Classes: AdminStatusChecker
Instance Method Summary collapse
-
#all_tasker_permissions ⇒ Array<String>
Get all Tasker permissions for this user.
-
#can_access_tasker_resource?(resource) ⇒ Boolean
Check if user can perform any actions on a resource.
-
#has_tasker_permission?(permission) ⇒ Boolean
Check if the user has a specific permission.
-
#tasker_admin? ⇒ Boolean
Check if the user is an admin.
-
#tasker_permissions_for_resource(resource) ⇒ Array<Symbol>
Get permissions for a specific resource.
-
#tasker_roles ⇒ Array
Get the user’s roles.
Instance Method Details
#all_tasker_permissions ⇒ Array<String>
Get all Tasker permissions for this user
This method returns all permissions the user has that are related to Tasker resources.
156 157 158 159 |
# File 'lib/tasker/concerns/authorizable.rb', line 156 def = Tasker::Authorization::ResourceRegistry. .select { || () } end |
#can_access_tasker_resource?(resource) ⇒ Boolean
Check if user can perform any actions on a resource
146 147 148 |
# File 'lib/tasker/concerns/authorizable.rb', line 146 def can_access_tasker_resource?(resource) (resource).any? end |
#has_tasker_permission?(permission) ⇒ Boolean
Check if the user has a specific permission
This is the primary method for checking permissions. The default implementation looks for a ‘permissions` method on the user object. Override this method to integrate with your authorization system.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tasker/concerns/authorizable.rb', line 74 def () # Use configured method or default logic = self.class.[:permission_method] if != :has_tasker_permission? && respond_to?() send(, ) elsif respond_to?(:permissions) = &.include?() || false else false end end |
#tasker_admin? ⇒ Boolean
Check if the user is an admin
This method checks for admin status using common patterns. Override this method if your application uses different admin detection.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/tasker/concerns/authorizable.rb', line 113 def tasker_admin? # Use configured method or default logic admin_method = self.class.[:admin_method] if admin_method != :tasker_admin? && respond_to?(admin_method) send(admin_method) || false else AdminStatusChecker.check(self) end end |
#tasker_permissions_for_resource(resource) ⇒ Array<Symbol>
Get permissions for a specific resource
This method returns an array of actions the user can perform on a specific resource. Override this method to provide resource-specific permission logic.
132 133 134 135 136 137 138 139 140 |
# File 'lib/tasker/concerns/authorizable.rb', line 132 def (resource) # Default: return all actions for the resource if user has any permissions resource_config = Tasker::Authorization::ResourceRegistry.resources[resource] return [] unless resource_config resource_config[:actions].select do |action| ("#{resource}:#{action}") end end |
#tasker_roles ⇒ Array
Get the user’s roles
This method should return an array of role names/identifiers. The default implementation looks for a ‘roles` method.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/tasker/concerns/authorizable.rb', line 94 def tasker_roles # Use configured method or default logic role_method = self.class.[:role_method] if role_method != :tasker_roles && respond_to?(role_method) send(role_method) || [] elsif respond_to?(:roles) roles || [] else [] end end |