user.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Auth
  3. class User < SimpleDelegator
  4. attr_reader :user
  5. def initialize(username)
  6. @user = ::User.identify(username)
  7. super(@user)
  8. end
  9. # Checks if a given user can login. Check for the following criteria:
  10. # * valid user
  11. # * user is active
  12. # * user has not reached the maximum of failed login tries
  13. #
  14. # @return [Boolean] true if the user can login, false otherwise.
  15. def can_login?
  16. return false if !exists?
  17. return false if !active?
  18. !max_login_failed?
  19. end
  20. # Increase the current failed login count for the user.
  21. def increase_login_failed
  22. self.login_failed += 1
  23. save!
  24. end
  25. private
  26. # Checks if a user has reached the maximum of failed login tries.
  27. #
  28. # @return [Boolean] true if the user has reached the maximum of failed login tries, otherwise false.
  29. def max_login_failed?
  30. max_login_failed = Setting.get('password_max_login_failed').to_i
  31. return false if login_failed <= max_login_failed
  32. Rails.logger.info "Max login failed reached for user #{login}."
  33. true
  34. end
  35. # Checks if a valid user exists.
  36. #
  37. # @return [Boolean] true if a valid user exists, otherwise false.
  38. def exists?
  39. present? && __getobj__.is_a?(::User)
  40. end
  41. end
  42. end