在 Active Admin Controllers 中设置多租户(Searchkick 索引需要) [英] Set Multi Tenant in Active Admin Controllers (required for Searchkick index)

查看:23
本文介绍了在 Active Admin Controllers 中设置多租户(Searchkick 索引需要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的多租户应用中使用 Active Admin.我还使用 Searchkick,它在每个模型中都有一个自定义的租户特定索引:

课程预算<申请记录多租户:公司searchkick 继承:true,index_name:->{ [MultiTenant.current_tenant.tenant_name, model_name.plural, Rails.env].join('_') }结尾

问题是在 AA 中此逻辑失败,因为在租户上设置.我希望能够在更新记录时在 AA 中进行设置.

例如,我会更新 http://localhost:4000/admin/budgets/dt2kqvgm,其中 dt2kqvgm 是记录的友好 ID.所以我想调用类似的东西:

MultiTenant.current_tenant = Budget.friendly.find(params['budget']['company_id'])

当我创建/更新记录等时

目前我得到:

 nil:NilClass 的未定义方法 `tenant_name'

因为在我的应用程序控制器中,租户是根据用户身份验证设置的,以限定当前公司的范围等.在 AA 中,我想/需要根据参数进行设置,而您似乎无法从 AA 控制器逻辑访问这些参数.我的参数在 AA 中看起来像这样:

<预> <代码> {" UTF8" =>"✓"," _method" =>"膜片"," authenticity_token" =>" PrhNGnPvV1Qfb5RCwTVv4Wwz9tjf9SFy2VWDcyJXoFLytM8y5ZAyF7h8I7xa + fy01E9Fc/v2CvR52I4/LKOLHQ ==", "budget"="{"company_id"="9", "name"="qweqwe", "description"=""qweqwe", "notes";=>qwee"、flag_active"=>1"、slug"=>dt2kqvgm"、title"=>qweqwe"}、提交"=>更新预算",控制器"=管理/预算",动作"=>更新",id"=dt2kqvgm"}

解决方案

我不知道这是否是最好的方法,但它有效.我知道它需要调整,但这是一个开始 - 您需要:

  1. 设置around_action过滤器
  2. 添加 allowed_pa​​rams

<块引用>

around_action :set_tenant, only: :update控制器做def set_tenantMultiTenant.with(Company.find(resource.company_id)) 做屈服结尾结尾def allowed_pa​​ramsparams.permit 位置:%[ company_id ]结尾结尾

似乎每个控制器都需要这样做.也许有办法将其添加为默认 AA 过滤器?

我还在 update 操作中添加了过滤器.

I am using Active Admin in my multi tenant app. I also use Searchkick which has a custom tenant specific index in each model:

class Budget < ApplicationRecord

  multi_tenant :company

  searchkick inheritance: true,index_name: -> { [MultiTenant.current_tenant.tenant_name, model_name.plural, Rails.env].join('_') }

end

The issue is that in AA this logic fails because on the tenant is set. I want to be able to set this in AA when updating a record.

For example I would update http://localhost:4000/admin/budgets/dt2kqvgm where dt2kqvgm is the Friendly ID of the record. So I want to call something like:

MultiTenant.current_tenant = Budget.friendly.find(params['budget']['company_id'])

when I create / update a record etc.

Currently I get:

undefined method `tenant_name' for nil:NilClass

because in my application controller the tenant is set based on the user authentication to scope the current company etc. In AA I want/ need to set this based on the params which it seems you can't access from the AA controller logic. My params look like this in AA:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"PrhNGnPvV1Qfb5RCwTVv4Wwz9tjf9SFy2VWDcyJXoFLytM8y5ZAyF7h8I7xa+fy01E9Fc/v2CvR52I4/LKOLHQ==", "budget"=>{"company_id"=>"9", "name"=>"qweqwe", "description"=>"qweqwe", "notes"=>"qwee", "flag_active"=>"1", "slug"=>"dt2kqvgm", "title"=>"qweqwe"}, "commit"=>"Update Budget", "controller"=>"admin/budgets", "action"=>"update", "id"=>"dt2kqvgm"}

解决方案

I don't know if this is the best way to do this but it works. I am know it needs tweaks but it's a start - you needs to:

  1. set an around_action filter
  2. add permitted_params

around_action :set_tenant, only: :update

controller do

  def set_tenant
    MultiTenant.with(Company.find(resource.company_id)) do
      yield
    end
  end

  def permitted_params
    params.permit location: %[ company_id ]
  end

end

It would seem this is required for each controller. Perhaps there is a way to add this as a default AA filter?

I also added the filter to just the update action.

这篇关于在 Active Admin Controllers 中设置多租户(Searchkick 索引需要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆