Pundit is a tiny Rails authorization library that will make you jump for joy after using the likes of CanCan.
Strictly speaking, Pundit it a small, unopinionated library of helper methods designed to make it easier to interface with Policy objects (classes that define your application’s authorization logic). It’s explicit, easy to understand, easy to use, and powerful.
It’s everything an authorization library should be.
Policy Objects
Pundit’s only strict opinion is that authorization should be performed by discrete objects, called policies. Policy objects make a ton on sense (even if you aren’t using Pundit). Their goal is to completely isolate and centralize all authorization logic in your app. (Sayonara to authorization logic in your views.) A policy object and its use could be as simple as:
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Policy</span></span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initialize</span><span class="hljs-params">(user)</span></span>
<span class="hljs-variable">@user</span> = user
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">authorized?</span></span>
user.is_admin?
<span class="hljs-keyword">end</span>
private
<span class="hljs-keyword">attr_reader</span> <span class="hljs-symbol">:user</span>
<span class="hljs-keyword">end</span>
<span class="hljs-constant">Policy</span>.new(current_user).authorized?
Obviously this example is simplistic. In real applications, authorization logic can pile up and become difficult to organize. Pundit’s ease-of-use comes from its suggestion that policies be organized RESTfully as described below.
Naming + Inference
Pundit policies are named after the models for which they authorize actions. A policy for authorizing actions pertaining to the BlogPost model would be named BlogPostPolicy. This is for convenient inference of the appropriate policy in the contexts of a controller or view. Policies aren’t handcuffed to model names though. You can still create “headless” policies that aren’t tied to any specific model, and you can always use policies with models (less convienently) while breaking the naming convention… although at that point you wouldn’t really be using Pundit anymore.
An example of a policy directory: 
Policy Actions
Pundit suggests that your policies’ public API consist of predicate methods conventionally named after the REST action they authorize. (But of course you can customize to your liking.)
#index?#show?#new?#create?#edit?#update?#destroy?
Within policy action methods, you have access to user and record. The user—current_user if using the #authorize helper from a controller (some more overridable inference)—is the user that is trying to perform the action. The record is the item to which the action is being applied.
Example
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArticlePolicy</span> <span class="hljs-inheritance">< <span class="hljs-parent">ApplicationPolicy</span></span></span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index?</span></span>
<span class="hljs-keyword">true</span>
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show?</span></span>
<span class="hljs-comment"># More about this in the following examples...</span>
scope.where(<span class="hljs-symbol">id:</span> record.id).exists?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create?</span></span>
is_contributor?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new?</span></span>
create?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update?</span></span>
is_editor?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">edit?</span></span>
update?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">destroy?</span></span>
is_editor?
<span class="hljs-keyword">end</span>
private
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">is_editor?</span></span>
user.editor?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">is_contributor?</span></span>
is_editor? || user.author?
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
Use in a Controller
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">edit</span></span>
authorize(article)
<span class="hljs-comment"># :point_up: is essentially the same as :point_down: just more convenient</span>
<span class="hljs-constant">ArticlePolicy</span>.new(current_user, article).edit? || raise <span class="hljs-constant">Pundit::NotAuthorizedError</span>
<span class="hljs-keyword">end</span>
private
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">article</span></span>
<span class="hljs-variable">@article</span> ||= <span class="hljs-constant">Article</span>.find(params[<span class="hljs-symbol">:id</span>])
<span class="hljs-keyword">end</span>
Use in a View
<%-<span class="ruby"> <span class="hljs-keyword">if</span> policy(<span class="hljs-symbol">:articles</span>).new? </span>-%><span class="xml">
</span><%<span class="ruby"> content_for <span class="hljs-symbol">:page__nav_left</span> <span class="hljs-keyword">do</span> </span>%><span class="xml">
</span><%=<span class="ruby"> link_to <span class="hljs-string">"New Article"</span>, new_article_path </span>%><span class="xml">
</span><%<span class="ruby"> <span class="hljs-keyword">end</span> </span>%><span class="xml">
</span><%-<span class="ruby"> <span class="hljs-keyword">end</span> </span>-%><span class="xml">
</span> Policy Scope
Simple true-or-false-returning authorization methods work fine for most actions. A user can either edit a record or they can’t. But some Actions (like viewing an index of records) require more complex authorization. For these cases, Pundit uses the notion of authorized scopes to provide the granularity you need. An authorized scope is just an authorization-based limitation to the default scope of a particular model.
Authorized scopes are defined as a class with a single instance method: #resolve. In this method, you need simply to restrict the scope to that allowed for the given user.
Example
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArticlePolicy</span> <span class="hljs-inheritance">< <span class="hljs-parent">ApplicationPolicy</span></span></span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Scope</span> <span class="hljs-inheritance">< <span class="hljs-parent">ApplicationScope</span></span></span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">resolve</span></span>
<span class="hljs-comment"># Only contributors can see unpublished Articles</span>
is_contributor? ? scope <span class="hljs-symbol">:</span> scope.published
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show?</span></span>
<span class="hljs-comment"># Only show the given record if it is within the authorized scope.</span>
scope.where(<span class="hljs-symbol">id:</span> record.id).exists?
<span class="hljs-keyword">end</span>
<span class="hljs-comment"># ...</span>
private
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">is_editor?</span></span>
user.editor?
<span class="hljs-keyword">end</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">is_contributor?</span></span>
is_editor? || user.author?
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
Use in Controller
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span></span>
<span class="hljs-keyword">end</span>
private
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">articles</span></span>
policy_scope(<span class="hljs-constant">Article</span>)
<span class="hljs-comment"># You can also chain conditions onto the return of #policy_scope</span>
policy_scope(<span class="hljs-constant">Article</span>).where(<span class="hljs-symbol">author_id:</span> current_user.id).order(<span class="hljs-symbol">published_at:</span> <span class="hljs-symbol">:desc</span>)
<span class="hljs-keyword">end</span>
Ensuring Authorization
Pundit provides two methods that can ensure that authorization methods have been called for the current request:
after_action <span class="hljs-symbol">:verify_authorized</span>, <span class="hljs-symbol">except:</span> <span class="hljs-symbol">:index</span>
after_action <span class="hljs-symbol">:verify_policy_scoped</span>, <span class="hljs-symbol">only:</span> <span class="hljs-symbol">:index</span>
Liberal use of these methods protects you from accidentally omitting authorization where it should occur.
Testing
Since policy object are just POROs, they’re easy to test. And when your authorization logic is backed by rock-solid tests, you sleep better at night.
Example
<span class="hljs-keyword">require</span> <span class="hljs-string">"rails_helper"</span>
describe <span class="hljs-constant">ArticlePolicy</span> <span class="hljs-keyword">do</span>
it_behaves_like <span class="hljs-string">"an admin-only permissions policy"</span>
describe <span class="hljs-symbol">described_class:</span><span class="hljs-symbol">:Scope</span> <span class="hljs-keyword">do</span>
let(<span class="hljs-symbol">:user</span>) { create(<span class="hljs-symbol">:user</span>) }
let!(<span class="hljs-symbol">:article_0</span>) { create(<span class="hljs-symbol">:article</span>) }
let!(<span class="hljs-symbol">:article_1</span>) { create(<span class="hljs-symbol">:unpublished_article</span>) }
subject <span class="hljs-keyword">do</span>
described_class.new(user, <span class="hljs-constant">Article</span>)
<span class="hljs-keyword">end</span>
describe <span class="hljs-string">"#resolve"</span> <span class="hljs-keyword">do</span>
context <span class="hljs-string">"when the user is an editor"</span> <span class="hljs-keyword">do</span>
before <span class="hljs-keyword">do</span>
user.update_attributes(<span class="hljs-symbol">role:</span> <span class="hljs-symbol">:editor</span>)
<span class="hljs-keyword">end</span>
it <span class="hljs-string">"returns an unconstrained scope"</span> <span class="hljs-keyword">do</span>
[article_<span class="hljs-number">0</span>, article_1].each <span class="hljs-keyword">do</span> |article|
expect(subject.resolve).to <span class="hljs-keyword">include</span>(article)
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
context <span class="hljs-string">"when the user is an author"</span> <span class="hljs-keyword">do</span>
before <span class="hljs-keyword">do</span>
user.update_attributes(<span class="hljs-symbol">role:</span> <span class="hljs-symbol">:author</span>)
<span class="hljs-keyword">end</span>
it <span class="hljs-string">"returns an unconstrained scope"</span> <span class="hljs-keyword">do</span>
[article_<span class="hljs-number">0</span>, article_1].each <span class="hljs-keyword">do</span> |article|
expect(subject.resolve).to <span class="hljs-keyword">include</span>(article)
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
context <span class="hljs-string">"when the user is not a contributor"</span> <span class="hljs-keyword">do</span>
before <span class="hljs-keyword">do</span>
user.update_attributes(<span class="hljs-symbol">role:</span> <span class="hljs-symbol">:normal_person</span>)
<span class="hljs-keyword">end</span>
it <span class="hljs-string">"returns a scope including published articles"</span> <span class="hljs-keyword">do</span>
expect(subject.resolve).to <span class="hljs-keyword">include</span>(article_<span class="hljs-number">0</span>)
<span class="hljs-keyword">end</span>
it <span class="hljs-string">"returns a scope excluding unpublished articles"</span> <span class="hljs-keyword">do</span>
expect(subject.resolve).to_not <span class="hljs-keyword">include</span>(article_1)
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
Flexibility & Code Reuse
One of the greatest benefits of well-implemented use of an authorization library like Pundit is the ability to reuse code, especially in multi-user role context. In many multi-role apps, controllers become a duplicative mess of variants that differ only by the inclusion of a particular action, or the restriction of a certain scope. When you offload all authorization logic to policies, you’re able to write a single, generic version of a controller, and reuse it for every role without fear of exposing unauthorized information or abilities.
<span class="hljs-class"><span class="hljs-keyword">module</span> <span class="hljs-title">Editor</span></span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ArticlesController</span> <span class="hljs-inheritance">< <span class="hljs-parent">Editor::Controller</span></span></span>
<span class="hljs-keyword">include</span> <span class="hljs-constant">ArticleManagement</span> <span class="hljs-comment"># This includes generic actions for reuse with all roles.</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
And with this reuse (and if you’re using Pundit view helpers to appropriately hide UI based on authorization), when authorization logic eventually needs to change, a simple change to the policy is all that’s required. It’s difficult to express how awesome it feels to make a one-line update that fundamentally changes how a user can interact with the app.
In Closing
Authorization is an important aspect of nearly every modern web application. And when it comes to the security of your app, it’s critical that you understand exactly what is going on behind the scenes. Pundit is so lightweight, developing a full understanding of its inner workings is a trivial affair. And despite its small size, Pundit still provides powerful, easy-to-use tools that can dramatically improve your application.
So next time you reach for an authorization library, give Pundit a shot.






