2017-06-10 03:46:01 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UnreservedUsernameValidator < ActiveModel::Validator
|
|
|
|
def validate(account)
|
2019-01-03 15:10:02 +11:00
|
|
|
@username = account.username
|
|
|
|
|
2021-03-01 14:59:13 +11:00
|
|
|
return if @username.blank?
|
|
|
|
|
|
|
|
account.errors.add(:username, :reserved) if reserved_username?
|
2017-06-10 03:46:01 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-03 15:10:02 +11:00
|
|
|
def pam_controlled?
|
2018-02-02 20:18:55 +11:00
|
|
|
return false unless Devise.pam_authentication && Devise.pam_controlled_service
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2019-01-03 15:10:02 +11:00
|
|
|
Rpam2.account(Devise.pam_controlled_service, @username).present?
|
2018-02-02 20:18:55 +11:00
|
|
|
end
|
|
|
|
|
2019-01-03 15:10:02 +11:00
|
|
|
def reserved_username?
|
|
|
|
return true if pam_controlled?
|
2017-06-10 03:46:01 +10:00
|
|
|
return false unless Setting.reserved_usernames
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2019-01-03 15:10:02 +11:00
|
|
|
Setting.reserved_usernames.include?(@username.downcase)
|
2017-06-10 03:46:01 +10:00
|
|
|
end
|
|
|
|
end
|