acts_as_tenant icon indicating copy to clipboard operation
acts_as_tenant copied to clipboard

How to set user before require_tenant check?

Open seanarnold opened this issue 2 years ago • 1 comments

I want to conditionally set require_tenant based on a user attribute. i.e all non-admins need to have a Tenant explicitly set.

ActsAsTenant.configure do |config|
  config.require_tenant = lambda do
    Current.user && !Current.user.admin?
  end
end
class ApplicationController < ActionController::Base
  set_current_tenant_through_filter
  before_action :authenticate_user!
  before_action :set_tenant_record

  def authenticate_user!
    ActsAsTenant.without_tenant do
      super
      # Set a global var as `current_user` isn't available in initialiser 
      Current.user = current_user
    end
  end

  def set_tenant_record
    set_current_tenant(current_user.tenant)
  end
end

However require_tenant is evaluated when authenticate_user! is called, even though it's within a without_tenant block. As Current.user is nil, the lamba evaluates to false.

How do I set my Devise user before the require_tenant lambda is evaluated?

seanarnold avatar Jan 09 '23 22:01 seanarnold