mirror of
https://github.com/discourse/discourse.git
synced 2024-12-13 19:43:52 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
42 lines
1.0 KiB
Ruby
42 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class UsernameCheckerService
|
|
def initialize(allow_reserved_username: false)
|
|
@allow_reserved_username = allow_reserved_username
|
|
end
|
|
|
|
def check_username(username, email)
|
|
if username && username.length > 0
|
|
validator = UsernameValidator.new(username)
|
|
if !validator.valid_format?
|
|
{ errors: validator.errors }
|
|
else
|
|
check_username_availability(username, email)
|
|
end
|
|
end
|
|
end
|
|
|
|
def check_username_availability(username, email)
|
|
available = User.username_available?(
|
|
username,
|
|
email,
|
|
allow_reserved_username: @allow_reserved_username
|
|
)
|
|
|
|
if available
|
|
{ available: true, is_developer: is_developer?(email) }
|
|
else
|
|
{ available: false, suggestion: UserNameSuggester.suggest(username) }
|
|
end
|
|
end
|
|
|
|
def is_developer?(value)
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
end
|
|
|
|
def self.is_developer?(email)
|
|
UsernameCheckerService.new.is_developer?(email)
|
|
end
|
|
|
|
end
|