rolify

repository·master·Indexed 25 days ago

https://github.com/rolifycommunity/rolify

A 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.

Tokens
4.1K
Snippets
10
Records
53
Agent score
82%

What's inside rolify

  1. Generate Rolify Role and User models

    master

    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 User
  2. Generate Rolify models and migrations

    master

    Use 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 Account
  3. Generate Mongoid Role Model

    master
    Use the Rolify Mongoid generator to create a role model compatible with Mongoid. By default, it assumes a User 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.
  4. Use Cached Roles to avoid N+1 queries

    master

    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
    end
  5. Manage user roles (Add and Remove)

    master

    You can assign roles globally, to a specific resource instance, or to a resource class.

    • Global role: user.add_role :admin
    • Instance-scoped role: user.add_role :moderator, forum_instance
    • Class-scoped role: user.add_role :moderator, Forum

    To remove a role, use user.remove_role :role_name.

  6. Configure the User model with Rolify

    master

    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_add
    • after_add
    • before_remove
    • after_remove

    You 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
    end
  7. Query resources by roles

    master

    You 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.
  8. Query user roles

    master

    Check if a user has a specific role using has_role?.

    • Global check: user.has_role? :admin
    • Instance check: user.has_role? :moderator, forum_instance
    • Class check: user.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