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.

Examples:

Basic usage

class User < ApplicationRecord
  include Tasker::Concerns::Authorizable
end

Customized configuration

class User < ApplicationRecord
  include Tasker::Concerns::Authorizable

  configure_tasker_authorization(
    permission_method: :can_do?,
    role_method: :user_roles,
    admin_method: :is_admin?
  )
end

Defined Under Namespace

Classes: AdminStatusChecker

Instance Method Summary collapse

Instance Method Details

#all_tasker_permissionsArray<String>

Get all Tasker permissions for this user

This method returns all permissions the user has that are related to Tasker resources.

Returns:

  • (Array<String>)

    Array of permission strings



156
157
158
159
# File 'lib/tasker/concerns/authorizable.rb', line 156

def all_tasker_permissions
  all_permissions = Tasker::Authorization::ResourceRegistry.all_permissions
  all_permissions.select { |permission| has_tasker_permission?(permission) }
end

#can_access_tasker_resource?(resource) ⇒ Boolean

Check if user can perform any actions on a resource

Parameters:

  • resource (String)

    The resource name

Returns:

  • (Boolean)

    True if user has any permissions for the resource



146
147
148
# File 'lib/tasker/concerns/authorizable.rb', line 146

def can_access_tasker_resource?(resource)
  tasker_permissions_for_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.

Parameters:

  • permission (String)

    Permission string (e.g., “tasker.task:show”)

Returns:

  • (Boolean)

    True if the user has the permission



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tasker/concerns/authorizable.rb', line 74

def has_tasker_permission?(permission)
  # Use configured method or default logic
  permission_method = self.class.tasker_authorizable_config[:permission_method]

  if permission_method != :has_tasker_permission? && respond_to?(permission_method)
    send(permission_method, permission)
  elsif respond_to?(:permissions)
    permissions_list = permissions
    permissions_list&.include?(permission) || 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.

Returns:

  • (Boolean)

    True if the user is an admin



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.tasker_authorizable_config[: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.

Parameters:

  • resource (String)

    The resource name (e.g., ‘tasker.task’)

Returns:

  • (Array<Symbol>)

    Array of permitted actions



132
133
134
135
136
137
138
139
140
# File 'lib/tasker/concerns/authorizable.rb', line 132

def tasker_permissions_for_resource(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|
    has_tasker_permission?("#{resource}:#{action}")
  end
end

#tasker_rolesArray

Get the user’s roles

This method should return an array of role names/identifiers. The default implementation looks for a ‘roles` method.

Returns:

  • (Array)

    Array of user roles



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.tasker_authorizable_config[:role_method]

  if role_method != :tasker_roles && respond_to?(role_method)
    send(role_method) || []
  elsif respond_to?(:roles)
    roles || []
  else
    []
  end
end