]> git.openstreetmap.org Git - rails.git/commitdiff
Reorder auth_success handlers
authorTom Hughes <tom@compton.nu>
Sun, 14 Aug 2016 17:30:13 +0000 (18:30 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 14 Aug 2016 17:30:13 +0000 (18:30 +0100)
Only treat auth_success as a possible login attempt if we're not
in the middle of validating a new user, or a change to a user.

Also validate the uniqueness of external auth credentials at the
rails level rather than just at the database level, and make sure
any errors are properly reported.

Fixes #1265

app/controllers/user_controller.rb
app/models/user.rb

index e5515f727885e3464bb8372e8e7ce41f2b3a8f72..c8abb4d85a3f84ce1389e956b72cad9f558c8319 100644 (file)
@@ -131,6 +131,10 @@ class UserController < ApplicationController
         session[:new_user_settings] = params
         redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
       end
         session[:new_user_settings] = params
         redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
       end
+    elsif errors = session.delete(:user_errors)
+      errors.each do |attribute, error|
+        @user.errors.add(attribute, error)
+      end
     end
   end
 
     end
   end
 
@@ -501,31 +505,14 @@ class UserController < ApplicationController
       email_verified = false
     end
 
       email_verified = false
     end
 
-    user = User.find_by_auth_provider_and_auth_uid(provider, uid)
-
-    if user.nil? && provider == "google"
-      openid_url = auth_info[:extra][:id_info]["openid_id"]
-      user = User.find_by_auth_provider_and_auth_uid("openid", openid_url) if openid_url
-      user.update(:auth_provider => provider, :auth_uid => uid) if user
-    end
-
-    if user
-      case user.status
-      when "pending" then
-        unconfirmed_login(user)
-      when "active", "confirmed" then
-        successful_login(user, env["omniauth.params"]["referer"])
-      when "suspended" then
-        failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}")
-      else
-        failed_login t("user.login.auth failure")
-      end
-    elsif settings = session.delete(:new_user_settings)
+    if settings = session.delete(:new_user_settings)
       @user.auth_provider = provider
       @user.auth_uid = uid
 
       update_user(@user, settings)
 
       @user.auth_provider = provider
       @user.auth_uid = uid
 
       update_user(@user, settings)
 
+      session[:user_errors] = @user.errors.as_json
+
       redirect_to :action => "account", :display_name => @user.display_name
     elsif session[:new_user]
       session[:new_user].auth_provider = provider
       redirect_to :action => "account", :display_name => @user.display_name
     elsif session[:new_user]
       session[:new_user].auth_provider = provider
@@ -537,8 +524,29 @@ class UserController < ApplicationController
 
       redirect_to :action => "terms"
     else
 
       redirect_to :action => "terms"
     else
-      redirect_to :action => "new", :nickname => name, :email => email,
-                  :auth_provider => provider, :auth_uid => uid
+      user = User.find_by_auth_provider_and_auth_uid(provider, uid)
+
+      if user.nil? && provider == "google"
+        openid_url = auth_info[:extra][:id_info]["openid_id"]
+        user = User.find_by_auth_provider_and_auth_uid("openid", openid_url) if openid_url
+        user.update(:auth_provider => provider, :auth_uid => uid) if user
+      end
+
+      if user
+        case user.status
+        when "pending" then
+          unconfirmed_login(user)
+        when "active", "confirmed" then
+          successful_login(user, env["omniauth.params"]["referer"])
+        when "suspended" then
+          failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}")
+        else
+          failed_login t("user.login.auth failure")
+        end
+      else
+        redirect_to :action => "new", :nickname => name, :email => email,
+                    :auth_provider => provider, :auth_uid => uid
+      end
     end
   end
 
     end
   end
 
index e255dc2169b1158cb735f4a3afc244223449409a..a550b9f05fd2a8ddd891b9dd134e58a04898d88a 100644 (file)
@@ -52,6 +52,8 @@ class User < ActiveRecord::Base
   validates :home_zoom, :allow_nil => true, :numericality => { :only_integer => true }
   validates :preferred_editor, :inclusion => Editors::ALL_EDITORS, :allow_nil => true
   validates :image, :attachment_content_type => { :content_type => %r{\Aimage/.*\Z} }
   validates :home_zoom, :allow_nil => true, :numericality => { :only_integer => true }
   validates :preferred_editor, :inclusion => Editors::ALL_EDITORS, :allow_nil => true
   validates :image, :attachment_content_type => { :content_type => %r{\Aimage/.*\Z} }
+  validates :auth_uid, :unless => proc { |u| u.auth_provider.nil? },
+                       :uniqueness => { :scope => :auth_provider }
 
   validates_email_format_of :email, :if => proc { |u| u.email_changed? }
   validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? }
 
   validates_email_format_of :email, :if => proc { |u| u.email_changed? }
   validates_email_format_of :new_email, :allow_blank => true, :if => proc { |u| u.new_email_changed? }