在我的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. PHP MYSQL选择过去24小时内和过去24小时内的数据
- 4. 的Python:在过去24小时内
- 5. 发送邮件和过期账户在24小时内登记
- 6. 在过去24小时内选择记录和日期
- 7. Ruby on Rails:在过去的24小时内检索每小时的值
- 8. 过去24小时
- 9. MySQL:按小时插入记录,在过去24小时内
- 10. iPhone日期24小时制
- 11. Datediff日期和小时(24小时)
- 12. 确定24小时日期时间是否在间隔内
- 13. 如何在HQL中过滤24小时内的日期时间值?
- 14. 显示时间在24小时内
- 15. SQL:另一日期/时间的24小时内的最小日期/时间值
- 16. Datediff超过24小时
- 17. JFreeChart - TimeSeries过去24小时
- 18. 检查日期是否在未来24小时内与C#
- 19. 检查日期从现在开始的24小时范围内
- 20. 在过去24小时内查找带有日期字段的记录
- 21. NSDateFormatter在日本不编码24小时预期
- 22. 在24小时内查找最大值
- 23. C#DateTime在最近24小时内
- 24. 检查OSX是否在24小时内
- 25. 理解PHP代码,并在24小时
- 26. 石墨将只在过去24小时
- 27. 在过去24小时的订单数
- 28. API V3在过去24小时内发布?
- 29. DynamoDB在过去24小时内的衰减?
- 30. 如何使用SQLite在过去24小时内获取事件?
有在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'' –