防止 Devise 在更改密码时发送电子邮件

Eas*_*per 3 ruby-on-rails devise

我正在将设计与我的 Rails 3 应用程序一起使用。当前重置密码的行为是单击“忘记密码?” 关联。这里的链接是:

(url)/password/new.user
Run Code Online (Sandbox Code Playgroud)

这将在设计 passwords_controller.rb 中调用以下方法:

def new
    build_resource({})
end
Run Code Online (Sandbox Code Playgroud)

此方法将执行以下操作:

  1. 生成密码重置令牌并将其添加到数据库中,

  2. 向此人发送一封电子邮件,其中包含包含令牌的链接:

    (url)/password/edit?reset_password_token=xxxxxxxxxxxxxxx

有什么方法可以说服 Devise 只执行第 1 步而不是第 2 步吗?如果可能的话,是否有任何安全问题我应该注意,我确实采用了这种方法来简化网站的一部分。

mic*_*ter 7

问题的标题很笼统,但问题本身更具体。这是截至 2021 年一般问题的答案。

要防止在更改用户密码时发送密码更改电子邮件通知,请在保存用户之前skip_password_change_notification!呼叫该用户。

user = User.find(123)
user.skip_password_change_notification!
user.password = 'DoNotUse$123'
user.save
Run Code Online (Sandbox Code Playgroud)


小智 6

我建议覆盖send_devise_notification您的 User (?) 模型并在通知值为:reset_password_instructions. 像这样的东西:

# app/models/user.rb
def send_devise_notification(notification)
    return true if notification == :reset_password_instructions
end
Run Code Online (Sandbox Code Playgroud)

检查他们关于如何覆盖/自定义发送电子邮件的行为的示例 https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L127