Skip to content

How to set user before require_tenant check? #304

@seanarnold

Description

@seanarnold

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions