gpt4 book ai didi

ruby - 在设备登录时取消删除 acts_as_paranoid 已删除的用户

转载 作者:数据小太阳 更新时间:2023-10-29 07:53:56 24 4
gpt4 key购买 nike

我有一个 Rails 3.1.3 应用程序,它使用 devise 进行用户身份验证,并使用 acts_as_paranoid 软删除它们。我希望这些帐户在重新创建密码、用户注册和用户登录时被取消删除,所以如果他们提供已删除的电子邮件,我会获取该帐户,使其重新生效,然后继续操作(重新创建密码或登录).

但是在 Users::SessionsController#create 操作中,在取消删除用户后它会得到一个 Unauthorized 错误(但用户现在应该是可见的)。代码是:

def create
# Take into account acts_as_paranoid deleted users
resource = resource_class.only_deleted.find_by_email(params[resource_name][:email])
resource.undelete! if resource

resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end

如果我在取消删除后添加一个 resource.reload 调用,它不会改变任何东西。如果我再次登录,用户会正常登录,因为它在上一次尝试中未被删除。

为什么会这样?如何在单个 create 调用中取消删除并登录?

最佳答案

用下面的代码片段解决了它:

def create
# Take into account acts_as_paranoid deleted users
if (resource = resource_class.only_deleted.find_by_email(params[resource_name][:email]))
resource.undelete!
# Copied from Warden::Strategies database_authenticatable:
sign_in resource if resource.valid_password?(params[resource_name][:password])
end
super
end

关于ruby - 在设备登录时取消删除 acts_as_paranoid 已删除的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8886317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com