Jim*_*dra 5 ruby-on-rails authlogic reset-password
我正在为使用Authlogic的Rails应用程序添加密码重置功能.我在这里遵循指南:http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/除了一件事之外,一切都按照我的意愿运行:密码重置表单接受空白密码,只是不更改它们.
我一直在搜索,并且已经了解到这是预期的默认行为,因为它允许您创建用户编辑表单,只有在用户输入新密码时才更改用户密码,否则忽略它.但在这种情况下,我特别希望强制验证密码,就像用户最初注册时一样.我找到了两个可能解决这个问题的方法,但还没弄清楚如何实现它们中的任何一个.
1)有人在Google网上论坛上提出同样的问题:
Ben的回应是用来@user.validate_password = true强制验证密码.我试过这个但是我得到一个未定义的方法错误:undefined method 'validate_password_field=' for #<User>.
2)似乎有一个名为的Authlogic配置选项ignore_blank_passwords.它记录在这里:
模块:Authlogic :: ActsAsAuthentic :: Password :: Config#ignore_blank_passwords
这看起来会起作用,但我的理解是这是一个全局配置选项,你acts_as_authentic在User模型的初始调用中使用,我不想在应用程序范围内更改它,因为我有一个常规的编辑我希望默认情况下忽略空白密码的用户表单.
有人找到了解决方案吗?我validate_password=在Authlogic 1.4.1的更改日志中看到,从那时起它就没有被删除了.我只是错误地使用它吗?有没有办法ignore_blank_passwords在每个请求的基础上使用?
这是一个旧线程,但由于没有答案,我会发布这个.
我已经设法比其他解决方案更干净,"帮助"我自己的authlogic验证.
我把它添加到用户:
class User < ActiveRecord::Base
...
attr_writer :password_required
validates_presence_of :password, :if => :password_required?
def password_required?
@password_required
end
...
end
Run Code Online (Sandbox Code Playgroud)
你可以通过制作attr_accessor和使用:if => :password_required(没有询问)将它减少到两行,但我更喜欢这个带有询问符号的其他语法.
然后您的控制器操作可以这样完成:
def update
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][: password_confirmation]
@user.password_required = true
if @user.save
flash[:notice] = "Password successfully updated"
redirect_to account_url
else
render :action => :edit
end
end
Run Code Online (Sandbox Code Playgroud)
这将产生局部效应; 应用程序的其余部分不会受到影响(除非password_required在其他地方设置为true,即).
我希望它有所帮助.
这就是我做的.
class User < ActiveRecord::Base
attr_accessor :ignore_blank_passwords
# object level attribute overrides the config level
# attribute
def ignore_blank_passwords?
ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
end
end
Run Code Online (Sandbox Code Playgroud)
现在在您的控制器中,将该ignore_blank_passwords属性设置为false.
user.ignore_blank_passwords = false
Run Code Online (Sandbox Code Playgroud)
在这里,您正在AuthLogic的范围内工作.您不必更改验证逻辑.
| 归档时间: |
|
| 查看次数: |
2738 次 |
| 最近记录: |