Roles & Permissions
Admins create roles (Catalyst::Role) and enable permissions per model and action from the Roles page. Catalyst turns them into Pundit policies at runtime, optionally limited to a permission scope. Each user gets one role.
Quick exampleβ
rails g catalyst:install_role
rails g migration AddCtRoleToUser ct_role:references
rails db:migrate
catalyst do
policy_scopes [{ scope_name: "test_supplier_filter", display_name: "By Test Supplier" }]
end
Setupβ
1. Add the role and permission tablesβ
rails g catalyst:install_role
Requires the paper_trail gem.
2. Add the role column to usersβ
rails g migration AddCtRoleToUser ct_role:references
The column name must be ct_role_id, or the policy fails.
3. Add the unique index for bulk permission updatesβ
rails g catalyst:add_unique_index_to_permissions
Adds a unique index on (ar_model_name, action_name, ct_role_id) so bulk permission updates can use upsert_all.
4. Assign roles to usersβ
- Add
request_paramsas an attribute inapp/models/current.rb. - Set it on every request so Catalyst can identify the action in the Pundit policy:
module Authentication
extend ActiveSupport::Concern
included do
before_action :check_current_user
before_action :set_params
end
def set_params
Current.request_params = params if params
end
end
- Add
belongs_to :ct_role, optional: trueto the User model. - Add
Catalyst::Roletoconfig.included_modelsinconfig/initializers/catalyst.rb. - Set
ct_role_idon a user and log in as that user.
Permission scopesβ
Every permission gets a Full Access scope by default. Declare extra scopes on the model:
catalyst do
policy_scopes [{ scope_name: 'test_supplier_filter', display_name: 'By Test Supplier' }]
end
Using permission scopes in codeβ
ct_permission_scoped_records filters records by the current user's permission scope β useful in background jobs, custom controllers and helpers where you need the same scoping as the admin UI. It lives in Catalyst::PermissionHelper:
module Catalyst
module CustomHelper
include Catalyst::Admin::PermissionHelper
def select_options_for_users(_ = nil, _ = nil)
ct_permission_scoped_records(::User).map { |user| [user.ct_display_name, user.id] }
end
end
end
ct_permission_scoped_records(model_class)
model_classβ the ActiveRecord model class to scope (User,Order, β¦)- Returns an
ActiveRecord::Relationscoped to the user's permissions
It finds the user's permission for the model's index action, calls the permission's scope on the model when it isn't 'all', and falls back to model_class.all. Requires the user to have a ct_role with ct_permissions, and the model to define the scope.
Checking permissions in codeβ
Always check permissions, never role names. Add these helpers to your User model:
def permission_with_scope?(model_name:, action_name:, scope_name:)
ct_role.ct_permissions.where(ar_model_name: model_name, action_name:, scope_name:).exists?
end
def has_permission?(model_name, action_name)
return false unless ct_role.present?
ct_role.ct_permissions.exists?(ar_model_name: model_name, action_name: action_name)
end
With a scope:
unless Current.user.permission_with_scope?(model_name: 'ReimbursementRequest', action_name: 'revert_acknowledged', scope_name: 'all')
# handle unauthorized
end
Without a scope:
column :note_type, header: 'Note Type', field_type: :enum, display_if: lambda { |_|
::Current.user.has_permission?('Customer', 'customer_restricted_fields')
}
Avoid role checks like Current.user&.role?('admin') or Current.user.ct_role.name == 'admin' β they bypass the permission system.
Overriding policiesβ
Policies are generated for every model by default. Set override_policy to supply your own:
catalyst do
override_policy true
end
class Catalyst::UserPolicy < ApplicationPolicy
def index?
true
end
end
Recipesβ
Show a field only with a scoped permissionβ
tab :details, '' do
row do
show_section 'Details' do
field :status, field_type: :badge, badge_class: Item::STATUS_TAG_COLOR, display_if: ->(record) {
scoped_model = Catalyst::ItemPolicyArchiveScope.new(Current.user, ::Item).resolve
scoped_model.find_by(id: record.id).present?
}
end
end
end
Set a default redirect path for a roleβ
Set the root route and add a default_redirect_path column on ct_roles (added automatically in new apps):
root 'catalyst/static#index'
add_column :ct_roles, :default_redirect_path, :string, default: "#{Catalyst::Engine.mount_path}/users"
Relatedβ
- Authentication & OTP
- Has One Tab: policies for association tabs
- Generators