Skip to main content

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​

  1. Add request_params as an attribute in app/models/current.rb.
  2. Set it on every request so Catalyst can identify the action in the Pundit policy:
app/controllers/concerns/authentication.rb
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
  1. Add belongs_to :ct_role, optional: true to the User model.
  2. Add Catalyst::Role to config.included_models in config/initializers/catalyst.rb.
  3. Set ct_role_id on 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::Relation scoped 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
app/policies/catalyst/user_policy.rb
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):

config/routes.rb
root 'catalyst/static#index'
add_column :ct_roles, :default_redirect_path, :string, default: "#{Catalyst::Engine.mount_path}/users"