unreserved_username_validator.rb 627 B

123456789101112131415161718192021222324
  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 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