What problem does it solve?
Centralizes authorization and permission logic so Rails applications can consistently enforce who can access which resources across controllers, views, APIs, and background processes.
Core Features & Use Cases
- Centralized Ability Definitions: Keep all permission rules in a single Ability class for predictable, testable RBAC.
- Controller and API Integration: Use load_and_authorize_resource, authorize!, and accessible_by to scope queries and raise consistent access errors.
- View and UI Controls: Show or hide actions and navigation links with can? and cannot? helpers to prevent unauthorized operations.
- Advanced Patterns: Supports multi-tenancy, time/state-based rules, attribute-level restrictions, delegated sharing, IP or token-scoped API access, and performance optimizations for collection queries.
- Testing & Best Practices: Includes RSpec examples, accessible_by testing, and recommendations for caching and eager loading to maintain performance.
Quick Start
Add CanCanCan to your Gemfile, generate an Ability class, and ask the assistant to produce an initial Ability with guest, user, moderator, and admin rules tailored to your models.