From 916a3f33f200f9ffa26588036b95481a08fbd3cb Mon Sep 17 00:00:00 2001 From: Einar Jonsson Date: Sun, 18 Aug 2013 14:37:38 +0000 Subject: [PATCH] Refactored user activation business logic out of UsersController and into a UserActivator class. --- app/controllers/users_controller.rb | 38 ++++++++----------- lib/user_activator.rb | 57 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 lib/user_activator.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7addd1014fc..bd309f1a2b0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,6 @@ require_dependency 'discourse_hub' require_dependency 'user_name_suggester' +require_dependency 'user_activator' class UsersController < ApplicationController @@ -160,33 +161,16 @@ class UsersController < ApplicationController return fake_success_response if suspicious? params user = User.new_from_params(params) - - auth = session[:authentication] - if valid_session_authentication?(auth, params[:email]) - user.active = true - end - user.password_required! unless auth + auth = authenticate_user(user, params) if user.valid? && SiteSetting.call_discourse_hub? DiscourseHub.register_nickname(user.username, user.email) end - if user.save - if SiteSetting.must_approve_users? - message = I18n.t("login.wait_approval") - elsif !user.active? - message = I18n.t("login.activate_email", email: user.email) - Jobs.enqueue(:user_email, - type: :signup, - user_id: user.id, - email_token: user.email_tokens.first.token - ) - else - message = I18n.t("login.active") - log_on_user(user) - user.enqueue_welcome_message('welcome_user') - end + if user.save + activator = UserActivator.new(user, session, cookies) + message = activator.activation_message create_third_party_auth_records(user, auth) if auth.present? # Clear authentication session. @@ -210,12 +194,22 @@ class UsersController < ApplicationController errors:I18n.t( "login.not_available", suggestion: UserNameSuggester.suggest(params[:username]) ) - ) + ) } rescue RestClient::Forbidden render json: { errors: [I18n.t("discourse_hub.access_token_problem")] } end + def authenticate_user(user, params) + auth = session[:authentication] + if valid_session_authentication?(auth, params[:email]) + user.active = true + end + user.password_required! unless auth + + auth + end + def get_honeypot_value render json: {value: honeypot_value, challenge: challenge_value} end diff --git a/lib/user_activator.rb b/lib/user_activator.rb new file mode 100644 index 00000000000..d6d4b40b0a5 --- /dev/null +++ b/lib/user_activator.rb @@ -0,0 +1,57 @@ +# Responsible for dealing with different activation processes when a user is created +class UserActivator + attr_reader :user, :session, :cookies + def initialize(user, session, cookies) + @user = user + @session = session + @cookies = cookies + end + + def activation_message + activator.activate + end + + private + + def activator + factory.new(user, session, cookies) + end + + def factory + if SiteSetting.must_approve_users? + ApprovalActivator + elsif !user.active? + EmailActivator + else + LoginActivator + end + + end +end + +class ApprovalActivator < UserActivator + def activate + I18n.t("login.wait_approval") + end +end + +class EmailActivator < UserActivator + def activate + Jobs.enqueue(:user_email, + type: :signup, + user_id: user.id, + email_token: user.email_tokens.first.token + ) + I18n.t("login.activate_email", email: user.email) + end +end + +class LoginActivator < UserActivator + include CurrentUser + + def activate + log_on_user(user) + user.enqueue_welcome_message('welcome_user') + I18n.t("login.active") + end +end