Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Using rolify devise and cancan gem together for user authorizations and access in rails

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 4.44k
    Comment on it

    Hi friends,
    Whenever we create an application, there is always a case where we want to have different kind of users who needs to login into the app but they have different access rights, so that some functionalities are hidden for some users and some can have access to them. In rails we have different kinds of gems available that are used for making these functionalities in easy way. Here I am going to discuss few of them.

    1. devise gem: Used for authentication and login

    2. rolify gem: Used for creating different kinds of roles and assigning them to users

    3. cancancan gem: Used for defining abilities and access to users with different kinds of roles

    Now we will discuss how we can use them:

    1. Add these gems to Gemfile

       gem "rolify"
       gem "devise"
       gem "cancan"

    2. Now run bundle

       bundle install

    3. Now run devise command

       rails generate devise:install
       ## this will create these files
       create  config/initializers/devise.rb
       create  config/locales/devise.en.yml

    4. Create the user model using devise

       rails generate devise User
       ## this will create these files
       invoke  active_record
       create    db/migrate/20160507180200_devise_create_users.rb
       create    app/models/user.rb
       invoke    test_unit
       create      test/models/user_test.rb
       create      test/fixtures/users.yml
       insert    app/models/user.rb
       route  devise_for :users

    5. For creating roles for users we need to use rolify

       rails generate rolify Role User
       ## this will create these files
       invoke  active_record
       create    app/models/role.rb
       invoke    test_unit
       create      test/models/role_test.rb
       create      test/fixtures/roles.yml
       insert    app/models/role.rb
       create    db/migrate/20160507180239_rolify_create_roles.rb
       insert  app/models/user.rb
       create  config/initializers/rolify.rb

    6. Now define the abilities we need to create ability class using cancan

       rails generate cancan:ability
       ## this will create these files
       create  app/models/ability.rb

    7. Now run migrations to create these tables like role, users etc

       rake db:migrate

    8. Now for configuring devise you can go through the following link.

    9. Now you can create roles and assign to them using rolify gem. you can use these roles like

      user = User.find(1)
      ## For adding role 
      user.add_role :admin
      ## For checking whether a user has some role
      user.has_role? :admin
      ## For removing role from a user
      user.remove_role :admin

    Sometimes on adding roles to a user you will get error, for solving that error, you need to remove this line from the role.rb

       :optional => true

    For more info regarding rolify you can go to this link.
    10. Now at last for defining abilities, you can set it in ability.rb file. Like this

        if user.has_role? :admin
          can :manage, :all
          can :read, :all

    For more info regarding cancan you can go to this link.

    Hope you liked this blog.

 1 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: