devise – 如何禁止某些用户login?

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

如何禁止某些用户login – 禁用用户的种类?

像这样做:

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

然后将下面的代码添加到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 

UPDATE

正如马特·哈金斯(Matt Huggins)所说,这个方法现在被称为active_for_authentication? ( 文档 )

将一列添加到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的标准信息是:

“您的帐户尚未激活。”

这是令用户感到困惑的,真正的原因是您已经“禁止”他们login。

你想做授权,而不是authentication。 尽pipe如此,devise也只能是authentication。
即devise只告诉你,一个用户是他说他是谁。
你需要别的东西来禁止他使用这个网站。

授权是一个stream行的话题,并有一个可以帮助你的gem的完整列表:
http://ruby-toolbox.com/categories/rails_authorization.html
拿你的select。