Rails 4 user roles and permissions

gdiazc picture gdiazc · Aug 12, 2014 · Viewed 23.5k times · Source

I am writing a rails application for an organization. Every user may have 1 or more roles and can only access certain controller actions depending on those roles.

For example, only admins can create, destroy and update certain fields of Users. Also, there are Teams which each have a team leader, and only the team leader can update certain information about the Team (like the member list, for example). However, Admins are the one who assign the team leader in the first place.

The specific details of my scenario are not important, I merely hope I described the situation where there are many different roles and permissions.

My question is: what gem to use? My first thought was CanCan, but the last commit was almost a year ago and there is no mention of Rails 4 compatibility. Is there a currently maintained alternative?

Answer

Andrey Deineko picture Andrey Deineko · Aug 12, 2014

Your first guess was right, use cancancan and you'll be good with it.

EDIT Jul 24, 2015

I've been using cancancan for a long time now and it was always working great. I've recently started working on a project where Pundit is used for authorization.

It is awesome. It prompts you to define the policy for each resource and it feels more natural than one bloated Ability class.

For bigger projects, I would definitely recommend Pundit.