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.76k
    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

    1. gem "rolify"
    2. gem "devise"
    3. gem "cancan"

    2. Now run bundle

    1. bundle install

    3. Now run devise command

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

    4. Create the user model using devise

    1. rails generate devise User
    2. ## this will create these files
    3. invoke active_record
    4. create db/migrate/20160507180200_devise_create_users.rb
    5. create app/models/user.rb
    6. invoke test_unit
    7. create test/models/user_test.rb
    8. create test/fixtures/users.yml
    9. insert app/models/user.rb
    10. route devise_for :users

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

    1. rails generate rolify Role User
    2. ## this will create these files
    3. invoke active_record
    4. create app/models/role.rb
    5. invoke test_unit
    6. create test/models/role_test.rb
    7. create test/fixtures/roles.yml
    8. insert app/models/role.rb
    9. create db/migrate/20160507180239_rolify_create_roles.rb
    10. insert app/models/user.rb
    11. create config/initializers/rolify.rb

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

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

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

    1. rake db:migrate

    8. Now for configuring devise you can go through the following link.
    https://github.com/plataformatec/devise

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

    1. user = User.find(1)
    2.  
    3. ## For adding role
    4. user.add_role :admin
    5.  
    6. ## For checking whether a user has some role
    7. user.has_role? :admin
    8.  
    9. ## For removing role from a user
    10. 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

    1. :optional => true

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

    1. if user.has_role? :admin
    2. can :manage, :all
    3. else
    4. can :read, :all
    5. end

    For more info regarding cancan you can go to this link.
    https://github.com/CanCanCommunity/cancancan

    Hope you liked this blog.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: