在我的rails 3.1应用程序中,我想为用户创建和过期随机密码。我正在使用devise gem。任何插件可用于expiring password
持续一段时间?
否则请给我一些合理的建议来实现这个功能。
请考虑我作为一个新手。Rails在24小时内过期密码
3
A
回答
3
这听起来像你只是想过一次密码。如果您希望定期(例如每隔几个月)执行此操作,或者您想防止用户重新使用密码,则会变得更加复杂。
从一个应用程序采取我的工作:
应用程序/模型/ 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
4
当您创建密码时,请记下它创建的时间。然后,在使用密码时,请检查密码是否在24小时前创建。
根据你使用的是什么框架,这个功能(或类似的东西)可能已经存在于框架内,或者可能作为一个插件。如果不是,实施并不困难。您需要的只是数据存储中的额外列以保存密码创建日期/时间以及创建密码和使用密码时的一些额外逻辑。
2
退房的设计安全扩展宝石:
https://github.com/phatworx/devise_security_extension
我一直在使用它来过期密码和存档密码(以确保旧密码不被重用),没有任何问题。
0
@ Jeriko的回答包含了一些旧代码,以下是编辑
在模型/ user.rb:
def password_should_expire?
if DateTime.now() > password_changed_at + 30.seconds
return true;
else
return false;
end
end
在应用程序控制器:
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.set_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
相关问题
- 1. 创建密码重置链接,在PHP中过期24小时
- 2. 如何在过期的24小时内设置到生成的密码在node.js
- 3. 发送邮件和过期账户在24小时内登记
- 4. Ruby on Rails:在过去的24小时内检索每小时的值
- 5. MySQL:按小时插入记录,在过去24小时内
- 6. iPhone日期24小时制
- 7. 确定24小时日期时间是否在间隔内
- 8. 如何在HQL中过滤24小时内的日期时间值?
- 9. 显示时间在24小时内
- 10. Python检查日期是否在24小时内
- 11. C#DateTime在最近24小时内
- 12. 检查OSX是否在24小时内
- 13. 在过去24小时的订单数
- 14. 石墨将只在过去24小时
- 15. DynamoDB在过去24小时内的衰减?
- 16. 24小时数= 24个值
- 17. python - 密码过期
- 18. 密码已过期
- 19. Rails finder方法找到一个超过24小时的实体?
- 20. MemberMouse注册日期加24小时
- 21. 新日期()12/24小时格式JavaScript?
- 22. Jqgrid日期列24小时格式
- 23. 显示SQL Server中选定日期的最近24小时内
- 24. 显示在过去24小时,按小时
- 25. 时间戳x小时前和24小时后的日期
- 26. 的Python:过滤项24小时日期格式
- 27. 需要计算过去24小时内发生的状态代码
- 28. 如何在Rails中的特定日期后24小时发生操作?
- 29. 如何使用超过24小时
- 30. 计算超过24小时,2008
有在devise_security_extension一些问题v 0.8.0。他们中很少有人喜欢https://github.com/phatworx/devise_security_extension/issues/83(这是一个主要问题)已经修复,但尚未发布。因此,想要使用修复程序的人直接引用来自Gemfile的gem的git repo URL,并引用最新的提交。对于例如'gem“devise_security_extension”,git:“https://github.com/phatworx/devise_security_extension”,ref:'1f35d'' –