password_policy.rb 670 B

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # Check if password matches system settings
  3. class PasswordPolicy
  4. include ::Mixin::HasBackends
  5. attr_reader :password
  6. # @param password [String, nil] to evaluate. nil is treated as empty string
  7. def initialize(password)
  8. @password = password || ''
  9. end
  10. def valid?
  11. errors.blank?
  12. end
  13. def error
  14. errors.first
  15. end
  16. def errors
  17. @errors ||= applicable_backends
  18. .map { |backend| backend.new(password) }
  19. .reject(&:valid?)
  20. .map(&:error)
  21. end
  22. private
  23. def applicable_backends
  24. @applicable_backends ||= backends.select(&:applicable?)
  25. end
  26. end