Browse Source

Added support for configurable reserved usernames (fix of #1382) (#3566)

* Added support for configurable reserved usernames

* Added reserved usernames from mastodon issue 1355

* Fix reserved usernames
closed-social-glitch-2
Eugen Rochko 7 years ago
committed by GitHub
parent
commit
f7a30e2fae
5 changed files with 31 additions and 2 deletions
  1. +1
    -1
      app/models/account.rb
  2. +15
    -0
      app/validators/unreserved_validator.rb
  3. +1
    -0
      config/locales/en.yml
  4. +8
    -1
      config/settings.yml
  5. +6
    -0
      spec/models/account_spec.rb

+ 1
- 1
app/models/account.rb View File

@ -56,7 +56,7 @@ class Account < ApplicationRecord
# Local user validations # Local user validations
with_options if: :local? do 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 :display_name, length: { maximum: 30 }
validates :note, length: { maximum: 160 } validates :note, length: { maximum: 160 }
end end

+ 15
- 0
app/validators/unreserved_validator.rb View File

@ -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

+ 1
- 0
config/locales/en.yml View File

@ -40,6 +40,7 @@ en:
posts: Posts posts: Posts
remote_follow: Remote follow remote_follow: Remote follow
unfollow: Unfollow unfollow: Unfollow
reserved_username: The username is reserved
activitypub: activitypub:
activity: activity:
announce: announce:

+ 8
- 1
config/settings.yml View File

@ -7,7 +7,7 @@
# For more information, see docs/Running-Mastodon/Administration-guide.md # For more information, see docs/Running-Mastodon/Administration-guide.md
# #
defaults: &defaults defaults: &defaults
site_title: 'Mastodon'
site_title: Mastodon
site_description: '' site_description: ''
site_extended_description: '' site_extended_description: ''
site_contact_username: '' site_contact_username: ''
@ -27,6 +27,13 @@ defaults: &defaults
interactions: interactions:
must_be_follower: false must_be_follower: false
must_be_following: false must_be_following: false
reserved_usernames:
- admin
- support
- help
- root
- webmaster
- administrator
development: development:
<<: *defaults <<: *defaults

+ 6
- 0
spec/models/account_spec.rb View File

@ -381,6 +381,12 @@ RSpec.describe Account, type: :model do
expect(account_2).to model_have_error_on_field(:username) expect(account_2).to model_have_error_on_field(:username)
end end
it 'is invalid if the username is reserved' do
account = Fabricate.build(:account, username: 'support')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
context 'when is local' do context 'when is local' do
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
account = Fabricate.build(:account, username: 'the-doctor') account = Fabricate.build(:account, username: 'the-doctor')

Loading…
Cancel
Save