From f40e87a03bdaaafa72e5afede57581d6c54dc506 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 24 Jun 2014 12:57:41 +0200 Subject: [PATCH] Return better error when account exists when attempting oauth account create. --- app/controllers/omniauth_callbacks_controller.rb | 13 +++++++++---- lib/gitlab/oauth/user.rb | 8 +++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/controllers/omniauth_callbacks_controller.rb b/app/controllers/omniauth_callbacks_controller.rb index 0c87fe0d9ae..477743b34c8 100644 --- a/app/controllers/omniauth_callbacks_controller.rb +++ b/app/controllers/omniauth_callbacks_controller.rb @@ -45,14 +45,19 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController # Create user if does not exist # and allow_single_sign_on is true - if Gitlab.config.omniauth['allow_single_sign_on'] - @user ||= Gitlab::OAuth::User.create(oauth) + if Gitlab.config.omniauth['allow_single_sign_on'] && !@user + @user, errors = Gitlab::OAuth::User.create(oauth) end - if @user + if @user && !errors sign_in_and_redirect(@user) else - flash[:notice] = "There's no such user!" + if errors + error_message = errors.map{ |attribute, message| "#{attribute} #{message}" }.join(", ") + flash[:notice] = "There was a problem creating your account. #{error_message}" + else + flash[:notice] = "There's no such user!" + end redirect_to new_user_session_path end end diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb index c5be884a895..38e33c0eee5 100644 --- a/lib/gitlab/oauth/user.rb +++ b/lib/gitlab/oauth/user.rb @@ -44,7 +44,13 @@ module Gitlab user.username = email_username.gsub("'", "") end - user.save! + begin + user.save! + rescue ActiveRecord::RecordInvalid => e + log.info "(OAuth) Email #{e.record.errors[:email]}. Username #{e.record.errors[:username]}" + return nil, e.record.errors + end + log.info "(OAuth) Creating user #{email} from login with extern_uid => #{uid}" if Gitlab.config.omniauth['block_auto_created_users'] && !ldap?