forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
username_checker_service.rb
38 lines (32 loc) · 1.02 KB
/
username_checker_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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