2017-06-09 20:46:01 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UnreservedUsernameValidator < ActiveModel::Validator
|
|
|
|
def validate(account)
|
2019-01-03 06:10:02 +02:00
|
|
|
@username = account.username
|
|
|
|
return if @username.nil?
|
|
|
|
|
|
|
|
account.errors.add(:username, I18n.t('accounts.reserved_username')) if reserved_username?
|
2017-06-09 20:46:01 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-03 06:10:02 +02:00
|
|
|
def pam_controlled?
|
2018-02-02 11:18:55 +02:00
|
|
|
return false unless Devise.pam_authentication && Devise.pam_controlled_service
|
2019-01-03 06:10:02 +02:00
|
|
|
Rpam2.account(Devise.pam_controlled_service, @username).present?
|
2018-02-02 11:18:55 +02:00
|
|
|
end
|
|
|
|
|
2019-01-03 06:10:02 +02:00
|
|
|
def reserved_username?
|
|
|
|
return true if pam_controlled?
|
2017-06-09 20:46:01 +03:00
|
|
|
return false unless Setting.reserved_usernames
|
2019-01-03 06:10:02 +02:00
|
|
|
Setting.reserved_usernames.include?(@username.downcase)
|
2017-06-09 20:46:01 +03:00
|
|
|
end
|
|
|
|
end
|