Search code examples
ruby-on-railsdeviseroutesresque

Allow only admin users to access a resque web interface


In my rails app I am using devise for authentication and my model name is user for which I have a Boolean field admin. In my routes.rb, I am trying to limit all users except admins from accessing a url.

mount Resque::Server.new, :at => "/resque"

. I tried using devise's current_user helper like

 mount Resque::Server.new, :at => "/resque" if current_user.admin == true

But got the error undefined local variable or method 'current_user' for #<ActionDispatch::Routing::Mapper:0x000000053ac780> (NameError) . How to do this? I am new to rails please help


Solution

  • Finally this is what worked for me

      # routes.rb
      match "/resque" => Resque::Server, :anchor => false, :constraints => lambda { |req|
        req.env['warden'].authenticated? and req.env['warden'].user.is_admin?
      }
    
      # user.rb
      class MUserLogin < ActiveRecord::Base
        .
        .
        .
        def is_admin?
          # your admin logic here for example:
          self.admin == true
        end
      end