docs/RoleManagement

Role and Permission Management

Overview

We can manage the roles and permission through CmAdmin. CmAdmin creates pundit policies dynamically and helps us the manage the permission through a interface.

Features

  • Create Role: We can create any role we want on the application
  • Manage Permissions: All the possible actions are listed for each role and we can enable or disable permission for each role.

Usage

  • To add role and permission table
  rake cm_admin:install_role
  • Rake task creates a default migration

  • Create a role column on the user table. Note: The column name has to be cm_role_id else policy will fail

rails g migration AddCmRoleToUser cm_role:references
  • Assign the role to the user. Right now only one role can be assigned to a user.

  • Assigning the params in the current attribute.

    In the `app/models/current.rb` add request_params as attribure
    

In the app/controllers/concerns/authentation.rb set the request_params. This helps cm-admin identify which action is being performed in the pundit policy.

module Authentication
extend ActiveSupport::Concern

included do
before_action :check_current_user
before_action :set_params
end

def set_params
if params
Current.request_params = params
end
end
end


## Override policy

By default, the Roles and policy gets enabled for all the models on the application. We can override the policy with the following


cm_admin do
actions only: []
set_icon “fa fa-user”
override_pundit_policy true
cm_index do
page_title ‘User’
….

and create a policy file on the application for the respective model. eg: `app/policies/cm_admin/user_policy.rb`

class CmAdmin::UserPolicy < ApplicationPolicy

def index?
true
end

end