From fec7f6ad9b5a789bada5b088c6e167f67f94f897 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 10 May 2017 12:42:15 +0300 Subject: [PATCH] Multi-db fixes --- app/Http/Middleware/DatabaseLookup.php | 15 +++++++++------ app/Http/Middleware/StartupCheck.php | 6 ++++++ app/Listeners/HandleUserSignedUp.php | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/Http/Middleware/DatabaseLookup.php b/app/Http/Middleware/DatabaseLookup.php index 314cabee4d..d1eb157663 100644 --- a/app/Http/Middleware/DatabaseLookup.php +++ b/app/Http/Middleware/DatabaseLookup.php @@ -22,13 +22,13 @@ class DatabaseLookup if ($guard == 'user') { if ($code = $request->confirmation_code) { LookupUser::setServerByField('confirmation_code', $code); - } elseif ($server = session(SESSION_DB_SERVER)) { - config(['database.default' => $server]); - $user = Auth::user()->fresh(); - $user->load('account'); - Auth::setUser($user); - } elseif ($email = $request->email) { + } elseif (session(SESSION_DB_SERVER)) { + // do nothing + } elseif (! Auth::check() && $email = $request->email) { LookupUser::setServerByField('email', $email); + } else { + Auth::logout(); + return redirect('/login'); } } elseif ($guard == 'api') { if ($token = $request->header('X-Ninja-Token')) { @@ -41,6 +41,9 @@ class DatabaseLookup LookupInvitation::setServerByField('invitation_key', $key); } elseif ($key = request()->contact_key ?: session('contact_key')) { LookupContact::setServerByField('contact_key', $key); + } else { + Auth::logout(); + return redirect('/client/sessionexpired'); } } elseif ($guard == 'postmark') { LookupInvitation::setServerByField('message_id', request()->MessageID); diff --git a/app/Http/Middleware/StartupCheck.php b/app/Http/Middleware/StartupCheck.php index a1aabd9f7b..de8f7be8a5 100644 --- a/app/Http/Middleware/StartupCheck.php +++ b/app/Http/Middleware/StartupCheck.php @@ -66,6 +66,12 @@ class StartupCheck } } + if (env('MULTI_DB_ENABLED')) { + if ($server = session(SESSION_DB_SERVER)) { + config(['database.default' => $server]); + } + } + // Check the application is up to date and for any news feed messages if (Auth::check()) { $count = Session::get(SESSION_COUNTER, 0); diff --git a/app/Listeners/HandleUserSignedUp.php b/app/Listeners/HandleUserSignedUp.php index e76f6c1c41..4043d2dd0b 100644 --- a/app/Listeners/HandleUserSignedUp.php +++ b/app/Listeners/HandleUserSignedUp.php @@ -55,5 +55,6 @@ class HandleUserSignedUp } session([SESSION_COUNTER => -1]); + session([SESSION_DB_SERVER => config('database.default')]); } }