Install Rolify
masterAdd rolify to your Gemfile and run bundle to install the gem.
Requirements:
- Rails >= 4.2
- ActiveRecord >= 4.2 OR Mongoid >= 4.0
- Ruby 2.2+
gem "rolify"repository·master·Indexed 25 days ago
https://github.com/rolifycommunity/rolifyA Ruby library for managing roles with support for scoping roles to specific resource objects or classes. It integrates with authentication gems like Devise and Authlogic, and authorization gems like CanCanCan and Pundit. Rolify supports Rails >= 4.2, ActiveRecord >= 4.2, and Mongoid >= 4.0.
Add rolify to your Gemfile and run bundle to install the gem.
Requirements:
gem "rolify"Use the Rolify generator to create your Role model, add migrations, and update your User class. By default, it uses Role and User as class names. You can specify custom names if your authentication solution uses a different User class.
If using Mongoid, add the --orm=mongoid flag.
Note for Rolify versions < 3.3: Use rails g rolify:role Role User instead.
rails g rolify Role UserUse the Rolify Rails generator to set up the necessary ActiveRecord models and migrations for role management. By default, the generator expects a model named User to exist in your application. If your user model has a different name, you must provide it as an argument.
To use the generator, run the following command in your terminal:
rails generate rolify [USER_CLASS_NAME]Example for a model named Account:
rails generate rolify Accountbundle to install the required gems followed by rake to execute the specs.bundle
rakeRolify supports active_record (the default) and mongoid. You can switch the adapter used during testing by setting the ADAPTER environment variable before running bundle or rake commands.
ADAPTER=mongoid bundle
ADAPTER=mongoid rakeUser model, but you can specify a custom user class name as an argument. The generator creates a model that includes has_and_belongs_to_many for the user, a polymorphic belongs_to :resource relationship, and the necessary fields and indexes for Rolify functionality.To avoid N+1 query issues when checking roles for a collection of users, preload the roles and use has_cached_role?.
Warning: You must preload the roles correctly. If you preload only specific roles using with_role, has_cached_role? will return false for any roles not included in that preload.
@user.add_role :admin, Forum
@user.add_role :member, Forum
# Preload roles to allow cached checking
users = User.with_role(:admin, Forum).preload(:roles)
users.each do |user|
user.has_cached_role?(:member, Forum) # no extra queries
endYou can assign roles globally, to a specific resource instance, or to a resource class.
user.add_role :adminuser.add_role :moderator, forum_instanceuser.add_role :moderator, ForumTo remove a role, use user.remove_role :role_name.
The rolify method is added to your User class. You can use it to define callbacks that trigger when roles are added or removed.
Supported callback options:
before_addafter_addbefore_removeafter_removeYou can also use the inverse_of option to disambiguate relationships.
class User < ActiveRecord::Base
rolify :before_add => :before_add_method
def before_add_method(role)
# do something before it gets added
end
endYou can search for resource instances based on the roles assigned to them.
Instance level queries:
forum.roles: Roles bound only to this instance.forum.applied_roles: Roles bound to this instance AND the class.Class level queries:
Forum.with_role(:admin): Instances with the :admin role.Forum.without_role(:admin): Instances without the :admin role.Forum.with_role(:admin, current_user): Instances with :admin role belonging to current_user.Forum.with_roles([:admin, :user], current_user): Instances with either :admin or :user role belonging to current_user.User.with_any_role(:user, :admin): Users with either role.User.with_role(:site_admin, current_site): Users with a scoped role on a specific site.User.with_role(:site_admin, :any): Users with a scoped role on any site.User.with_all_roles(:site_admin, :admin): Users having both roles.Finding role definitions:
Forum.find_roles: All roles bound to any Forum instance or the Forum class.Forum.find_roles(:admin): Roles bound to Forum that match the name :admin.Forum.find_roles(:admin, current_user): Roles matching :admin that belong to current_user.To allow roles to be scoped to a specific resource, add the resourcify method to that resource's class.
class Forum < ActiveRecord::Base
resourcify
endCheck if a user has a specific role using has_role?.
user.has_role? :adminuser.has_role? :moderator, forum_instanceuser.has_role? :moderator, Forum (returns true for both class-scoped and instance-scoped roles).Use has_strict_role? to check for the exact role scope without falling back to global or class-level roles.
# Global
user.has_role? :admin
# Instance
user.has_role? :moderator, Forum.first
# Class
user.has_role? :moderator, Forum
# Strict (only returns true if the role was explicitly added to that specific scope)
user.has_strict_role? :moderator, Forum.last