unreserved_username_validator.rb 629 B

1234567891011121314151617181920212223242526
  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