Pergunta

I have an interesting case. I'm working on white-labeling functionality for a Rails 2.3 application and have problems with setting up dynamic session domains.

So, each white-label should have own domain: company1.com, company2.com, company3.com. More over each of them should have a support for subdomains and sharing the session between them.

I.e. test1.company1.com, test2.company1.com, company1.com <= should share a common session; test1.example.com, example.com <= should share a common session but different from company1.com .

In the common case (for a single base domain) it's done this way:

file:production.rb
config.action_controller.session[:domain] = '.company1.com'

If I want to support multiple domains I'll have to set this config option dynamically based on the accessed site domain. I'm currently trying to accomplish it in this way:

file:environment.rb
require 'dispatcher'
module ActionController
  class Dispatcher
    def set_session_domain
      tld_size = SubdomainFu.tld_size+1
      host_name = @env['HTTP_HOST'].split('.')[-tld_size,tld_size].join('.')
      ActionController::Base.session = { :domain => ".#{host_name}" }
    end
    before_dispatch :set_session_domain
  end
end

However the above code does not work.

Have you done something similar successfully or do you have any idea where the problem/solution could be?

Thank you in advance for your time

Foi útil?

Solução

Using ActionController::Base.session is not going to work, because it is used only when initializing the rack middleware handling the sessions, therefore only when booting your app. In order to make the above code work you should use

@env['rack.session.options']

Have a look here for an example:

http://railsforum.com/viewtopic.php?id=41306

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top