From a73df4f72d9d5fbc118b80cea6f6bda4f877f5df Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Mon, 7 Apr 2014 14:09:29 +0300
Subject: [PATCH] Allow oauth signup without email

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
---
 app/controllers/application_controller.rb     |  7 +++++++
 app/controllers/profiles/emails_controller.rb |  2 +-
 app/controllers/profiles_controller.rb        |  1 +
 app/models/user.rb                            |  8 ++++++++
 app/views/profiles/show.html.haml             |  5 ++++-
 lib/gitlab/oauth/user.rb                      | 10 ++++++++--
 6 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 5f8b2da06f8..cfc9550b79e 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
   before_filter :default_headers
   before_filter :add_gon_variables
   before_filter :configure_permitted_parameters, if: :devise_controller?
+  before_filter :require_email
 
   protect_from_forgery
 
@@ -234,4 +235,10 @@ class ApplicationController < ActionController::Base
   def hexdigest(string)
     Digest::SHA1.hexdigest string
   end
+
+  def require_email
+    if current_user && current_user.temp_oauth_email?
+      redirect_to profile_path, notice: 'Please complete your profile with email address' and return
+    end
+  end
 end
diff --git a/app/controllers/profiles/emails_controller.rb b/app/controllers/profiles/emails_controller.rb
index 9996b67a8a4..40c352dab0c 100644
--- a/app/controllers/profiles/emails_controller.rb
+++ b/app/controllers/profiles/emails_controller.rb
@@ -8,7 +8,7 @@ class Profiles::EmailsController < ApplicationController
 
   def create
     @email = current_user.emails.new(params[:email])
-    
+
     flash[:alert] = @email.errors.full_messages.first unless @email.save
 
     redirect_to profile_emails_url
diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb
index 9234cd1708f..9c9a129b26b 100644
--- a/app/controllers/profiles_controller.rb
+++ b/app/controllers/profiles_controller.rb
@@ -3,6 +3,7 @@ class ProfilesController < ApplicationController
 
   before_filter :user
   before_filter :authorize_change_username!, only: :update_username
+  skip_before_filter :require_email, only: [:show, :update]
 
   layout 'profile'
 
diff --git a/app/models/user.rb b/app/models/user.rb
index 25c10a6faa0..295f7d783be 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -462,4 +462,12 @@ class User < ActiveRecord::Base
   def all_ssh_keys
     keys.map(&:key)
   end
+
+  def temp_oauth_email?
+    email =~ /\Atemp-email-for-oauth/
+  end
+
+  def generate_tmp_oauth_email
+    self.email = "temp-email-for-oauth-#{username}"
+  end
 end
diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml
index 523a07db400..fb670fdc3a5 100644
--- a/app/views/profiles/show.html.haml
+++ b/app/views/profiles/show.html.haml
@@ -30,7 +30,10 @@
             %span.help-block.light
               Email is read-only for LDAP user
           - else
-            = f.text_field :email, class: "form-control", required: true
+            - if @user.temp_oauth_email?
+              = f.text_field :email, class: "form-control", required: true, value: nil
+            - else
+              = f.text_field :email, class: "form-control", required: true
             - if @user.unconfirmed_email.present?
               %span.help-block
                 We sent confirmation email to
diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb
index 529753c4019..65d692034ba 100644
--- a/lib/gitlab/oauth/user.rb
+++ b/lib/gitlab/oauth/user.rb
@@ -29,7 +29,13 @@ module Gitlab
 
           user = model.build_user(opts, as: :admin)
           user.skip_confirmation!
-          user.save!
+
+          if user.email.blank?
+            user.generate_tmp_oauth_email
+          end
+
+          user.save!(validate: false)
+
           log.info "(OAuth) Creating user #{email} from login with extern_uid => #{uid}"
 
           if Gitlab.config.omniauth['block_auto_created_users'] && !ldap?
@@ -58,7 +64,7 @@ module Gitlab
         end
 
         def username
-          email.match(/^[^@]*/)[0]
+          auth.info.nickname.to_s.force_encoding("utf-8")
         end
 
         def provider
-- 
GitLab