1
0

unreserved_username_validator.rb 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. class UnreservedUsernameValidator < ActiveModel::Validator
  3. def validate(account)
  4. @username = account.username
  5. return if @username.blank?
  6. account.errors.add(:username, :reserved) if reserved_username?
  7. end
  8. private
  9. def reserved_username?
  10. pam_username_reserved? || settings_username_reserved?
  11. end
  12. def pam_username_reserved?
  13. pam_controlled? && pam_reserves_username?
  14. end
  15. def pam_controlled?
  16. Devise.pam_authentication && Devise.pam_controlled_service
  17. end
  18. def pam_reserves_username?
  19. Rpam2.account(Devise.pam_controlled_service, @username)
  20. end
  21. def settings_username_reserved?
  22. settings_has_reserved_usernames? && settings_reserves_username?
  23. end
  24. def settings_has_reserved_usernames?
  25. Setting.reserved_usernames.present?
  26. end
  27. def settings_reserves_username?
  28. Setting.reserved_usernames.include?(@username.downcase)
  29. end
  30. end