Search code examples
authenticationruby-on-rails-5

Easy way to let only admin manage users


I need to let only the admin user to create and edit all users. I'm using Devise (but I can change gem if needed) and I tried a lot of suggetions from stackoverflow and the web. I hasn't able to get any of those examples/suggestions to work.

Is there an EASY way to have and admin panel with which only the administrator can create and edit users, while all "simple" users can manage the rest of the app (after authentication)?

I'm using RAILS 5 Please let me know if I have to give more informations.

thanks


Solution

  • You can use Rolify and CanCanCan gems to define roles and ability for users. And specify that only role admin has the ability to modify the records and other roles can only read the records .

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

    This tutorial has in detail https://github.com/RolifyCommunity/rolify/wiki/Devise---CanCanCan---rolify-Tutorial