Permission Sets (Recommended)
Permission Sets provide a clean, modular way to manage permissions. Each permission set is a reusable group of permissions that can be assigned to roles.How It Works
- Roles - Determine which permission sets govern a user’s access (e.g.,
admin,customer_service) - Permission Sets - Contain the logic defining what actions users can perform on resources
- Role Configuration - Associates roles with their corresponding permission sets
Configuring Roles
In yourconfig/initializers/spree.rb, configure which permission sets are assigned to each role:
Built-in Permission Sets
Creating Custom Permission Sets
Create a new permission set inapp/models/spree/permission_sets/:
Permission Set API
Within a permission set, you have access to:can(action, subject, conditions = {})- Grant permissioncannot(action, subject, conditions = {})- Deny permissioncan?(action, subject)- Check if permission existsuser- The current userstore- The current store (for multi-store setups)
Managing Permission Configuration
Users and Roles
Spree comes with anadmin role by default. You can create more roles in the Admin Panel or via Rails console:
Default Role Behavior
- Users with no roles assigned automatically get the
:defaultrole permissions - No
RoleUserrecords are created for regular customers - Only users with special permissions need explicit role assignments
Legacy Approach: Custom Ability Classes
The permission sets approach above is recommended for new projects. The legacy approach below is supported for backward compatibility.
Adding Custom Permissions via Decorator
Create a new ability class inapp/models/customer_service_ability.rb:
app/models/spree/ability_decorator.rb:
Replacing the Ability Class
You can replace the entire ability class via Dependencies:CanCanCan Reference
Spree’s permission system is built on CanCanCan. Key concepts:can :action, Subject- Grant permissioncan :manage, Subject- Grant all actions (create, read, update, destroy)cannot :action, Subject- Explicitly deny permissioncan :action, Subject, conditions- Conditional permission

