discourse/app/services/username_checker_service.rb

32 lines
811 B
Ruby
Raw Normal View History

class UsernameCheckerService
def check_username(username, email)
if username && username.length > 0
validator = UsernameValidator.new(username)
if !validator.valid_format?
{errors: validator.errors}
else
2016-04-01 05:33:25 +08:00
check_username_availability(username, email)
end
end
end
2016-04-01 05:33:25 +08:00
def check_username_availability(username, email)
if User.username_available?(username)
2016-04-01 05:33:25 +08:00
{ available: true, is_developer: is_developer?(email) }
else
{ available: false, suggestion: UserNameSuggester.suggest(username) }
end
end
2016-04-01 05:33:25 +08:00
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