lolix
lolix

Reputation: 1527

manage roles in rails

I want create roles in my project. Each user can be: admin, registered or demo. Each role see different things. How can I do that? What is the best gem to do roles?

This is a example in 'bad programming" of what I want:

  def index
    if current_user.role[:name] == 'admin'
      @installations = Installation.all
    elsif current_user.role[:name] == 'registered'
      @installations = current_user.installations
    elsif current_user.role[:name] == 'demo'
      @installations = current_user.installations.first
    else
    end
  end

Upvotes: 1

Views: 7354

Answers (4)

Pholochtairze
Pholochtairze

Reputation: 1854

Some gems that might be interesting for you :

If you decide to implement it yourself, then within some page you might want to change the content, for that you might want to do something like this :

Add a role to the user model using a migration :

class AddRoleToUsers < ActiveRecord::Migration
  def change
    add_column :users, :role, :string, default: :demo
  end
end

Then in your app you can use it as follows:

def index
  case current_user.role
    when :admin
      @installations = Installation.all
    when :registered
      @installations = current_user.installations
    else 
      @installations = current_user.installations.first
  end
end 

You can also simply create a boolean admin for instance.

What you might want to do also is create some methods in your model so that you can call current_user.admin? or current_user.registered? . You can do that by doing (if you chose to use a string to store the role):

class User < ActiveRecord::Base
  def admin?
    self.role == "admin"
  end

  def registered?
    self.role == "registered"
  end
end

One advantage I see of having a role stored in a string is that if you have 5 roles for instance then you do not have 4 booleans (as when you store admin in a boolean) but only one string. On the long run you might want to store actually a role_id instead of a string and have a separate role model.

An excellent alternative pointed out by Jorge de Los Santos (another answer) is to use enum :

class User < ActiveRecord::Base
  enum role: [:demo, :admin, :registered]
end

It is an excellent alternative because it will automagically add the methods described above such as current_user.admin? without hard coding them.

With your roles, you might want to do some authorization (admins can have access to specific pages, demo users are restricted to only a subset of pages, etc.). For this, you can use the gem called cancancan. You can look at this railscast to learn more about it. Also, you can have some infos here : How to use cancancan? .

Upvotes: 3

Jorge de los Santos
Jorge de los Santos

Reputation: 4633

There are plenty of solutions available to you.

Starting by gems:

https://github.com/RolifyCommunity/rolify

https://github.com/martinrehfeld/role_model

By using Devise architecture (in case you use it):

https://github.com/plataformatec/devise/wiki/How-To:-Add-a-default-role-to-a-User

By using enums in rails 4:

class AddRolesToUser < ActiveRecord::Migration
  #add_column 'role', :integer, default: 0 to the users table
end

class User < ActiveRecord::Base
  enum role: [:demo, :admin, :registered]
end

That will enable role methods.

user = User.find(1)
user.role #:demo
user.admin? #false
user.registered? #false

And consequently:

if user.admin?
  #somethig
elsif user.registered?
  #another something
else
  #another another something.

And last but not least, what you are searching is not the manage roles solution, is the manage permissions solutions:

https://github.com/ryanb/cancan

Upvotes: 2

Michael Gaskill
Michael Gaskill

Reputation: 8042

The CanCan gem adds authorization to your project, and is especially useful if you want to implement multiple roles with differing abilities. When used with an authentication system like devise, you get a full suite of capability for your site.

You're in full control of what roles you want to define and what abilities they have. CanCan manages tracking, assignment, and querying of roles, and then gets out of your way to let you build what you need.

You can find the CanCan gem in Github: https://github.com/ryanb/cancan

It's simple to use, and the documentation is straightforward and easy to follow.

Upvotes: 0

Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

Add a boolean, :admin to your User model.

class AddAdminToUsers < ActiveRecord::Migration
  def change
    add_column :users, :admin, :boolean, deafult: false
  end
end

Create a method for a registered user to separate them from demo users, such as verifying their email, providing a home address and phone number, filling out a profile, etc. This is up to you though, first you need to decide how a registered and demo user should be different.

Upvotes: 0

Related Questions