使用 Devise Invitable 接受邀请时禁止更改密码电子邮件

Suppress password change email when accepting invitation with Devise Invitable

我已使用

启用密码更改通知

config.send_password_change_notification = true

但是,目前当用户接受由 Devise Invitable 触发的邀请时,他们会收到一封密码更改电子邮件。 例如

Started PUT "/users/invitation" for 127.0.0.1 at 2017-10-20 16:14:41 +0100
Processing by UsersController::InvitationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9gDHF+Vm6oxGPILonaQe7NSnhytoQGsOBm0eVEMziSS6J93UFnoHSwouyV9NezleulmstfcNW8Axr/nJajBBYw==", "user"=>{"invitation_token"=>"98usw1XW4w31UuCd_DzQ", "full_name"=>"example", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Set my password"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."invitation_token" =  ORDER BY "users"."id" ASC LIMIT   [["invitation_token", "0a0787a3270a097c22a1272f49040c5c11d67b2cb222059d88d85f35e95c8d78"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "users" SET "invitation_token" = , "encrypted_password" = , "full_name" = , "invitation_accepted_at" = , "updated_at" =  WHERE "users"."id" =   [["invitation_token", nil], ["encrypted_password", "a$qQKCx4FWQS2ARyiiGf8zdeAn7XLBa0clbWuv1cH1c1cXWbF65VMd6"], ["full_name", "example"], ["invitation_accepted_at", "2017-10-20 16:14:41.323768"], ["updated_at", "2017-10-20 16:14:41.324814"], ["id", 9]]
DeviseMailer#password_change: processed outbound mail in 1.7ms
Sent mail to example@example.com (195.9ms)

如果用户接受邀请但继续为其他密码更改事件发送通知,是否可以禁止通知?

环境信息:

Rails 5.1.4
devise (4.3.0, 4.2.1)
devise_invitable (1.7.2)

好的,明白了。 将以下内容添加到 app/models/user.rb

 def send_devise_notification(notification, *args)
    unless(notification == :password_change && sign_in_count.zero?)
      super
    end
  end

感谢您为我指明了正确的方向,这是我对此的看法

def send_password_change_notification
    super unless accepting_invitation?
end

accepting_invitation?是进程开始时设置的变量

作为奖励,它让我很烦,当你要求恢复密码时,它会提示你一条成功消息,但什么都不做,它会提示一条错误消息。

[:en, :errors, :messages, :invitation_active] 在您的语言环境中或仅使用默认值 :not_found

def send_reset_password_instructions
    if invited_to_sign_up?
        self.errors.add :email, :invitation_active
    else
        super
    end
end