在我的 Rails 3.1 应用程序中,我想为用户创建随机密码并使其过期。我正在使用 devise gem 来实现这一点。任何可用的插件expiring password
在一段时间内?
否则请给我一些逻辑建议来实现此功能。
请把我当作一个新手。
听起来您只想让密码过期一次。如果您希望定期(例如每几个月)执行此操作,或者如果您想防止用户重复使用密码,那么情况会变得更加复杂。
摘自我正在开发的应用程序:
app/models/user.rb(假设这就是您为模型命名的名称):
def password_should_expire?
# your logic goes here, remember it should return false after the password has been reset
end
应用程序/控制器/application_controller.rb
before_filter :check_password_expiry
def check_password_expiry
return if !current_user || ["sessions","passwords"].include?(controller_name)
# do nothing if not logged in, or viewing an account-related page
# otherwise you might lock them out completely without being able to change their password
if current_user.password_should_expire?
@expiring_user = current_user # save him for later
@expiring_user.generate_reset_password_token! # this is a devise method
sign_out(current_user) # log them out and force them to use the reset token to create a new password
redirect_to edit_password_url(@expiring_user, :reset_password_token => @expiring_user.reset_password_token, :forced_reset => true)
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)