设计 - 我如何禁止某些用户登录?

时间:2011-05-14 19:13:09

标签: ruby-on-rails devise

我在我的应用程序中使用Devise进行身份验证。

如何禁止某些用户登录 - 禁用用户?

4 个答案:

答案 0 :(得分:134)

这样做:

is_active模型创建一个名为User的列。

然后将以下代码添加到User模型中:

class User < ActiveRecord::Base
  #this method is called by devise to check for "active" state of the model
  def active_for_authentication?
    #remember to call the super
    #then put our own check to determine "active" state using 
    #our own "is_active" column
    super and self.is_active?
  end
end

更新

Matt Huggins指出,该方法现称为active_for_authentication?Documentation

答案 1 :(得分:15)

User模型添加一列:allowed_to_log_in

然后将其添加到/app/models/user.rb

def active_for_authentication?
    super and self.allowed_to_log_in?
end

如果您想通过自定义消息通知用户,您也可以添加此消息:

def inactive_message
    "You are not allowed to log in."
end

我认为这非常重要,因为Devise的标准信息说:

  

&#34;您的帐户尚未激活。&#34;

这对用户来说很困惑,而真正的原因是你已经被禁止了#34;他们登录。

答案 2 :(得分:0)

您想要进行授权,而不是身份验证。但是,设计只会进行认证 即设计只告诉你一个用户就是他所说的那个人 你需要别的东西来禁止他使用这个网站。

授权是一个热门话题,有一整套可以帮助你的宝石:
http://ruby-toolbox.com/categories/rails_authorization.html
随便挑选。

答案 3 :(得分:-1)