1

I have been using devise and cancancan for authentication and authorisation resp., resque for background jobs. I have been following Ryans screencast number 271 and saw below code snippet for routes.rb file.

authenticate :admin do
  mount Resque::Server, :at => "/resque"
end  

to authenticated user, but in my case I have only users table and admin is also users separated by role column, Now I would like to authenticate and authorise the route for resque server path based on users role, How can I achieve solution for this problem ?

authenticate :user do
  mount Resque::Server, :at => "/resque"
end  

works fine for logged in user but i want it to be accessible only to admin user. Any help will be heartly appreciated.

4

1 回答 1

1
# config/initializers/admin.rb
class CanAccessResque
  def self.matches?(request)
    current_user = request.env['warden'].user
    return false if current_user.blank?
    Ability.new(current_user).can? :manage, Resque
  end
end


# routes.rb
namespace :admin do
  constraints CanAccessResque do
    mount Resque::Server, at: 'resque'
  end
end

# ability.rb
class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new
    if user.is_admin?
      can :manage, Resque
    end
  end
end

# user.rb
class User < ActiveRecord::Base
  def is_admin?
    # your admin logic here for example:
    self.role == "admin"
  end
end

或者有关更多信息,请查看此博客:resque-admin-in-rails-routes-with-cancan

希望这对您有所帮助。

于 2015-05-08T04:53:42.723 回答