unreserved_username_validator.rb 651 B

1234567891011121314151617181920212223
  1. # frozen_string_literal: true
  2. class UnreservedUsernameValidator < ActiveModel::Validator
  3. def validate(account)
  4. @username = account.username
  5. return if @username.nil?
  6. account.errors.add(:username, I18n.t('accounts.reserved_username')) if reserved_username?
  7. end
  8. private
  9. def pam_controlled?
  10. return false unless Devise.pam_authentication && Devise.pam_controlled_service
  11. Rpam2.account(Devise.pam_controlled_service, @username).present?
  12. end
  13. def reserved_username?
  14. return true if pam_controlled?
  15. return false unless Setting.reserved_usernames
  16. Setting.reserved_usernames.include?(@username.downcase)
  17. end
  18. end