将密码重置发送到其他电子邮件-设计 [英] Send Password Reset to a Different Email - Devise

查看:176
本文介绍了将密码重置发送到其他电子邮件-设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用devise在rails 5上使用ruby,我需要将密码重置电子邮件发送到不同于存储在User表中的电子邮件。如何做到这一点?

I'm using ruby on rails 5 with devise and I need to send a password reset email to a different email than the one stored in my User table. How can this be achieved?

推荐答案

请注意:这是不建议使用的实现方式。
不是在最佳实践的范围之内。

这是肮脏而脆弱的。

但是,如果您真的需要实现它,那么无论这些措施有多肮脏,就是这样。

Please note: it is pretty unrecommended way to implement things.
It is not within scope of the best practices.
It is dirty and fragile.
But if you really need to achieve it no matter how dirty are the measures, this is it.

将重置指令发送到其他电子邮件的要求已经很奇怪了。

Well, the requirement to send the reset instruction to other email is already weird enough. Is it really a last resort?

无论如何,

您尚未指定Devise版本,但该行为不太可能更改太多,因此让我们来了解当前的主服务器,看看它如何发送电子邮件:

You've not specified the Devise version but that behaviour was unlikely changed too much so lets take the current master and look how it sends emails:

https://github.com/plataformatec/devise/blob/f39c6fd92774cb66f96f546f8d5d4e5er828

def headers_for(action, opts)
    headers = {
      subject: subject_for(action),
      to: resource.email,

因此,吸气剂有些硬编码。

So, the getter is somewhat hardcoded.

不过,可以创建令牌并将其设置为Devise:

Though, it is possible to create a token and set it as Devise does:

https://github.com/plataformatec/devise/blob/d1948b79d3e933253baa753bd033c92171c0a7d0/lib/devise/models/recoverable.rb#L89

def set_reset_password_token
  raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)

  self.reset_password_token   = enc
  self.reset_password_sent_at = Time.now.utc
  save(validate: false)
  raw
end

然后在资源中找到Devise如何发送它并尝试以某种方式复制它,但使用您的自定义电子邮件。

And when find in sources how Devise sends it and try to somehow replicate it but using your custom email.

我认为这种情况下的邪恶程度较小

否则,您将无法与当前版本的Devise结合使用。

I think the less evil in this case would be just implementing your own mailer for that kind of reset instructions which would use the same URL as Devise does.
Otherwise you would have too much coupling with a current version of Devise.

这篇关于将密码重置发送到其他电子邮件-设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆