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
|
|
|
|
|
2021-03-01 05:59:13 +02:00
|
|
|
return if @username.blank?
|
|
|
|
|
|
|
|
account.errors.add(:username, :reserved) if reserved_username?
|
2017-06-09 20:46:01 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-10-17 14:05:28 +03:00
|
|
|
def reserved_username?
|
|
|
|
pam_username_reserved? || settings_username_reserved?
|
|
|
|
end
|
|
|
|
|
|
|
|
def pam_username_reserved?
|
|
|
|
pam_controlled? && pam_reserves_username?
|
|
|
|
end
|
|
|
|
|
2019-01-03 06:10:02 +02:00
|
|
|
def pam_controlled?
|
2023-10-17 14:05:28 +03:00
|
|
|
Devise.pam_authentication && Devise.pam_controlled_service
|
|
|
|
end
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2023-10-17 14:05:28 +03:00
|
|
|
def pam_reserves_username?
|
|
|
|
Rpam2.account(Devise.pam_controlled_service, @username)
|
2018-02-02 11:18:55 +02:00
|
|
|
end
|
|
|
|
|
2023-10-17 14:05:28 +03:00
|
|
|
def settings_username_reserved?
|
|
|
|
settings_has_reserved_usernames? && settings_reserves_username?
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings_has_reserved_usernames?
|
|
|
|
Setting.reserved_usernames.present?
|
|
|
|
end
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2023-10-17 14:05:28 +03:00
|
|
|
def settings_reserves_username?
|
2019-01-03 06:10:02 +02:00
|
|
|
Setting.reserved_usernames.include?(@username.downcase)
|
2017-06-09 20:46:01 +03:00
|
|
|
end
|
|
|
|
end
|