Description
CanCan is an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access. All permissions are defined in a single location (the Ability class) and not duplicated across controllers, views, and database queries.
CanCanCan alternatives and similar gems
Based on the "Authorization" category.
Alternatively, view CanCanCan alternatives based on common mentions on social networks and blogs.
-
ruby_llm
The Ruby-native AI framework. Chats, agents, tools, images, audio, and video through one consistent API, in plain Ruby or Rails. -
Declarative Authorization
An unmaintained authorization plugin for Rails. Please fork to support current versions of Rails -
Awesome Ruby blogs
A curated list of Awesome Ruby Blogs and Newsletters for Ruby Developers and Newbies -
RatyRate Stars Rating Gem
:star: A Ruby Gem that wraps the functionality of jQuery Raty library, and provides optional IMDB style rating. -
Rabarber
Rabarber is a role-based authorization library for Ruby on Rails. It provides a set of tools for managing user roles and defining authorization rules, with support for multi-tenancy and fine-grained access control. -
Canard
Makes role based authorization in Rails really simple. Wraps CanCan and RoleModel up with a smattering of syntactic sugar, some generators and scopes. -
IslandJS Rails
IslandJS Rails supports the development of React (or other JS library) islands in Rails (7+) apps -
ActiveRegistration
A simple gem that adds generators for sign up with Rails Authentication Generator. -
Kno Ruby
DID is an Identity Provider, that authenticates users by verifying access to either an email address or securely stored private key. -
Rails-Domino
DISCONTINUED. Domain-first service and repository scaffold for Rails. Enforces DDD architecture with Blueprinter and Dry-rb libraries [GET https://api.github.com/repos/kiebor81/rails-domino: 404 - Not Found // See: https://docs.github.com/rest/repos/repos#get-a-repository]
AppSignal - Monitoring that respects your time & budget
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of CanCanCan or a related project?
README
CanCanCan
[Developer guide](./docs/README.md) | 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:
Authorizations library that allows you to define the rules to access different objects, and provides helpers to check for those permissions.
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](./docs/README.md) to learn how to use CanCanCan in details.
Installation
Add this to your Gemfile:
gem 'cancancan'
and run the bundle install command.
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.
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.
<% if can? :read, @post %>
<%= link_to "View", @post %>
<% end %>
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:
@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.
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.
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 user is authorized to read
end
end
Documentation
Head to our complete [Developer Guide](./docs/README.md) to learn how to use CanCanCan in details.
Questions?
If you have any question or doubt regarding CanCanCan which you cannot find the solution to in the [documentation](./docs/README.md), please open a question on Stackoverflow with tag cancancan
Bugs?
If you find a bug please add an issue on GitHub or fork the project and send a pull request.
Development
CanCanCan uses appraisals to test the code base against multiple versions of Rails, as well as the different model adapters.
When first developing, you need to run bundle install and then bundle exec appraisal install, to install the different sets.
You can then run all appraisal files (like CI does), with appraisal rake or just run a specific set DB='sqlite' bundle exec appraisal activerecord_5.2.2 rake.
If you use RubyMine, you can run RSpec tests by configuring the RSpec configuration template like this: [rubymine_rspec.png](rubymine_rspec.png)
See the [CONTRIBUTING](./CONTRIBUTING.md) for more information.
Special Thanks
Thanks to our Sponsors and to all the CanCanCan contributors. See the CHANGELOG for the full list.