7

我正在构建一个以 ActiveAdmin 作为主管理界面的多租户应用程序。我已经使用acts_as_tenant gem 很好地完成了数据分离。

我使用 AdminUser 模型作为所有用户的用户模型对象。

为了添加其他用户,AdminUser 也被限定为租户。

这是放弃登录,因为当 ActiveAdmin/Devise 尝试进行身份验证时,我假设它首先点击 find_tenant 过滤器,如下所示:

class ApplicationController
  set_current_tenant_through_filter

  before_filter :find_tenant

  def find_tenant
     if admin_user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
     end
  end

不知道如何解决这个问题......我希望用户登录,然后应用程序从登录用户那里获取 company_id 并设置租户,ActiveAdmin 上显示的所有数据都通过该租户进行范围(这部分通过如果我可以通过登录,acts_as_tenant gem)。

谢谢

4

2 回答 2

1

我认为您的怀疑是正确的,并且在身份验证之前调用了 find_tenant 方法,导致 admin_user_signed_in?是假的。如果确实如此(来自http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters),则将其调整为使用后过滤器应该可以解决问题。

class ApplicationController
  set_current_tenant_through_filter

  after_filter :find_tenant

  def find_tenant
    if admin_user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
    end
  end

不确定 set_current_tenant_through_filter 如何在所有这些中起作用,您是否尝试以两种不同的方式做同样的事情?

于 2012-09-04T05:14:03.747 回答
1

8 岁的帖子,但很远……我的设置routes.rb是这样的:

  devise_for :admin_users, ActiveAdmin::Devise.config
  require 'sidekiq/web'
  Sidekiq::Web.set :sessions, false
  authenticate :admin_user do
    mount Sidekiq::Web => '/admin/sidekiq'
  end

在我的情况下,我的非管理员用户有company_id,我认为你想要这个:

  def find_tenant
    if user_signed_in?
      set_current_tenant(Company.find(current_admin_user.company_id))
    end
  end

在 AA 空间中,我这样做了:

在活动管理控制器中设置多租户(Searchkick 索引需要)

我只在更新 AA 中的记录时设置租户。我这样做只是因为 Searchkick 索引要求。在您的情况下,您可能只需要允许 company_id 参数。

于 2020-10-07T19:27:37.873 回答