【问题标题】:Devise - Setting the scope when authenticating设计 - 在身份验证时设置范围
【发布时间】:2011-07-16 06:23:48
【问题描述】:

我正在使用devise 进行身份验证。如何设置登录范围?例如,假设我只想对范围内的用户进行身份验证:

User.where(:active => true)

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

(我意识到有一个可锁定的模块,但我的 actual 范围不适用于活跃用户,它更像 current_site.users,其中 current_site 是基于域的)

【问题讨论】:

    标签: ruby-on-rails ruby authentication devise


    【解决方案1】:

    只需在您的 User 模型中覆盖这两个方法,以检查活动标志是否为 true

    # 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!"
    

    【讨论】:

      【解决方案2】:

      可以使用 default_scope...但这可能会妨碍您。

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

      【讨论】:

      • wiki 的链接已损坏。这就是为什么您需要在答案中包含实际代码的原因。 @cache.zero 的答案更好。
      猜你喜欢
      • 2015-12-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2014-11-15
      • 1970-01-01
      相关资源
      最近更新 更多