使用Devise Invitable接受邀请时,禁止更改密码

时间:2017-10-20 15:19:17

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

我已使用

启用了密码更改通知

config.send_password_change_notification = true

但是,目前当用户接受由Devise Invitable触发的邀请时,他们正在收到密码更改电子邮件。 e.g。

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" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["invitation_token", "0a0787a3270a097c22a1272f49040c5c11d67b2cb222059d88d85f35e95c8d78"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  SQL (0.6ms)  UPDATE "users" SET "invitation_token" = $1, "encrypted_password" = $2, "full_name" = $3, "invitation_accepted_at" = $4, "updated_at" = $5 WHERE "users"."id" = $6  [["invitation_token", nil], ["encrypted_password", "$2a$11$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)

2 个答案:

答案 0 :(得分:1)

谢谢你指出我正确的方向,这是我对此的看法

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

答案 1 :(得分:0)

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

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