【问题标题】:How to enable guest account for a specific controller如何为特定控制器启用访客帐户
【发布时间】:2022-11-04 05:18:55
【问题描述】:

我被困在我正在从事的项目的实施中。使用带有设计的 rails 7,所有用户都可以登录,所以我有 authenticate_user!current_user 方法,这同样可以正常工作。

我介绍了一个Customer 模型。目前,我不希望客户使用密码等进行注册,我希望他们点击一个链接,从我的应用程序发送给他们,然后访问一个页面:

# Observer

class CustomerObserver < ActiveRecord::Observer
  def after_create(customer)
    secret_param = customer.to_sgid(expires_in: nil).to_s
    url = ENV['HOST'] + "?csig=#{secret_param}"

    # Send email with the url...
  end
end

一旦客户点击该链接,他们应该被带到一个特殊的页面,并且应该只能访问这个控制器。

一直在查看GlobalID,但不确定如何在.to_sgid 中使用for: name,以便我可以限制客户仅访问CustomersControllershow 操作。

在所有控制器中,我都有before_action :authenticate_user!。我的第一件事是overriding that method。感觉不对。如何让客户通过签名链接访问CustomersController 并且仍然受到未经授权的用户的保护?

【问题讨论】:

  • 你曾经使用过专家吗?还是灿灿?

标签: ruby-on-rails devise


【解决方案1】:

像这样的懒惰检查怎么样?

before_action :authenticate_user!, :if => :check_csig

# ...
private

def check_csig
  # if csig param do not exists, call the authenticate_user!
  true unless params[:csig].exists?

  # if csig param exists and csig is valid do not call the authenticate_user!
  false if check_csig
  true
end

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2014-09-27
    • 2017-12-04
    相关资源
    最近更新 更多