2011-10-04 54 views
3

在我的rails 3.1应用程序中,我想为用户创建和过期随机密码。我正在使用devise gem。任何插件可用于expiring password持续一段时间?
否则请给我一些合理的建议来实现这个功能。
请考虑我作为一个新手。Rails在24小时内过期密码

回答

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

有在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'' –

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