5

我正在使用Apartment gem 和 Rails 4.1 设置多租户应用程序。

我的方法是将管理界面托管在单个域上,然后在自定义域上提供特定于租户的数据。管理用户将能够属于多个租户,并且能够在管理界面中在租户之间切换而无需切换域。

使用 Apartment,我已经能够使用会话 cookie 在租户之间切换而没有任何问题。这是我的代码:

class ApplicationController < ActionController::Base
  before_filter :check_tenant

  private 

  def check_tenant
    if session[:current_tenant] and Apartment::Tenant.current_tenant != session[:current_tenant]
      Apartment::Tenant.switch(session[:current_tenant])
    elsif !session[:current_tenant]
      Apartment::Tenant.switch() # Revert to the primary tenant
    end
  end
end

我遇到的困难是 Apartment 在中间件级别提供基于域名的切换,我想保持这种方式,因为我假设中间件比使用 before_filter 更快。我看到了如何实现一个通用中间件来根据请求切换租户,但我似乎无法访问和解密传递给中间件的 proc 中的会话存储。

由于使用会话存储似乎对管理界面有意义,您对在中间件级别访问会话存储或将这两个方向结合在一起的其他方法有什么建议吗?

还是我只是太担心将租户切换保持在中间件级别?

4

0 回答 0