implement-authorization
Skill26 starsChanged 4 months agoMITView on GitHub
Use when adding or reviewing Rails authorization with Pundit, CanCanCan, or policy objects. Trigger words: authorization, Pundit, CanCanCan, policy, roles, permissions.
In this repository32 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- apply-code-conventionsSkill
- apply-stack-conventionsSkill
- code-reviewSkill
- create-engine-installerSkill
- create-engineSkill
- document-engineSkill
- extract-engineSkill
- generate-api-collectionSkill
- implement-background-jobSkill
- implement-graphqlSkill
- implement-hotwireSkill
- load-contextSkill
- optimize-performanceSkill
- plan-testsSkill
- rails-featureSkill
- rails-maintenanceSkill
- rails-reviewSkill
- refactor-codeSkill
- release-engineSkill
- review-architectureSkill
- review-engineSkill
- review-migrationSkill
- security-checkSkill
- seed-databaseSkill
- setup-environmentSkill
- test-engineSkill
- test-serviceSkill
- upgrade-engineSkill
- version-apiSkill
- write-testsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 101
When implementing or reviewing authorization, the output answer.md must include:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Implement Authorization
Quick Reference
| Gem | Pattern | Best For |
| Pundit | Explicit policy classes | Complex per-resource rules |
| CanCanCan | Centralized Ability class | Simple role-based permissions |
Core Process
Implementation Workflow
- Inspect existing authorization — reuse the installed policy framework; add
punditorcancancanonly if the task requires a new framework and project conventions permit it - Generate base only when needed — run the gem's installer (
rails g pundit:installorrails g cancan:ability) only when no supported framework is present and the project permits the generator; otherwise proceed with the detected framework without invoking generators - Define policies/abilities — create policy classes (Pundit) or populate the Ability class (CanCanCan); always use policy objects, never inline authorization logic in controllers
- Authorize in controllers — call
authorize @record(Pundit) orauthorize! :action, @record(CanCanCan) in each action - Verify authorization — attempt an unauthorized action in the browser or console and confirm it raises
Pundit::NotAuthorizedErrororCanCan::AccessDeniedas expected; use persisted records (e.g.,User.create!) not unsaved ones - Scope queries — use
policy_scope(Model)oraccessible_by(current_ability)for index actions - Test all roles — write policy specs and request specs covering admin, owner, and guest; check specific permissions, never presence checks alone
Patterns
Pundit
class PostPolicy < ApplicationPolicy
def update?
user.admin? || record.user_id == user.id
end
endCanCanCan
class Ability
include CanCan::Ability
def initialize(user)
can :update, Post, user_id: user.id
can :manage, :all if user.admin?
end
endTroubleshooting
| Error | Likely Cause | Fix |
Pundit::NotDefinedError | No policy class found for the record | Create app/policies/model_policy.rb inheriting from ApplicationPolicy |
Pundit::AuthorizationNotPerformedError | authorize not called in a controller action | Add authorize @record in the action, or after_action :verify_authorized to catch misses |
CanCan::AccessDenied unexpectedly raised | Ability rules not matching the current user/role | Inspect current_ability.can?(:action, @record) in the console to debug rule evaluation |
Testing
Cover every role (admin, owner, guest) in both policy specs and request specs.
Minimal Pundit policy spec
RSpec.describe PostPolicy do
subject { described_class.new(user, post) }
let(:post) { create(:post, user: owner) }
let(:owner) { create(:user) }
context 'as admin' do
let(:user) { create(:user, :admin) }
it { is_expected.to permit_action(:update) }
end
context 'as owner' do
let(:user) { owner }
it { is_expected.to permit_action(:update) }
end
context 'as guest' do
let(:user) { create(:user) }
it { is_expected.not_to permit_action(:update) }
end
endOutput Style
When implementing or reviewing authorization, the output answer.md must include:
- Manual Denied-Action Verification — a dedicated section with actual Rails console output, or an explicit unrun status showing the authorization exception raised when an unauthorized action is attempted. Always use persisted records (
User.create!,Post.create!), never unsaved ones. - HTTP and Policy Verification — concrete
curlrequests or controller test commands with expected HTTP response codes (e.g.403 Forbiddenor302 Found) when access is denied. - Language — English unless explicitly requested otherwise.
See references/output-style.md for full formatting examples including Pundit and CanCanCan console output templates.
Integration
| Skill | When to chain |
| write-tests | When implementing authorization tests. |
Extended Resources (Progressive Disclosure)
Load these files only when their specific content is needed:
- EXAMPLES.md — Use when you need complete Pundit or CanCanCan implementation examples beyond the inline samples
- references/workflow.md — Use when you need the step-by-step authorization implementation workflow diagram
- references/output-style.md — Use when you need full formatting templates for console verification output
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

