Extracted nickname registration out of the UsersController and into its

own service.
This commit is contained in:
Einar Jonsson 2013-09-06 09:35:29 +00:00
parent c59c2a4bb3
commit 724b3aadcf
3 changed files with 176 additions and 63 deletions

View File

@ -119,77 +119,17 @@ class UsersController < ApplicationController
# The special case where someone is changing the case of their own username
return render_available_true if changing_case_of_own_username(target_user, username)
validator = UsernameValidator.new(username)
if !validator.valid_format?
render json: {errors: validator.errors}
elsif !SiteSetting.call_discourse_hub?
check_username_locally(username)
else
check_username_with_hub_server(target_user, username)
end
checker = UsernameCheckerService.new
email = params[:email] || target_user.try(:email)
render(json: checker.check_username(username, email))
rescue RestClient::Forbidden
render json: {errors: [I18n.t("discourse_hub.access_token_problem")]}
end
def check_username_locally(username)
if User.username_available?(username)
render_available_true
else
render_unavailable_with_suggestion(UserNameSuggester.suggest(username))
end
end
def user_from_params_or_current_user
params[:for_user_id] ? User.find(params[:for_user_id]) : current_user
end
def available_globally_and_suggestion_from_hub(target_user, username, email_given)
if email_given
global_match, available, suggestion =
DiscourseHub.nickname_match?(username, params[:email] || target_user.email)
{ available_globally: available || global_match,
suggestion_from_discourse_hub: suggestion,
global_match: global_match }
else
args = DiscourseHub.nickname_available?(username)
{ available_globally: args[0],
suggestion_from_discourse_hub: args[1],
global_match: false }
end
end
# Contact the Discourse Hub server
def check_username_with_hub_server(target_user, username)
email_given = (params[:email].present? || target_user.present?)
available_locally = User.username_available?(username)
info = available_globally_and_suggestion_from_hub(target_user, username, email_given)
available_globally = info[:available_globally]
suggestion_from_discourse_hub = info[:suggestion_from_discourse_hub]
global_match = info[:global_match]
if available_globally && available_locally
render json: { available: true, global_match: (global_match ? true : false) }
elsif available_locally && !available_globally
if email_given
# Nickname and email do not match what's registered on the discourse hub.
render json: { available: false, global_match: false, suggestion: suggestion_from_discourse_hub }
else
# The nickname is available locally, but is registered on the discourse hub.
# We need an email to see if the nickname belongs to this person.
# Don't give a suggestion until we get the email and try to match it with on the discourse hub.
render json: { available: false }
end
elsif available_globally && !available_locally
# Already registered on this site with the matching nickname and email address. Why are you signing up again?
render json: { available: false, suggestion: UserNameSuggester.suggest(username) }
else
# Not available anywhere.
render_unavailable_with_suggestion(suggestion_from_discourse_hub)
end
end
def render_unavailable_with_suggestion(suggestion)
render json: { available: false, suggestion: suggestion }
end
def create
return fake_success_response if suspicious? params

View File

@ -0,0 +1,69 @@
class UsernameCheckerService
def check_username(username, email)
validator = UsernameValidator.new(username)
if !validator.valid_format?
{errors: validator.errors}
elsif !SiteSetting.call_discourse_hub?
check_username_locally(username)
else
check_username_with_hub_server(username, email)
end
end
# Contact the Discourse Hub server
def check_username_with_hub_server(username, email)
available_locally = User.username_available?(username)
info = available_globally_and_suggestion_from_hub(username, email)
available_globally = info[:available_globally]
suggestion_from_discourse_hub = info[:suggestion_from_discourse_hub]
global_match = info[:global_match]
if available_globally && available_locally
{ available: true, global_match: (global_match ? true : false) }
elsif available_locally && !available_globally
if email.present?
# Nickname and email do not match what's registered on the discourse hub.
{ available: false, global_match: false, suggestion: suggestion_from_discourse_hub }
else
# The nickname is available locally, but is registered on the discourse hub.
# We need an email to see if the nickname belongs to this person.
# Don't give a suggestion until we get the email and try to match it with on the discourse hub.
{ available: false }
end
elsif available_globally && !available_locally
# Already registered on this site with the matching nickname and email address. Why are you signing up again?
{ available: false, suggestion: UserNameSuggester.suggest(username) }
else
# Not available anywhere.
render_unavailable_with_suggestion(suggestion_from_discourse_hub)
end
end
def render_unavailable_with_suggestion(suggestion)
{ available: false, suggestion: suggestion }
end
def check_username_locally(username)
if User.username_available?(username)
{ available: true }
else
{ available: false, suggestion: UserNameSuggester.suggest(username) }
end
end
def available_globally_and_suggestion_from_hub(username, email)
if email.present?
global_match, available, suggestion =
DiscourseHub.nickname_match?(username, email)
{ available_globally: available || global_match,
suggestion_from_discourse_hub: suggestion,
global_match: global_match }
else
args = DiscourseHub.nickname_available?(username)
{ available_globally: args[0],
suggestion_from_discourse_hub: args[1],
global_match: false }
end
end
end

View File

@ -0,0 +1,104 @@
require 'spec_helper'
describe UsernameCheckerService do
describe 'check_username' do
before do
@service = UsernameCheckerService.new
@nil_email = nil
@email = 'vincentvega@example.com'
end
context 'Username invalid' do
it 'rejects blank usernames' do
result = @service.check_username('', @nil_email)
expect(result).to have_key(:errors)
end
it 'rejects too short usernames' do
result = @service.check_username('a', @nil_email)
expect(result).to have_key(:errors)
end
it 'rejects too long usernames' do
result = @service.check_username('a123456789b123456789c123456789', @nil_email)
expect(result).to have_key(:errors)
end
it 'rejects usernames with invalid characters' do
result = @service.check_username('vincent-', @nil_email)
expect(result).to have_key(:errors)
end
it 'rejects usernames that do not start with an alphanumeric character' do
result = @service.check_username('_vincent', @nil_email)
expect(result).to have_key(:errors)
end
end
context 'Using Discourse Hub' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
end
context 'and email is given' do
it 'username is available locally but not globally' do
DiscourseHub.stubs(:nickname_available?).returns([false, 'suggestion'])
DiscourseHub.stubs(:nickname_match?).returns([true, false, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: true }
expect(result).to eq(expected)
end
end
it 'username is available both locally and globally' do
DiscourseHub.stubs(:nickname_available?).returns([true, nil])
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: false }
expect(result).to eq(expected)
end
it 'username is available locally but not globally' do
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: false }
expect(result).to eq(expected)
end
it 'username is available globally but not locally' do
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil])
User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j')
expected = { available: false, suggestion: 'einar-j' }
result = @service.check_username('vincent', @email)
expect(result).to eq(expected)
end
it 'username not available anywhere' do
DiscourseHub.stubs(:nickname_match?).returns([false, false, 'suggestion'])
expected = { available: false, suggestion: 'suggestion', global_match: false }
@nil_email = nil
result = @service.check_username('vincent', @email)
expect(result).to eq(expected)
end
end
context 'Discourse Hub disabled' do
it 'username not available locally' do
User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j')
result = @service.check_username('vincent', @nil_email)
result[:available].should be_false
result[:suggestion].should eq('einar-j')
end
it 'username available locally' do
User.stubs(:username_available?).returns(true)
result = @service.check_username('vincent', @nil_email)
result[:available].should be_true
end
end
end
end