Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

CanCanCan

Gem Version Github Actions badge Code Climate Badge

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


Pennylane


Honeybadger


Goboony


Renuo AG

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.

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 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, 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'd like to run a specific set of tests within a specific file or folder you can use DB='sqlite' SPEC=path/to/file/or/folder 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

See the CONTRIBUTING for more information.

Special Thanks

Thanks to our Sponsors and to all the CanCanCan contributors. See the CHANGELOG for the full list.

Ruby
Ruby
Ruby is a dynamic, object-oriented programming language known for its simplicity and productivity. It powers the Ruby on Rails framework, making it popular for web application development. Ruby emphasizes clean and readable code.
GitHub - TrestleAdmin/trestle: A modern, responsive admin framework for Ruby on Rails
GitHub - TrestleAdmin/trestle: A modern, responsive admin framework for Ruby on Rails
GitHub - ElMassimo/vite_ruby: ⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
GitHub - ElMassimo/vite_ruby: ⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
Rails Assets
Rails Assets
Avo
Avo
GitHub - mina-deploy/mina: Blazing fast deployer and server automation tool
GitHub - mina-deploy/mina: Blazing fast deployer and server automation tool
GitHub - rails/actionpack-action_caching: Action caching for Action Pack (removed from core in Rails 4.0)
GitHub - rails/actionpack-action_caching: Action caching for Action Pack (removed from core in Rails 4.0)
Awesome Ruby | LibHunt
Awesome Ruby | LibHunt
GitHub - palkan/action_policy: Authorization framework for Ruby/Rails applications
GitHub - palkan/action_policy: Authorization framework for Ruby/Rails applications
GitHub - pickhardt/betty: Friendly English-like interface for your command line. Don't remember a command? Ask Betty.
GitHub - pickhardt/betty: Friendly English-like interface for your command line. Don't remember a command? Ask Betty.
GitHub - metaskills/less-rails: :-1: Less.js For Rails
GitHub - metaskills/less-rails: :-1: Less.js For Rails
GitHub - codeplant/simple-navigation: A ruby gem for creating navigations (with multiple levels) for your Rails, Sinatra or Padrino applications.  Render your navigation as html list, link list or breadcrumbs.
GitHub - codeplant/simple-navigation: A ruby gem for creating navigations (with multiple levels) for your Rails, Sinatra or Padrino applications. Render your navigation as html list, link list or breadcrumbs.
GitHub - ruby/rake: A make-like build utility for Ruby.
GitHub - ruby/rake: A make-like build utility for Ruby.
GitHub - lazaronixon/authentication-zero: An authentication system generator for Rails applications.
GitHub - lazaronixon/authentication-zero: An authentication system generator for Rails applications.
GitHub - sorentwo/readthis: :newspaper: Pooled active support compliant caching with redis
GitHub - sorentwo/readthis: :newspaper: Pooled active support compliant caching with redis
GitHub - crepe/crepe: 🥞 The thin API stack.
GitHub - crepe/crepe: 🥞 The thin API stack.
GitHub - geemus/formatador: STDOUT text formatting
GitHub - geemus/formatador: STDOUT text formatting
GitHub - CanCanCommunity/cancancan: The authorization Gem for Ruby on Rails.
GitHub - CanCanCommunity/cancancan: The authorization Gem for Ruby on Rails.
GitHub - dry-rb/dry-cli: General purpose Command Line Interface (CLI) framework for Ruby
GitHub - dry-rb/dry-cli: General purpose Command Line Interface (CLI) framework for Ruby
The Ruby Toolbox - Know your options!
The Ruby Toolbox - Know your options!
GitHub - rharriso/bower-rails: Bundler-like DSL + rake tasks for Bower on Rails
GitHub - rharriso/bower-rails: Bundler-like DSL + rake tasks for Bower on Rails
GitHub - joshfrench/rakismet: Easy Akismet and TypePad AntiSpam integration for Rails
GitHub - joshfrench/rakismet: Easy Akismet and TypePad AntiSpam integration for Rails
GitHub - torba-rb/torba: Bundler for Sprockets
GitHub - torba-rb/torba: Bundler for Sprockets
GitHub - artsy/garner: A set of Rack middleware and cache helpers that implement various caching strategies.
GitHub - artsy/garner: A set of Rack middleware and cache helpers that implement various caching strategies.
GitHub - matthutchinson/acts_as_textcaptcha: Text-based logic question captcha's for Rails 🚫🤖
GitHub - matthutchinson/acts_as_textcaptcha: Text-based logic question captcha's for Rails 🚫🤖
GitHub - ai/autoprefixer-rails: Autoprefixer for Ruby and Ruby on Rails
GitHub - ai/autoprefixer-rails: Autoprefixer for Ruby and Ruby on Rails
GitHub - apneadiving/waterfall: A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow!
GitHub - apneadiving/waterfall: A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow!
GitHub - chaps-io/access-granted: Multi-role and whitelist based authorization gem for Rails (and not only Rails!)
GitHub - chaps-io/access-granted: Multi-role and whitelist based authorization gem for Rails (and not only Rails!)
MidiSmtpServer - brief profile
MidiSmtpServer - brief profile
GitHub - mdub/clamp: a Ruby command-line application framework
GitHub - mdub/clamp: a Ruby command-line application framework
GitHub - thoughtbot/administrate: A Rails engine that helps you put together a super-flexible admin dashboard.
GitHub - thoughtbot/administrate: A Rails engine that helps you put together a super-flexible admin dashboard.
Ruby
More on Ruby

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.