mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Multi-db fixes
This commit is contained in:
parent
2c8741bf96
commit
6a02c6e80e
@ -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,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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}");
|
||||
}
|
||||
|
||||
|
@ -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]);
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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');
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user