Role management library with resource scoping
open sourceMITself-hostablefree
rolify stores users' roles in a Rails app, either global (user.add_role :admin) or scoped to a resource class or a single record (user.add_role :moderator, Forum.first), and answers has_role?, with_role and similar queries from both sides. A generator creates the Role model and its join table for Active Record, and it works with Mongoid too. It enforces nothing itself: you check roles from an authorization library such as Pundit or CanCanCan, with any authentication such as Devise. Strict mode, callbacks when roles are added or removed, and has_cached_role? for preloaded roles are options.
Problems it solves
- give users roles like admin and moderator in my Rails app
- make someone a moderator of one forum but not the others
- list every user who is an admin of a given organization
- find all the projects a user has a manager role on
- store user roles in the database instead of a hardcoded string column
- give every new user a default role when they sign up
- check roles from my authorization policies without writing the join tables myself
- a role that applies to a whole class of records as well as to single records
- avoid an N+1 query when checking roles for a list of users
- assign and query roles on Mongoid models in a Rails app
Features
- global, class-scoped and instance-scoped roles
- has_role?, has_strict_role? and has_cached_role? checks
- with_role, without_role, with_any_role and with_all_roles queries
- resourcify for models roles apply to
- generator for the Role model and migration
- before_add, after_add, before_remove and after_remove callbacks
- strict mode
- Active Record and Mongoid
Limitations
- Doesn't enforce authorization: it stores and queries roles, and an authorization library does the checks12
Capabilities
| Has an API | plan not known |
|---|