【问题标题】:Preventing Devise from sending an email when changing password更改密码时防止设计发送电子邮件
【发布时间】:2013-01-16 01:27:03
【问题描述】:

我在我的 Rails 3 应用程序中使用 Devise。当前重置密码的行为是点击“忘记密码?”关联。这里的链接是:

(url)/password/new.user

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

def new
    build_resource({})
end

这个方法可以:

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

  2. 通过包含令牌的链接向此人发送电子邮件:

    (url)/password/edit?reset_password_token=xxxxxxxxxxxxxxx

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

【问题讨论】:

  • 您的问题是您是否可以阻止设计发送链接在代码中,或者由于来自客户端的一些格式错误的请求?
  • 您可以将 gem 拉入您的供应商/缓存并在那里修改代码。这将允许您更改设计处理 build_resource 方法的方式。
  • 致 Kobaltz:是的,我可以将宝石拉入并修改它,但这不是我想要的。
  • 致 Chris Cashwell:两者都不是。我希望它不发送电子邮件。请阅读问题。

标签: ruby-on-rails devise


【解决方案1】:

我建议在您的用户 (?) 模型上覆盖 send_devise_notification,并在通知值为 :reset_password_instructions 时返回 true。像这样的:

# app/models/user.rb
def send_devise_notification(notification)
    return true if notification == :reset_password_instructions
end

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

【讨论】:

    【解决方案2】:

    您可以在实例级别禁用它:

    # disable all notifications
    user.define_singleton_method(:send_devise_notification) { |*_| true }
    
    # disable the one you want
    user.define_singleton_method(:send_devise_notification) do |*args|
      return true if args[0] == :reset_password_instructions
      super
    end
    

    【讨论】:

    • 脚本的完美解决方案!谢谢@fabriciofreitag!!!
    【解决方案3】:

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

    为防止在更改用户密码时发送密码更改电子邮件通知,请在保存用户之前致电用户skip_password_change_notification!

    user = User.find(123)
    user.skip_password_change_notification!
    user.password = 'DoNotUse$123'
    user.save
    

    【讨论】:

      猜你喜欢
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      相关资源
      最近更新 更多