Suppose there are 2 roles- admins who can manage everything, and users who have registered, and need admin approval of the specific CRUD actions they can perform on each controller. Like X can create and read Articles only, while Y can perform update and delete actions. How do I also provide for the admin to assign the users these specific roles from the website itself?
How do I assign specific roles to each user in rails?
860 views Asked by Souvik Das At
2
There are 2 answers
3
On
You should check rolify gem: https://github.com/RolifyCommunity/rolify
But if you want just 2 roles(User and Admin), you can add new column to user table :admin, :boolean, default: false
, then you can check in your controllers you can write before_filter
which check if @current_user.admin
and if not it redirects or does smth else.
In your view you can hide buttons for updating and deleting unless @current_user.admin
How about this Gem https://github.com/stffn/declarative_authorization/ ?
You can make it very granule. Define the roles then you can assign your CURD accordingly. Also it has a very good Helper methods that you can use to determine which role do what task on the View or Controller.
Assumption is based on
Here is a quick digram ( I assume you will take care of the Models )
Here is the module that I made to DRY some of the common tasks.
NOTE: Declarative Authorization has stacks of helpers, following is just to compliment them, some of the functions might be redundant and can be refactored and written better. It is just a nightly built :)