Custom authentication check for delayed web gem?

64 Views Asked by At

currently I am using the gem delayed-web in my project. I have multiple user roles, and I don't wanna the users whose roles are sales can access to the page delayed web background interface. I already have a method to check for the authentication in my application controller. However, I don't know how to make it work in the route files. Any suggestion would be appreciated.

Updated: I am not using Devise gem. I roll my own authentication.

application_controller.rb:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :authenticate
  before_action :check_pricer_role, except: [:export_for_customer]
  helper_method :check_pricer_role
 def check_pricer_role
    unless current_user && (current_user.pricer? || current_user.admin?)
      redirect_to errors_not_found_path
    end
  end
end

routes.rb:

Rails.application.routes.draw do
  # How to apply the defined authentication here?
  mount Delayed::Web::Engine, at: '/jobs'
end
1

There are 1 best solutions below

0
Fatima On BEST ANSWER

Ok, I've solved the problem. It turns out that I can find the current user based on his authentication token which is saved inside of the Request object(again, I don't use any gems for authentication, I roll my own one, but I don't think that is the problem here anyway). This is the complete solution, in case somebody way run into the same difficulty:

    class AuthConstraint
      def matches?(request)
        current_user ||= User.find_by_auth_token(request.cookie_jar['auth_token']) if request.cookie_jar['auth_token']
         current_user.present? && !current_user.sales?
      end
    end

    Rails.application.routes.draw do
        mount Delayed::Web::Engine, at: '/jobs', :constraints => AuthConstraint.new
        //Other resources ........ 
    end