简体   繁体   English

自定义密码恢复设备

[英]Custom password recovery Devise

I'm dealing with a Devise User model with an optional email attribute (he can login with document number or email). 我正在处理带有可选电子邮件属性的Devise用户模型(他可以使用文档编号或电子邮件登录)。 Since admins can register new users (setting his password), I would like to know if there's a way to users recover their devise password using document number and birthdate, to then, register a new password. 由于管理员可以注册新用户(设置他的密码),所以我想知道是否有一种方法可以让用户使用文档编号和生日来恢复其设计密码,然后注册一个新密码。

Hope that was clear. 希望那是清楚的。 Thanks. 谢谢。

Well, if the person is logged in you could just call the relevant method in this model (on User) to recover the password. 好吧,如果此人已登录,您可以(在用户上)调用此模型中的相关方法来恢复密码。 I guess you'd need to create a view that then validate a request to reset the password based on those values in the controller and then called the correct method based on the current_user . 我猜您需要创建一个视图,然后根据控制器中的这些值来验证重置密码的请求,然后根据current_user调用正确的方法。 My guess is you need the send_reset_password_instructions method from this file . 我的猜测是您需要此文件中send_reset_password_instructions方法。

A simple form on a view should do it; 一个简单的视图形式应该可以做到; if the values match (ie if you are able to validate the User based on the document number and birthdate from the POST compared to the expected values on the User called from the database), then call the method and send the reset email. 如果值匹配(即,如果您能够根据POST的文档编号和生日与从数据库调用的User的期望值进行比较来验证User),则调用该方法并发送重置电子邮件。

If you do not have an email address for the user (as I suspect perhaps from your question), then you could (in the controller) instead of calling the above method to send an email, instead call the reset_password! 如果您没有该用户的电子邮件地址(我怀疑可能是您的问题),那么可以(在控制器中)而不是调用上述方法来发送电子邮件,而可以调用reset_password! method, passing in their form input for new desired password, and that would change it. 方法,以其形式输入新的所需密码,然后进行更改。

EDIT: This link might also be of some use. 编辑: 此链接也可能有一定用处。

My solution was based on @Alex answer. 我的解决方案基于@Alex答案。 I did a custom password reset view. 我做了一个自定义的密码重置视图。

When dealing with password requirement on devise model, there are two (or more) options: 处理设计模型上的密码要求时,有两个(或更多)选项:

First, we can override the devise password requirement method. 首先,我们可以覆盖设计密码要求方法。 This would save a model object without password: 这将保存没有密码的模型对象:

def email_required?
  false
end

Or we can set a simple token after the object creation: 或者我们可以在创建对象后设置一个简单的令牌:

if athlete.new_record?
  generated_password = Devise.friendly_token[0,20]
  athlete.password = generated_password
  athlete.password_confirmation = generated_password
  athlete.skip_confirmation!
end

Optionally, put the behavior above on model: (可选)将上述行为放在模型上:

  def reset_password!(new_password, new_password_confirmation)
    self.password = new_password
    self.password_confirmation = new_password_confirmation
    save
  end

Briefly, that was the solution, including a view with birthday, password and password confirmation, validating the data on controller and overriding the password. 简而言之,这就是解决方案,包括查看生日,密码和密码确认的视图,验证控制器上的数据并覆盖密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM