【问题标题】:Devise, multiple Models and ApplicationCable设计、多种型号和应用电缆
【发布时间】:2020-12-14 19:40:44
【问题描述】:

我需要为应用程序添加对多个模型(带有用户的表)的支持。总的来说,除了 ApplicationCable 之外,一切都很顺利。

该应用程序有这个代码:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      if verified_user = env['warden'].user
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

这段代码很受欢迎。它存在于许多来源中。

但它不适用于需要多个模型的情况。前端的连接被切断,原因是:

未经授权的连接尝试被拒绝

UPD:

我意识到我可以使用明确的指示:

env['warden'].user(:admin)

但现在我不明白如何为 ApplicationCable 连接自动确定这一点。


你能告诉我如何解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-6 actioncable


    【解决方案1】:
    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
        def connect
          self.current_user = find_verified_user
        end
    
        protected
    
        def find_verified_user
          if verified_user = env['warden'].user(:admin)
            verified_user
          elsif verified_user = env['warden'].user
            verified_user
          else
            reject_unauthorized_connection
          end
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多