tbl*_*tbl 6 login ruby-on-rails devise
我目前在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]消息...
我想知道两点.
Oli*_*ves 10
要停止已被"软删除"的用户,最好的方法是覆盖用户模型上的find_for_authentication类方法.如:
Class User < ActiveRecord::Base
def self.find_for_authentication(conditions)
super(conditions.merge(:deleted_flag => false))
end
Run Code Online (Sandbox Code Playgroud)
这将通过设计生成无效的电子邮件或密码flash消息(因为它无法找到用户进行身份验证)
至于你的第二个问题,你需要一些控制器中的方法来添加特定的flash消息.但是,在我看来,您应该将"软"删除的用户视为完全不存在于数据库中.因此,如果他们尝试登录,他们应该只获得有效的电子邮件或密码消息.
我还没有尝试过类似的事情,但似乎如果你想在身份验证之前捕获用户,你要么必须编写一个 Devise 身份验证策略,要么before_filter必须在authenticate_user!. 就像是:
before_filter :no_deleted_users
def no_deleted_users
if User.find(params[:email]).deleted?
redirect_to root_path, :flash => { :error => "Your user was deleted. You cannot log in." }
end
end
Run Code Online (Sandbox Code Playgroud)
尽管获取用户可能比这更复杂。我还没有玩过 Devise 预认证。
| 归档时间: |
|
| 查看次数: |
3299 次 |
| 最近记录: |