Multi-db fixes

This commit is contained in:
Hillel Coren 2017-05-10 18:18:48 +03:00
parent 2c8741bf96
commit 6a02c6e80e
6 changed files with 55 additions and 10 deletions

View File

@ -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,
];
}

View File

@ -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}");
}

View File

@ -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]);

View File

@ -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);
}
});

View File

@ -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 {

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddOauthToLookups extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('lookup_users', function ($table) {
$table->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');
});
}
}