Registered User Home Switch

I am trying to implement a custom elevator for apartment based on user input based on request .

I am mainly trying to achieve:

  • Each time a request arrives, switch to the right tenant
  • If there is no registered user, the tenant chooses by default

However, my problem is that I cannot extract the current user from the request ( Rack :: Request ) object provided by the Shared elevator. Any tips on how to do this, or any other way to get the current user without asking?

I use devise for authentication.

+4
source share
1 answer

For those who have the same problem as me, this was my solution.

config / Initializers / apartment.rb

#My excluded models    
config.excluded_models = %w{ User }

Rails.application.config.middleware.use 'Apartment::Elevators::Generic', lambda { |request|
      tenant_name = nil



  if request.env['rack.session']['warden.user.user.key'] != nil
      tenant_name = User.find(request.env['rack.session']['warden.user.user.key'][0][0]).account.name
  end

  return tenant_name
}

However, I'm not sure if this is 100% fault tolerance, so I will post updates if I find something wrong.

+4
source

All Articles