Ahmad hamza
Ahmad hamza

Reputation: 1936

Add an admin role in rails application

I want to know how to add an admin role by using devise gem. I ve already used it for normal users but not for admin role. I ve read this https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role. It is clearly mentioned about how to add the role of admin into the application. But i've some questions regarding that which are as follows:

  1. How to specify that particular user is an admin (in my case i just want a private admin for controlling the activities in an app)?

  2. How to do basic CRUD activities using admin role (i want CRUD activities for users and its contents)?

Thanks

Upvotes: 0

Views: 1643

Answers (3)

Abdulrazaq Haroon
Abdulrazaq Haroon

Reputation: 63

Current rails don't have a way to add except with gems like CancanCan and rolify. follow the steps in their documentation also setting the admin_role for all other users to false and use rails console to set your desired user to true The video below was very helpful.

Web crunch tutorial, Admin role

Upvotes: 0

gangelo
gangelo

Reputation: 3182

For an updated solution using Rails 5, Devise and CanCanCan (CanCan doesn't look like it's supported anymore), check this out:

  1. RAILS 5: USER ACCOUNTS WITH 3 TYPES OF ROLES – DEVISE, RAILS_ADMIN, CANCANCAN

Here is an alternate solution that is worth mentioning; it uses Rails Pundit and has_secure_password and is worth mentioning:

  1. Implementing Roles and Permissions in Ruby on Rails

The advantage of using CanCanCan is that it decouples authorization from user roles whereas a lot of people find Pundit easier to work with less invasive. Both are good.

Both address what you're looking to do.

Upvotes: 0

n_i_c_k
n_i_c_k

Reputation: 1534

I have used the Devise, CanCan, and Rolify stack to do some admin stuff. It's more than what you need, but the benefits are awesome. A wiki on how to set it up here.

If you want an admin, first give all your users a role, for example "member", then also give your admins a role, like "admin".

Then you can use devise checks like current_user, and you can also use Rolify stuff like if @user.has_role? "admin"

It's pretty boss

Upvotes: 2

Related Questions