具有Devise的多租户(acts_as_tenant)(用户默认为子域的作用域)会中断会话

Hab*_*bax 5 session scope multi-tenant devise ruby-on-rails-3

宝石

ruby 1.9.3
rails 3.2.11
devise 2.2.3
acts_as_tenant 0.2.9
Run Code Online (Sandbox Code Playgroud)

我的所有模型都是由domain_id限定的:

class User < ActiveRecord::Base
  acts_as_tenant(:domain)
  #...
end
Run Code Online (Sandbox Code Playgroud)

然后,在我的application_controller中,我从域中设置当前租户:

class ApplicationController < ActionController::Base
  set_current_tenant_through_filter
  before_filter :set_tenant
  protect_from_forgery
  #...

  def set_tenant
    #...
    @domain = Domain.find_or_create_by_name(request.host)
    set_current_tenant(@domain)
  end
end
Run Code Online (Sandbox Code Playgroud)

一切都适用于除会话之外的所有模型:每次加载页面时,它都会注销第一个用另一个租户加载页面的用户.通过加载此页面,它将注销[...等]的第一个用户

假设:当Alice访问域时,Rails加载current_tenant = alice_domain(ok).所有工作都按预期工作,直到Bob访问另一个域,加载current_tenant = bob_domain.当Alice刷新她的页面时,Rails仍然有current_tenant == bob_domain.Rails检查会话:Alice与bob_domain范围不存在,因此Devise强制Alice注销.然后application_controller设置current_tenant = alice_domain ...注销Bob.

肮脏的解决方法:不要在用户模型中使用acts_as_tenant,在每个控制器中按域自己定位用户,然后覆盖设计范围登录和按域注册.而且我不确定如何让Devise知道会话中的当前域名.顺便说一句,用户手动default_scope替换acts_as_tenant属于同样的奇怪错误.走这条路似乎很脏.

我正在寻找一个干净的解决方案好几天.我会非常感谢任何帮助.

Hab*_*bax 3

固定,在application_controller,改变

before_filter :set_tenant
Run Code Online (Sandbox Code Playgroud)

prepend_before_filter :set_tenant
Run Code Online (Sandbox Code Playgroud)

为了在 Devise 检查用户会话之前对所有内容进行默认范围,包括用户。