diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/account.rb | 2 | ||||
-rw-r--r-- | app/validators/unreserved_validator.rb | 15 |
2 files changed, 16 insertions, 1 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 5b0d2d65c..918d5b0f1 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -56,7 +56,7 @@ class Account < ApplicationRecord # Local user validations with_options if: :local? do - validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 } + validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true validates :display_name, length: { maximum: 30 } validates :note, length: { maximum: 160 } end diff --git a/app/validators/unreserved_validator.rb b/app/validators/unreserved_validator.rb new file mode 100644 index 000000000..4e5b9dafc --- /dev/null +++ b/app/validators/unreserved_validator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class UnreservedValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + return if value.nil? + record.errors.add(attribute, I18n.t('accounts.reserved_username')) if reserved_username?(value) + end + + private + + def reserved_username?(value) + return false unless Setting.reserved_usernames + Setting.reserved_usernames.include?(value.downcase) + end +end |