user.rb 1006 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Auth
  3. class User < SimpleDelegator
  4. attr_reader :user, :two_factor
  5. def initialize(username)
  6. @user = ::User.identify(username)
  7. @two_factor = Auth::TwoFactor.new(@user)
  8. super(@user)
  9. end
  10. def can_login?
  11. exists? && active? && !max_login_failed?
  12. end
  13. def increase_login_failed
  14. self.login_failed += 1
  15. save!
  16. end
  17. def requires_two_factor?
  18. two_factor.user_configured?
  19. end
  20. def two_factor_payload_valid?(two_factor_method, two_factor_payload)
  21. two_factor.verify?(two_factor_method, two_factor_payload)
  22. end
  23. private
  24. def max_login_failed?
  25. max_login_failed = Setting.get('password_max_login_failed').to_i
  26. return false if login_failed <= max_login_failed
  27. Rails.logger.info "Max login failed reached for user #{login}."
  28. true
  29. end
  30. def exists?
  31. present? && __getobj__.is_a?(::User)
  32. end
  33. end
  34. end