From ad02e34dff3ba2ca5a364fe731bcf9bf38a86014 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 27 Nov 2024 15:38:08 -0500 Subject: [PATCH] Move after login logic to event listener instead of controller (#1257) Co-authored-by: Luke Towers --- modules/backend/ServiceProvider.php | 39 ++++++++++++++++++++++++---- modules/backend/controllers/Auth.php | 16 ------------ modules/backend/models/User.php | 8 +++--- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/modules/backend/ServiceProvider.php b/modules/backend/ServiceProvider.php index c81552de9d..8d352d294d 100644 --- a/modules/backend/ServiceProvider.php +++ b/modules/backend/ServiceProvider.php @@ -1,13 +1,19 @@ registerMailer(); $this->registerAssetBundles(); $this->registerBackendPermissions(); + $this->registerBackendUserEvents(); /* * Backend specific @@ -207,6 +214,28 @@ protected function registerBackendPermissions() }); } + /** + * Register the backend user events + */ + protected function registerBackendUserEvents() + { + Event::listen('backend.user.login', function (\Backend\Models\User $user) { + // @TODO: Deprecate this, and only run migrations when it makes sense + $runMigrationsOnLogin = (bool) Config::get('cms.runMigrationsOnLogin', Config::get('app.debug', false)); + if ($runMigrationsOnLogin) { + try { + // Load version updates + UpdateManager::instance()->update(); + } catch (Exception $e) { + Flash::error($e->getMessage()); + } + } + + // Log the sign in event + AccessLog::add($user); + }); + } + /* * Register widgets */ diff --git a/modules/backend/controllers/Auth.php b/modules/backend/controllers/Auth.php index 82a39dec65..8ed4ecef42 100644 --- a/modules/backend/controllers/Auth.php +++ b/modules/backend/controllers/Auth.php @@ -4,13 +4,11 @@ use Backend; use BackendAuth; use Backend\Classes\Controller; -use Backend\Models\AccessLog; use Config; use Exception; use Flash; use Mail; use Request; -use System\Classes\UpdateManager; use ValidationException; use Validator; use Winter\Storm\Foundation\Http\Middleware\CheckForTrustedHost; @@ -94,20 +92,6 @@ public function signin_onSubmit() 'password' => post('password') ], $remember); - $runMigrationsOnLogin = (bool) Config::get('cms.runMigrationsOnLogin', Config::get('app.debug', false)); - - if ($runMigrationsOnLogin) { - try { - // Load version updates - UpdateManager::instance()->update(); - } catch (Exception $ex) { - Flash::error($ex->getMessage()); - } - } - - // Log the sign in event - AccessLog::add($user); - // Redirect to the intended page after successful sign in return Backend::redirectIntended('backend'); } diff --git a/modules/backend/models/User.php b/modules/backend/models/User.php index 4063c716c2..37c8892a18 100644 --- a/modules/backend/models/User.php +++ b/modules/backend/models/User.php @@ -1,10 +1,10 @@