将设计密码恢复限制为仅限某些用户

我的应用程序有一个预订系统,允许用户无需密码订阅。

但是尚未完成订阅的恶意用户(=还没有密码)可以按照密码恢复过程设置自己的密码并制止通常的订阅过程。

所以我希望Devise只为经过validation的用户(即完成订阅的用户)提供密码恢复。

如果未经validation的用户正在输入他的电子邮件以尝试获取新密码,我想向他显示“未找到电子邮件”错误,就好像他根本找不到。

我没有找到覆盖Devise的位置来添加这样的范围。

来自Recoverable 这个方法似乎适合但我无法自定义它:

 # Attempt to find a user by its email. If a record is found, send new # password instructions to it. If user is not found, returns a new user # with an email not found error. # Attributes must contain the user's email def send_reset_password_instructions(attributes={}) recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found) recoverable.send_reset_password_instructions if recoverable.persisted? recoverable end 

所以目前我覆盖了PasswordsController但这不是一个干净的解决方案。

 class Users::PasswordsController < Devise::PasswordsController def create if resource_params[:email].present? && resource_class.registration_complete.where(email: resource_params[:email]).none? self.resource = resource_class.new self.resource.errors.add(:email, :not_found) render :new else super end end end