diff --git a/app/Console/Commands/InitLookup.php b/app/Console/Commands/InitLookup.php index d639068b035f..b85892dbfcfc 100644 --- a/app/Console/Commands/InitLookup.php +++ b/app/Console/Commands/InitLookup.php @@ -147,6 +147,7 @@ class InitLookup extends Command 'lookup_account_id' => $lookupAccount->id, 'email' => $user['email'] ?: null, 'user_id' => $user['user_id'], + 'oauth_user_key' => $user['oauth_user_key'], ]); } } @@ -223,11 +224,12 @@ class InitLookup extends Command 'tokens' => [], ]; - $users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get(['email', 'id']); + $users = DB::table('users')->whereAccountId($accountId)->orderBy('id')->get(['email', 'id', 'oauth_user_id', 'oauth_provider_id']); foreach ($users as $user) { $data['users'][] = [ 'email' => $user->email, 'user_id' => $user->id, + 'oauth_user_key' => ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null, ]; } diff --git a/app/Models/LookupModel.php b/app/Models/LookupModel.php index 677a2d6b8f1f..719e5be04918 100644 --- a/app/Models/LookupModel.php +++ b/app/Models/LookupModel.php @@ -83,7 +83,14 @@ class LookupModel extends Eloquent static::setDbServer($server); // check entity is found on the server - if (! $entity::where($field, '=', $value)->first()) { + if ($field === 'oauth_user_key') { + list($providerId, $oauthId) = explode('-', $value); + $isFound = $entity::where('oauth_provider_id', '=', $providerId) + ->where('oauth_user_id', '=', $oauthId)->first(); + } else { + $isFound = $entity::where($field, '=', $value)->first(); + } + if (! $isFound) { abort("Looked up {$className} not found: {$field} => {$value}"); } diff --git a/app/Models/LookupUser.php b/app/Models/LookupUser.php index 18703aa90fea..697c59a70a3d 100644 --- a/app/Models/LookupUser.php +++ b/app/Models/LookupUser.php @@ -18,9 +18,10 @@ class LookupUser extends LookupModel 'email', 'user_id', 'confirmation_code', + 'oauth_user_key', ]; - public static function updateUser($accountKey, $userId, $email, $confirmationCode) + public static function updateUser($accountKey, $user) { if (! env('MULTI_DB_ENABLED')) { return; @@ -33,11 +34,12 @@ class LookupUser extends LookupModel ->firstOrFail(); $lookupUser = LookupUser::whereLookupAccountId($lookupAccount->id) - ->whereUserId($userId) + ->whereUserId($user->id) ->firstOrFail(); - $lookupUser->email = $email; - $lookupUser->confirmation_code = $confirmationCode; + $lookupUser->email = $user->email; + $lookupUser->confirmation_code = $user->confirmation_code; + $lookupUser->oauth_user_key = ($user->oauth_provider_id && $user->oauth_user_id) ? ($user->oauth_provider_id . '-' . $user->oauth_user_id) : null; $lookupUser->save(); config(['database.default' => $current]); diff --git a/app/Models/User.php b/app/Models/User.php index 5cce5fd27162..f8d647ea142a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -426,8 +426,11 @@ User::updating(function ($user) { User::onUpdatingUser($user); $dirty = $user->getDirty(); - if (array_key_exists('email', $dirty) || array_key_exists('confirmation_code', $dirty)) { - LookupUser::updateUser($user->account->account_key, $user->id, $user->email, $user->confirmation_code); + if (array_key_exists('email', $dirty) + || array_key_exists('confirmation_code', $dirty) + || array_key_exists('oauth_user_id', $dirty) + || array_key_exists('oauth_provider_id', $dirty)) { + LookupUser::updateUser($user->account->account_key, $user); } }); diff --git a/app/Services/AuthService.php b/app/Services/AuthService.php index 2c395f01eae6..b07758c4e71c 100644 --- a/app/Services/AuthService.php +++ b/app/Services/AuthService.php @@ -82,9 +82,9 @@ class AuthService Session::flash('error', $result); } } else { - LookupUser::setServerByField('email', $email); + LookupUser::setServerByField('oauth_user_key', $providerId . '-' . $oauthUserId); - if ($user = $this->accountRepo->findUserByOauth($providerId, $socialiteUser->id)) { + if ($user = $this->accountRepo->findUserByOauth($providerId, $oauthUserId)) { Auth::login($user, true); event(new UserLoggedIn()); } else { diff --git a/database/migrations/2017_05_10_144928_add_oauth_to_lookups.php b/database/migrations/2017_05_10_144928_add_oauth_to_lookups.php new file mode 100644 index 000000000000..8f3d78afab5b --- /dev/null +++ b/database/migrations/2017_05_10_144928_add_oauth_to_lookups.php @@ -0,0 +1,31 @@ +string('oauth_user_key')->nullable()->unique(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('lookup_users', function ($table) { + $table->dropColumn('oauth_user_key'); + }); + } +}