我有一个使用 Devise 处理登录/注册的应用程序。我也在使用 acts_as_tenant。我需要确保每次有人去注册/登录时都设置一个租户。要作为租户工作,必须在身份验证之前设置租户。现在我在我的 ApplicationController 上使用 before_action 但问题是即使有人有无效的凭据等,该方法也会被调用,我试图避免在方法中编写 if 语句来确定我是否有有效的用户与否。
实现此目标的最佳方法是什么?有遇到类似情况的吗?
最佳答案
如果需要在Devise认证前设置租户,则需要使用acts_as_tenant
的手动租户设置方法,这样可以控制时间。
# application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
# tell acts_as_tenant you'll manually set the tenant
set_current_tenant_through_filter
# manually set the tenant before Devise auth
prepend_before_action :set_current_tenant_by_subdomain
# Devise auth as usual but after tenant is set
before_action :authenticate_user!
private
def set_current_tenant_by_subdomain
current_organization = Organization.find_by_subdomain(request.subdomain)
set_current_tenant(current_organization)
end
end
https://stackoverflow.com/questions/57419511/
相关文章:
python - 设置 TensorBoard 以在 Google Colab 中为 TensorF
msbuild - 旧项目格式忽略 PackageReference 条件
python - 如何将文件传递给 unittest.mock.mock_open()?
react-native - 使用 wix react native navigation v3 设
reactjs - Next.js 状态改变不重新渲染 UI
python - 如何在 Python 和 web3.py 中获取 Solidity revert/