Skip to content

protector + rolify not playing nice. #179

@Altonymous

Description

@Altonymous

I'm using a gem called protector; it and rolify don't play very nice at the moment. From what I can tell it appears that rolify doesn't cache the user roles when instantiating the user so each call to has_role? or is_admin? queries the database. Couple that with a permission check on each model that looks at the role before trying to determine which permission checks to execute and grabbing a list of objects can take a very long time.

I'm not sure what to do in this case short of creating a wrapper method around has_role? that caches the result.

Example...

# app/controllers/widgets_controller.rb
class WidgetsController < ApplicationController
  def index
    respond_with Widget.restrict!(current_user)
  end
end

# app/models/widget.rb
class Widget < ActiveRecord::Base
  include Concerns::Permissions::Widget
end

# app/models/concerns/permissions/widget.rb
module Concerns::Permissions::Widget
  extend ActiveSupport::Concern

  included do
    protect do |user, widget|
      # Admins can retrieve anything
      if user.has_role? :administrator
        scope { all }

        # ... and view, create, update, or destroy anything
        can :view
        can :create
        can :update
        can :destroy
      elsif user.present?
        scope { all }

        can :view
        cannot :create
      end
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions