我目前在Rails项目中使用Devise进行用户注册/身份验证.当用户想要取消其帐户时,将以如下方式软删除用户对象.
我的这种方式有很小的不同.用户模型具有属性'deleted_flag'.并且,soft_delete方法执行"update_attribtue(:deleted_flag,true)"
但是,我必须实施sign_in行动.在我的implmenetation是以下.
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
if resource.deleted_flag
p "deleted account : " + resource.deleted_flag.to_s
sign_out(resource)
render :controller => :users, :action => :index
else
if is_navigational_format?
if resource.sign_in_count == 1
set_flash_message(:notice, :signed_in_first_time)
else
set_flash_message(:notice, :signed_in)
end
end
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
end
end
end
Run Code Online (Sandbox Code Playgroud)
我认为这段代码有点奇怪.
如果删除的用户试图进入,系统会允许记录并立即注销.并且,系统无法显示flash [:alert]消息...
我想知道两点.
我有一个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
Run Code Online (Sandbox Code Playgroud)
如果我resource.reload在取消删除后添加一个电话,它不会改变任何东西.如果我再次登录,用户通常会登录,因为它在之前的尝试中未被删除.
为什么会这样?如何通过一次create通话取消删除并登录?
我按照这些说明检查用户在登录时是否已被软删除.在下面的示例中,我可以检查布尔值:
Class User < ActiveRecord::Base
def self.find_for_authentication(conditions)
super(conditions.merge(:deleted_flag => false))
end
Run Code Online (Sandbox Code Playgroud)
我更喜欢时间戳(created_at)字段.如何检查这样的字段是否为空?数据库为以下检查抛出错误:
super(conditions.merge(:deleted_at => false)) # also tried nil, "NULL" and "IS NULL"
Run Code Online (Sandbox Code Playgroud)