Renames confirmed_oauth_email to oauth_email

Refactors the way oauth_email is used to cover more cases (fixes pending specs)
This commit is contained in:
kikito
2016-01-26 12:44:55 +01:00
parent 92786764b7
commit a606c7aa8d
5 changed files with 31 additions and 27 deletions

View File

@@ -32,23 +32,26 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# If there are no problems with the email/username, then they were provided by oauth or they # If there are no problems with the email/username, then they were provided by oauth or they
# correspond to an existing user. Associate the identity and sign in # correspond to an existing user. Associate the identity and sign in
if @user.save unless @user.save
# If either the username or email have provoked a failure, we save the user anyway (but marked for revision)
# This mark will be detected by applicationcontroller and the user will be redirected to finish_signup
@user.registering_with_oauth = true
unless @user.save
# If we still can't save the user, the email might be invalidating devise's validatable "unique"
# constraint. Set email to nil and try again (we'll reset later using oauth_email)
@user.email = nil
@user.save
end
end
if @user.persisted?
identity.update(user: @user) identity.update(user: @user)
sign_in_and_redirect @user, event: :authentication sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else else
# If either the username or email have provoked a failure, we save the user anyway (but marked for revision) # If the failure is because something else happens, just present the "new user" form
# This mark will be detected by applicationcontroller and the user will be redirected to finish_signup session["devise.#{provider}_data"] = auth
@user.registering_with_oauth = true redirect_to new_user_registration_url
if @user.save
identity.update(user: @user)
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else
# If the failure is because something else happens, just present the "new user" form
session["devise.#{provider}_data"] = auth
redirect_to new_user_registration_url
end
end end
end end

View File

@@ -25,6 +25,7 @@ class Users::RegistrationsController < Devise::RegistrationsController
def finish_signup def finish_signup
current_user.registering_with_oauth = false current_user.registering_with_oauth = false
current_user.email = current_user.oauth_email if current_user.email.blank?
current_user.validate current_user.validate
end end
@@ -32,12 +33,12 @@ class Users::RegistrationsController < Devise::RegistrationsController
current_user.registering_with_oauth = false current_user.registering_with_oauth = false
if current_user.update(sign_up_params) if current_user.update(sign_up_params)
if current_user.confirmed_oauth_email != current_user.email if current_user.oauth_email != current_user.email
current_user.update(confirmed_at: nil) current_user.update(confirmed_at: nil)
current_user.send_confirmation_instructions current_user.send_confirmation_instructions
end end
if current_user.confirmed_oauth_email.present? if current_user.oauth_email.present?
current_user.update(confirmed_oauth_email: nil) current_user.update(oauth_email: nil)
end end
sign_in_and_redirect current_user, event: :authentication sign_in_and_redirect current_user, event: :authentication

View File

@@ -52,16 +52,16 @@ class User < ActiveRecord::Base
# Get the existing user by email if the provider gives us a verified email. # Get the existing user by email if the provider gives us a verified email.
def self.first_or_initialize_for_oauth(auth) def self.first_or_initialize_for_oauth(auth)
oauth_email = auth.info.email oauth_email = auth.info.email
confirmed_oauth_email = oauth_email if auth.info.verified || auth.info.verified_email oauth_email_confirmed = auth.info.verified || auth.info.verified_email
oauth_user = User.find_by(email: confirmed_oauth_email) if confirmed_oauth_email.present? oauth_user = User.find_by(email: oauth_email) if oauth_email_confirmed
oauth_user || User.new( oauth_user || User.new(
username: auth.info.name || auth.uid, username: auth.info.name || auth.uid,
email: oauth_email, email: oauth_email,
confirmed_oauth_email: confirmed_oauth_email, oauth_email: oauth_email,
password: Devise.friendly_token[0,20], password: Devise.friendly_token[0,20],
terms_of_service: '1', terms_of_service: '1',
confirmed_at: confirmed_oauth_email.present? ? DateTime.now : nil confirmed_at: oauth_email_confirmed ? DateTime.now : nil
) )
end end
@@ -174,11 +174,6 @@ class User < ActiveRecord::Base
!erased? && !registering_with_oauth !erased? && !registering_with_oauth
end end
# Deactivates the email uniqueness validation when registering with oauth
def email_changed?
!registering_with_oauth && super
end
def has_official_email? def has_official_email?
domain = Setting['email_domain_for_officials'] domain = Setting['email_domain_for_officials']
!email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") ) !email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") )

View File

@@ -0,0 +1,5 @@
class RenameConfirmedOauthEmailToOauthEmail < ActiveRecord::Migration
def change
rename_column :users, :confirmed_oauth_email, :oauth_email
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160125100637) do ActiveRecord::Schema.define(version: 20160126090634) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -398,7 +398,7 @@ ActiveRecord::Schema.define(version: 20160125100637) do
t.integer "notifications_count", default: 0 t.integer "notifications_count", default: 0
t.string "locale" t.string "locale"
t.boolean "registering_with_oauth", default: false t.boolean "registering_with_oauth", default: false
t.string "confirmed_oauth_email" t.string "oauth_email"
end end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree