You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classApplicationController < ActionController::Baseset_current_tenant_through_filterbefore_action:authenticate_user!before_action:set_tenant_recorddefauthenticate_user!ActsAsTenant.without_tenantdosuper# Set a global var as `current_user` isn't available in initialiser Current.user=current_userendenddefset_tenant_recordset_current_tenant(current_user.tenant)endend
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?
The text was updated successfully, but these errors were encountered:
running into a similar issue, I have a primary and a secondary tenancy and need to determine which one to apply by looking into the current_user roles first
I had a similar need. This is my solution in case it helps anyone else
The initializer checks if the Current.user has been set. If so, it requires a tenant. conf/initializers/acs_as_tenant.rb
# Only require the current_tenant to be set once a user has logged inActsAsTenant.configuredo |config|
config.require_tenant=lambdadoCurrent.user.present?endend
The application controller sets the Current.user from the current_user variable provided by devise in each request. app/controllers/application_controller.rb
classApplicationController < ActionController::Basebefore_action:authenticate_user!before_action:set_current_userprivate# Set Current.user from devisedefset_current_userCurrent.user=current_userendend
Setting the Current.user also sets the Current.account and current_tenant app/models/current.rb
classCurrent < ActiveSupport::CurrentAttributesattribute:user,:account# Sets the user and associated account in the Current object.# Also sets the current tenant for ActsAsTenant.defuser=(user)superself.account=user&.accountActsAsTenant.current_tenant=user&.accountendend
I want to conditionally set
require_tenant
based on a user attribute. i.e all non-admins need to have a Tenant explicitly set.However
require_tenant
is evaluated whenauthenticate_user!
is called, even though it's within awithout_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?The text was updated successfully, but these errors were encountered: