简体   繁体   English

Rails-Devise:检查用户是否可以登录

[英]Rails - Devise : Check if user can sign in

I use devise to deal with Session. 我使用devise处理Session。

I need to check if a user is validate by admin before sign in. 登录前,我需要检查用户是否通过管理员验证。

I added a boolean field in my model 'is_validated'. 我在模型“ is_validated”中添加了一个布尔字段。

How can I check this field before sign_in a user ? 在登录用户之前如何检查此字段?

You need to overwrite the active_for_authentication? 您需要覆盖active_for_authentication? function of devise. 设计功能。 Basically you place your extra condition in that method and only if that method is true one can log in. 基本上,您将额外条件放在该方法中,并且只有该方法为true时才能登录。

You can find more details here: https://github.com/plataformatec/devise/wiki/How-To:-Customize-user-account-status-validation-when-logging-in 您可以在此处找到更多详细信息: https : //github.com/plataformatec/devise/wiki/操作方法-Customize-user-account-status-validation-when-logging-in

Overwrite the active_for_authentication? 覆盖active_for_authentication? method in your model 模型中的方法

Like this ....... 像这样 .......

class Your_model

  def active_for_authentication?
    super && is_validated?
  end

end

This returns true if user is validated. 如果用户通过验证,则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM