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

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

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

21254 次浏览

您希望执行授权,而不是身份验证。
也就是说,设计只是告诉你,一个用户是谁,他说他是。
你需要别的东西来禁止他使用这个网站。

授权是一个流行的话题,有一个完整的宝石列表可以帮助你:
Http://ruby-toolbox.com/categories/rails_authorization.html
随你挑。

这样做:

Create a column called is_active for the User model.

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

更新

As Matt Huggins notes, the method is now called 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 的标准信息是:

"Your account is not activated yet."

这让用户感到困惑,真正的原因是你“禁止”他们登录。