可以根据条件启用/禁用设备可锁定模块

时间:2016-06-08 11:38:59

标签: ruby-on-rails ruby devise

我想根据具体情况为用户禁用/启用设计可锁定模块。例如,仅为非管理员用户启用可锁定模块。

Confirmable模块confirmation_required?中有一个方法,如果需要确认,可以覆盖。

http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Confirmable#confirmation_required%3F-instance_method

Lockable模块确实有类似的方法吗?任何帮助将不胜感激

谢谢

2 个答案:

答案 0 :(得分:0)

把它放在你的用户模型中:

def active_for_authentication?
  super && (admin? || !access_locked?)
end

def valid_for_authentication?
  return super unless persisted? && lock_strategy_enabled?(:failed_attempts)

  # Unlock the user if the lock is expired, no matter
  # if the user can login or not (wrong password, etc)
  unlock_access! if lock_expired?

  if super && (admin? || !access_locked?)
    true
  else
    self.failed_attempts ||= 0
    self.failed_attempts += 1
    if attempts_exceeded?
      lock_access! unless access_locked?
    else
      save(validate: false)
    end
    false
  end
end

来自docs网站: http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Lockable#active_for_authentication%3F-instance_method

答案 1 :(得分:0)

您可以将以下方法放在user.rb文件中

def lock_strategy_enabled?(strategy)
  return false if self.admin?
  true
end
相关问题