The authorization Gem for Ruby on Rails
open sourceMITself-hostablefree
CanCanCan is an authorization library for Ruby and Rails that keeps permissions in one or more Ability classes: rules like can :read, Post, user: user are checked with can? and authorize! in controllers and views, and accessible_by turns the same rules into a database query that fetches only the records a user may access. load_and_authorize_resource loads and authorizes the resource for each action of a RESTful controller, and permitted_attributes says which attributes a user may change. It continues Ryan Bates's CanCan; Alessandro Rodi of Renuo has maintained it since 2015, and releases came again in October and November 2026 (3.6.2 to 3.7.1) after a gap of more than two years.
Problems it solves
- define all my Rails app's permissions in one place instead of scattered across controllers
- fetch only the records the current user is allowed to see, straight from the database
- admins can manage everything, editors only their own posts: role-based rules in Rails
- hide edit and delete links in views from users who can't use them
- load and authorize the record in every RESTful controller action without repeating code
- show a friendly page when someone opens something they aren't allowed to
- let a client change user permissions stored in the database without redeploying
- limit which fields of a record each user is allowed to update
- test my authorization rules in RSpec without going through controllers
- check permissions on nested resources like a project's tasks
- translate access denied messages for each language
Features
- can and cannot rules in Ability classes, split across files if needed
- can? and cannot? checks in controllers and views
- authorize! raising CanCan::AccessDenied
- accessible_by to fetch authorized records with SQL, with selectable SQL strategies
- load_and_authorize_resource for RESTful and nested resources
- hash conditions, block conditions and scopes
- permitted_attributes and disallowed_attributes
- action aliases and rules compression
- model adapter system: Active Record built in, custom adapters
- RSpec matchers and testing guide
View 1 more
- I18n for unauthorized messages
Limitations
- Block conditions can't fetch records: accessible_by raises an exception for a rule defined only by a Ruby block, unless you add SQL1
Capabilities
| Has an API | plan not known |
|---|
Alternatives
The same job
These do what CanCanCan does.
- Pundit Minimal authorization through OO design and pure Ruby classes
- Apache Casbin An authorization library that supports access control models like ACL, RBAC, ABAC
