mcpField
I
implement-authorization

implement-authorization

github.com/igmarinSecurity2839 installsUpdated 4w ago
Repository

About implement-authorization

implement-authorization is a skill for Rails developers implementing or reviewing authorization with Pundit or CanCanCan. It emphasizes reusing the existing framework, policy objects, scoped queries, and checks across user roles.

Key features

  1. 01Detect and reuse the existing authorization framework
  2. 02Use Pundit policies or CanCanCan Ability
  3. 03Authorize controller actions
  4. 04Scope index queries to accessible records
  5. 05Test policies and requests across roles
  6. 06Document denied-action verification

Use cases

  1. 01Implement role-based authorization for Rails resources
  2. 02Review controllers for missing authorization calls
  3. 03Limit index pages to accessible records
  4. 04Verify denied requests and their HTTP responses

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/igmarin/rails-agent-skills --skill implement-authorization

After installing, type/implement-authorizationin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

EXAMPLES.md
references/
output-style.md
workflow.md

Details

From the project README

Implement Authorization

Quick Reference

GemPatternBest For
PunditExplicit policy classesComplex per-resource rules
CanCanCanCentralized Ability classSimple role-based permissions

Core Process

Implementation Workflow
  1. Inspect existing authorization — reuse the installed policy framework; add pundit or cancancan only if the task requires a new framework and project conventions permit it
  2. Generate base only when needed — run the gem's installer (rails g pundit:install or rails 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
  3. Define policies/abilities — create policy classes (Pundit) or populate the Ability class (CanCanCan); always use policy objects, never inline authorization logic in controllers
  4. Authorize in controllers — call authorize @record (Pundit) or authorize! :action, @record (CanCanCan) in each action
  5. Verify authorization — attempt an unauthorized action in the browser or console and confirm it raises Pundit::NotAuthorizedError or CanCan::AccessDenied as expected; use persisted records (e.g., User.create!) not unsaved ones
  6. Scope queries — use policy_scope(Model) or accessible_by(current_ability) for index actions
  7. 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
end
CanCanCan
class Ability
  include CanCan::Ability

  def initialize(user)
    can :update, Post, user_id: user.id
    can :manage, :all if user.admin?
  end
end
Troubleshooting
ErrorLikely CauseFix
Pundit::NotDefinedErrorNo policy class found for the recordCreate app/policies/model_policy.rb inheriting from ApplicationPolicy
Pundit::AuthorizationNotPerformedErrorauthorize not called in a controller actionAdd authorize @record in the action, or after_action :verify_authorized to catch misses
CanCan::AccessDenied unexpectedly raisedAbility rules not matching the current user/roleInspect 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
end

Output Style

When implementing or reviewing authorization, the output answer.md must include:

  1. 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.
  2. HTTP and Policy Verification — concrete curl requests or controller test commands with expected HTTP response codes (e.g. 403 Forbidden or 302 Found) when access is denied.
  3. Language — English unless explicitly requested otherwise.

See references/output-style.md for full formatting examples including Pundit and CanCanCan console output templates.

Integration

SkillWhen to chain
write-testsWhen 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

Comments

Markdown supported