2011-03-16 30 views
3

我正在使用devise进行验证。我如何设置登录范围?例如,假设我只想验证用户的范围:设计 - 在验证时设置示波器

User.where(:active => true) 

我是否清楚?这很简单,但如果需要,我可以详细说明。

(我知道有一个可锁定模块,但我实际范围不活跃用户,它更像current_site.users,其中CURRENT_SITE基于关闭域)

回答

1

可能使用default_scope ...但这可能会阻碍你。

为什么不重写devise的find_for_database_authentication方法?见the wiki

+0

到维基的链接已损坏。这就是为什么你需要在你的答案中包含实际的代码。 @ cache.zero的答案比较好。 – 2014-11-13 13:53:54

14

只是简单地覆盖这两种方法在您的用户模型来检查活动标志是

# Called by Devise to see if an user can currently be signed in 
def active_for_authentication? 
    active? && super 
end 

# Called by Devise to get the proper error message when an user cannot be signed in 
def inactive_message 
    !active? ? :deactivated : super 
end 

而在你devise.en.yml,添加适当的错误消息:

devise: 
    failure: 
    deactivated: "Luke, I'm your father and your account was locked!"