【问题标题】:Rails 5 Using Devise and acts_as_tenantRails 5 使用设计和acts_as_tenant
【发布时间】:2019-12-16 13:45:14
【问题描述】:

我有一个使用 Devise 处理登录/注册的应用程序。我也在使用acts_as_tenant。我需要确保每次有人去注册/登录时都设置了租户。作为租户工作,必须在身份验证之前设置租户。现在我在我的 ApplicationController 上使用 before_action 但问题是即使有人拥有无效的凭据等也会调用该方法,并且我试图避免在方法中编写 if 语句来确定我是否有一个有效的用户与否。

实现这一目标的最佳方法是什么?有没有类似情况的人?

【问题讨论】:

    标签: ruby devise acts-as-tenant


    【解决方案1】:

    如果您需要在使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多