设计将忘记密码说明发送到任何电子邮件ID

时间:2012-11-09 11:23:30

标签: ruby-on-rails ruby ruby-on-rails-3 devise

我正在寻找设计中的自定义,如果我们点击忘记密码,它应该将邮件发送到任何电子邮件ID。无论电子邮件ID是否存在,Gmail中都会发生类似的情况。

屏幕1 enter image description here

屏幕2 enter image description here

目前我所拥有的是,它尝试使用系统中的有效用户进行验证。

enter image description here

Deviserecoverable module负责此

       def send_reset_password_instructions(attributes={})
          recoverable = find_or_initialize_with_errors(reset_password_keys, attributes,   :not_found)
          recoverable.send_reset_password_instructions if recoverable.persisted?
          recoverable
        end

如何删除此验证并将电子邮件发送到任何电子邮件ID?

2 个答案:

答案 0 :(得分:8)

有一个名为paranoid的Devise配置,当设置为true时,将以避免电子邮件枚举的方式更改消息。只需在您的Devise配置中设置config.paranoid = true

答案 1 :(得分:2)

我的解决方案是扩展/覆盖Devise的密码控制器。为此,创建一个继承自Devise密码控制器的控制器(让我们称之为密码),如下所示:

class PasswordsController < Devise::PasswordsController

然后编辑您的路线文件,以使此更改生效:

devise_for :users, :controllers => { :passwords => 'passwords' }

现在,您要覆盖create action。有几种方法可以做到这一点,但由于我不确定你想做什么,我会向你展示你可以做的两件事:

  1. 您只想阻止“找不到电子邮件”错误,以便人们无法在您的数据库中找到或不存在哪些电子邮件:

    def create
      self.resource = resource_class.send_reset_password_instructions(resource_params)
    
      respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
    end
    
  2. 您确实想要向任何输入的电子邮件发送电子邮件:

    def create
      self.resource = resource_class.send_reset_password_instructions(resource_params)
    
      unless successfully_sent?(resource)
        Devise::Mailer.reset_password_instructions(resource).deliver
      end
    
      respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
    end
    
  3. 现在,最后一个解决方案的问题是你正在向不存在的用户发送电子邮件......所以当用户回来时,他将无法输入他的新密码,因为他的无法找到用户帐户。但如果你真的想这样做,希望我能让你走上正轨。