Rails为某些用户类型设计了禁用密码恢复功能

Vit*_*its 4 ruby authentication ruby-on-rails devise ruby-on-rails-3

在我的Rails项目中,我有不同类型的用户,其中一个拥有user_status :admin,与其他用户不同,它拥有编辑内容的完全权限.出于显而易见的原因,我想为这些类型的用户添加额外的安全性,特别是完全禁用密码恢复.

覆盖标准Devise密码恢复(:recoverable设计模块)方法的正确方法是什么,以便当用户尝试获取管理员用户(user_status == "admin")的用户的重置密码链接时,系统会返回"未找到标准电子邮件"消息?

这有点像未回答的问题:将设计密码恢复限制为仅限某些用户

先感谢您.

Vit*_*its 7

我选择并且对我有用的send_reset_password_instructions方法是通过将以下内容添加到以下来覆盖User模型的方法models/user.rb:

def send_reset_password_instructions
  return false if self.user_status == 'admin'
  super
end
Run Code Online (Sandbox Code Playgroud)

这使得Devise在电子邮件属于管理员帐户时不会做任何事情.


Kel*_*tin 4

对于任何未来的观众,这里有另一种方法。维塔利的例子确实对我有用,但我仍然收到“您的密码电子邮件已发送”。注意(我想要一个单独的警报来闪烁),所以我走了另一条路。

扩展 Devise::PasswordsController 对我来说是最简单的解决方案:

class Devise::Extends::PasswordsController < Devise::PasswordsController

  def create
    if some_condition?
      redirect_to :root
      flash[:alert] = 'You cannot reset your password, buddy.'
    else
      super
    end
  end
Run Code Online (Sandbox Code Playgroud)

然后,在routes.rb中:

devise_for :users, controllers: { passwords: 'devise/extends/passwords' }
Run Code Online (Sandbox Code Playgroud)

这会将您的应用程序定向到扩展控制器,然后如果不满足您的条件,则点击设备控制器(“超级”)。