2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-22 15:49:52 -04:00
|
|
|
class MaxUsernameLengthValidator
|
2022-07-08 11:00:47 -04:00
|
|
|
MAX_USERNAME_LENGTH_RANGE = 8..60
|
|
|
|
|
2018-08-22 15:49:52 -04:00
|
|
|
def initialize(opts = {})
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(value)
|
2022-07-08 11:00:47 -04:00
|
|
|
if !MAX_USERNAME_LENGTH_RANGE.cover?(value)
|
|
|
|
@max_range_violation = true
|
|
|
|
return false
|
|
|
|
end
|
2018-08-22 15:49:52 -04:00
|
|
|
return false if value < SiteSetting.min_username_length
|
2019-10-21 06:32:27 -04:00
|
|
|
@username = User.where('length(username) > ?', value).pluck_first(:username)
|
2018-10-02 03:20:19 -04:00
|
|
|
@username.blank?
|
2018-08-22 15:49:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2022-07-08 11:00:47 -04:00
|
|
|
if @max_range_violation
|
|
|
|
I18n.t('site_settings.errors.invalid_integer_min_max', min: MAX_USERNAME_LENGTH_RANGE.begin, max: MAX_USERNAME_LENGTH_RANGE.end)
|
|
|
|
elsif @username.blank?
|
2018-08-22 15:49:52 -04:00
|
|
|
I18n.t("site_settings.errors.max_username_length_range")
|
|
|
|
else
|
|
|
|
I18n.t("site_settings.errors.max_username_length_exists", username: @username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|