All Projects → CanCanCommunity → Cancancan

CanCanCommunity / Cancancan

Licence: mit
The authorization Gem for Ruby on Rails.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Cancancan

Consul
Scope-based authorization for Ruby on Rails.
Stars: ✭ 268 (-94.69%)
Mutual labels:  rails, authorization
Access Granted
Multi-role and whitelist based authorization gem for Rails (and not only Rails!)
Stars: ✭ 733 (-85.47%)
Mutual labels:  rails, authorization
Action policy
Authorization framework for Ruby/Rails applications
Stars: ✭ 718 (-85.77%)
Mutual labels:  rails, authorization
Simonsays
💂 Simple, declarative, role-based access control system for Rails and Ruby
Stars: ✭ 245 (-95.14%)
Mutual labels:  rails, authorization
graphql authorize
Authorization helpers for ruby-graphql fields
Stars: ✭ 23 (-99.54%)
Mutual labels:  authorization, cancancan
Banken
Simple and lightweight authorization library for Rails
Stars: ✭ 247 (-95.11%)
Mutual labels:  rails, authorization
Monarchy
Hierarchical access management system with advanced roles inheritance. 🦋
Stars: ✭ 48 (-99.05%)
Mutual labels:  rails, authorization
active admin role
Role based authorization with CanCanCan for Active Admin
Stars: ✭ 53 (-98.95%)
Mutual labels:  authorization, cancancan
Six
Ultra lite authorization library
Stars: ✭ 323 (-93.6%)
Mutual labels:  rails, authorization
Graphql Ruby
Ruby implementation of GraphQL
Stars: ✭ 4,931 (-2.28%)
Mutual labels:  rails
Rails email preview
Preview and edit app mailer templates in Rails.
Stars: ✭ 524 (-89.62%)
Mutual labels:  rails
Kails
A Web App like Ruby on Rails with Koa2, Webpack and Postgres
Stars: ✭ 512 (-89.85%)
Mutual labels:  rails
Letter opener web
A web interface for browsing Ruby on Rails sent emails
Stars: ✭ 513 (-89.83%)
Mutual labels:  rails
Opa
An open source, general-purpose policy engine.
Stars: ✭ 5,939 (+17.7%)
Mutual labels:  authorization
Zero downtime migrations
Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
Stars: ✭ 513 (-89.83%)
Mutual labels:  rails
Serviceworker Rails
Use Service Worker with the Rails asset pipeline
Stars: ✭ 535 (-89.4%)
Mutual labels:  rails
Inline svg
Embed SVG documents in your Rails views and style them with CSS
Stars: ✭ 510 (-89.89%)
Mutual labels:  rails
Acts as api
makes creating API responses in Rails easy and fun
Stars: ✭ 506 (-89.97%)
Mutual labels:  rails
Annict
The platform for anime addicts built with Rails and Stimulus.js.
Stars: ✭ 542 (-89.26%)
Mutual labels:  rails
Autolab
Course management service that enables auto-graded programming assignments.
Stars: ✭ 528 (-89.54%)
Mutual labels:  rails

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


Renuo AG


Modern Treasury


Bullet Train


Goboony


NewRelic


InCloudCounsel


Honeybadger

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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].