取消删除acts_as_paranoid在设计登录时删除用户

时间:2012-01-16 21:11:19

标签: ruby ruby-on-rails-3.1 devise warden acts-as-paranoid

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

但在Users::SessionsController#create操作中,取消删除用户后会收到未经授权的错误(但用户现在应该可见)。代码是:

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来电中登录?

1 个答案:

答案 0 :(得分:5)

使用以下代码段解决它:

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
相关问题