{"id":7562,"date":"2015-07-31T01:59:32","date_gmt":"2015-07-31T01:59:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/07\/31\/ryanb-cancan\/"},"modified":"2015-07-31T01:59:32","modified_gmt":"2015-07-31T01:59:32","slug":"ryanb-cancan","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/07\/31\/ryanb-cancan\/","title":{"rendered":"ryanb\/cancan"},"content":{"rendered":"<p>= CanCan {<img decoding=\"async\" src=\"http:\/\/fury-badge.herokuapp.com\/rb\/cancan.png\" \/>}[http:\/\/badge.fury.io\/rb\/cancan] {<img decoding=\"async\" src=\"http:\/\/secure.travis-ci.org\/ryanb\/cancan.png?branch=master\" \/>}[http:\/\/travis-ci.org\/ryanb\/cancan] {<img decoding=\"async\" src=\"http:\/\/codeclimate.com\/github\/ryanb\/cancan.png\" \/>}[https:\/\/codeclimate.com\/github\/ryanb\/cancan]<\/p>\n<p>Wiki[https:\/\/github.com\/ryanb\/cancan\/wiki] | RDocs[http:\/\/rdoc.info\/projects\/ryanb\/cancan] | Screencast[http:\/\/railscasts.com\/episodes\/192-authorization-with-cancan]<\/p>\n<p>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.<\/p>\n<p>== Installation<\/p>\n<p>In <b>Rails 3<\/b>, add this to your Gemfile and run the +bundle+ command.<\/p>\n<p>gem \u201ccancan\u201d<\/p>\n<p>In <b>Rails 2<\/b>, add this to your environment.rb file.<\/p>\n<p>config.gem \u201ccancan\u201d<\/p>\n<p>Alternatively, you can install it as a plugin.<\/p>\n<p>rails plugin install git:\/\/github.com\/ryanb\/cancan.git<\/p>\n<p>== Getting Started<\/p>\n<p>CanCan expects a +current_user+ method to exist in the controller. First, set up some authentication (such as Authlogic[https:\/\/github.com\/binarylogic\/authlogic] or Devise[https:\/\/github.com\/plataformatec\/devise]). See {Changing Defaults}[https:\/\/github.com\/ryanb\/cancan\/wiki\/changing-defaults] if you need different behavior.<\/p>\n<p>=== 1. Define Abilities<\/p>\n<p>User permissions are defined in an +Ability+ class. CanCan 1.5 includes a Rails 3 generator for creating this class.<\/p>\n<p>rails g cancan:ability<\/p>\n<p>In Rails 2.3, just add a new class in <code>app\/models\/ability.rb<\/code> with the following contents:<\/p>\n<p>class Ability include CanCan::Ability<\/p>\n<pre><code>def initialize(user)\nend\n<\/code><\/pre>\n<p>end<\/p>\n<p>See {Defining Abilities}[https:\/\/github.com\/ryanb\/cancan\/wiki\/defining-abilities] for details.<\/p>\n<p>=== 2. Check Abilities &amp; Authorization<\/p>\n<p>The current user\u2019s permissions can then be checked using the can? and cannot? methods in the view and controller.<\/p>\n<p>See {Checking Abilities}[https:\/\/github.com\/ryanb\/cancan\/wiki\/checking-abilities] for more information<\/p>\n<p>The authorize! method in the controller will raise an exception if the user is not able to perform the given action.<\/p>\n<p>def show @article = Article.find(params[:id]) authorize! :read, @article end<\/p>\n<p>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 filter to load the resource into an instance variable and authorize it for every action.<\/p>\n<p>class ArticlesController &lt; ApplicationController load_and_authorize_resource<\/p>\n<pre><code>def show\n  # @article is already loaded and authorized\nend\n<\/code><\/pre>\n<p>end<\/p>\n<p>See {Authorizing Controller Actions}[https:\/\/github.com\/ryanb\/cancan\/wiki\/authorizing-controller-actions] for more information.<\/p>\n<p>=== 3. Handle Unauthorized Access<\/p>\n<p>If the user authorization fails, a CanCan::AccessDenied exception will be raised. You can catch this and modify its behavior in the +ApplicationController+.<\/p>\n<p>class ApplicationController &lt; ActionController::Base rescue_from CanCan::AccessDenied do |exception| redirect_to root_url, :alert =&gt; exception.message end end<\/p>\n<p>See {Exception Handling}[https:\/\/github.com\/ryanb\/cancan\/wiki\/exception-handling] for more information.<\/p>\n<p>=== 4. Lock It Down<\/p>\n<p>If you want to ensure authorization happens on every action in your application, add +check_authorization+ to your ApplicationController.<\/p>\n<p>class ApplicationController &lt; ActionController::Base check_authorization end<\/p>\n<p>This will raise an exception if authorization is not performed in an action. If you want to skip this add +skip_authorization_check+ to a controller subclass. See {Ensure Authorization}[https:\/\/github.com\/ryanb\/cancan\/wiki\/Ensure-Authorization] for more information.<\/p>\n<p>== Wiki Docs<\/p>\n<p>== Questions or Problems?<\/p>\n<p>If you have any issues with CanCan which you cannot find the solution to in the documentation[https:\/\/github.com\/ryanb\/cancan\/wiki], please add an {issue on GitHub}[https:\/\/github.com\/ryanb\/cancan\/issues] or fork the project and send a pull request.<\/p>\n<p>To get the specs running you should call +bundle+ and then +rake+. See the {spec\/README}[https:\/\/github.com\/ryanb\/cancan\/blob\/master\/spec\/README.rdoc] for more information.<\/p>\n<p>== Special Thanks<\/p>\n<p>CanCan was inspired by declarative_authorization[https:\/\/github.com\/stffn\/declarative_authorization\/] and aegis[https:\/\/github.com\/makandra\/aegis]. Also many thanks to the CanCan contributors[https:\/\/github.com\/ryanb\/cancan\/contributors]. See the CHANGELOG[https:\/\/github.com\/ryanb\/cancan\/blob\/master\/CHANGELOG.rdoc] for the full list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>= CanCan {}[http:\/\/badge.fury.io\/rb\/cancan] {}[http:\/\/travis-ci.org\/ryanb\/cancan] {}[https:\/\/codeclimate.com\/github\/ryanb\/cancan] Wiki[https:\/\/github.com\/ryanb\/cancan\/wiki] | RDocs[http:\/\/rdoc.info\/projects\/ryanb\/cancan] | Screencast[http:\/\/railscasts.com\/episodes\/192-authorization-with-cancan] 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. == Installation In Rails 3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7562","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7562"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7562\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}