-1
日增长
5,678
stars
627
forks
91
watchers
488
prs
433
issues
12
releases
README
CanCanCan
Developer guide |
RDocs |
Screencast 1 |
Screencast 2
CanCanCan is an authorization library for Ruby and Ruby on Rails which restricts what
resources a given user is allowed to access.
All permissions can be defined in one or multiple ability files and not duplicated across controllers, views,
and database queries, keeping your permissions logic in one place for easy maintenance and testing.
It consists of two main parts:
1. Authorizations library that allows you to define the rules to access different objects,
and provides helpers to check for those permissions.
2. Rails helpers to simplify the code in Rails Controllers by performing the loading and checking of permissions
of models automatically and reduce duplicated code.
Our sponsors
Do you want to sponsor CanCanCan and show your logo here?
Check our Sponsors Page.
Head to our complete Developer Guide to learn how to use CanCanCan in details.
Installation
Add this to your Gemfile:
gem 'cancancan'
and run the bundle install command.
CanCanCan 3.x is tested with Ruby 2.7+ and Active Record 5.2 to 8.1. Active Record is not a dependency of the gem, since abilities can be defined on any Ruby object, so Bundler cannot check its version for you.
Define Abilities
User permissions are defined in an Ability class.
rails g cancan:ability
Here follows an example of rules defined to read a Post model.
ruby
class Ability
include CanCan::Ability
def initialize(user)
can :read, Post, public: true
return unless user.present? additional permissions for logged in users (they can read their own posts)
can :read, Post, user: user
return unless user.admin? additional permissions for administrators
can :read, Post
end
end
Check Abilities
The current user's permissions can then be checked using the can? and cannot? methods in views and controllers.
erb
Fetching records
One of the key features of CanCanCan, compared to other authorization libraries,
is the possibility to retrieve all the objects that the user is authorized to access.
The following:
ruby
@posts = Post.accessible_by(current_ability)
will use your rules to ensure that the user retrieves only a list of posts that can be read.
Controller helpers
The authorize! method in the controller will raise an exception if the user is not able to perform the given action.
ruby
def show
@post = Post.find(params[:id])
authorize! :read, @post
end
Setting this for every action can be tedious, therefore the load_and_authorize_resource method is provided to
automatically authorize all actions in a RESTful style resource controller.
It will use a before action to load the resource into an instance variable and authorize it for every action.
ruby
class PostsController < ApplicationController
load_and_authorize_resource
def show
@post is already loaded and authorized
end
def index
@posts is already loaded with all posts the useTop issues
Discussions
#886Checking permissions on decorator in view0 comments · 2025-10-28#879cancancan with union join0 comments · 2025-04-14#864Custom override loading - How to handle errors?2 comments · 2024-09-05#859Access details about abilities programmatically, for generating documentation?1 comments · 2024-07-29#840How to authorize in multi-membership app?1 comments · 2024-03-13#834How to create a ability based on array values?0 comments · 2023-10-26#819CanCanCan Made Simple: Tips for New Users5 comments · 2023-02-23#784accessible_by strategies1 comments · 2022-05-02
Latest release
3.7.0 3.7.0 2026-10-14The one below is a list of all the PRs merged. For a more detailed update, read the blog post about this release. Merge the ControllerResourceLoader, ControllerResourceBuilder, ControllerResourceFinder, ControllerResourceNameFinder and ControllerResourceSanitizer modules into CanCan::ControllerResource, and call the params method of the controller only once per request: it was called once more for every attribute set by the conditions of the ability. WARNING: the five modules no longer exist; override their methods in a subclass of CanCan::ControllerResource instead. (@coorasse) Expand