Skip to content

Commit

Permalink
Move after login logic to event listener instead of controller (winte…
Browse files Browse the repository at this point in the history
…rcms#1257)

Co-authored-by: Luke Towers <[email protected]>
  • Loading branch information
mjauvin and LukeTowers authored Nov 27, 2024
1 parent 6df0e59 commit ad02e34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
39 changes: 34 additions & 5 deletions modules/backend/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php namespace Backend;

use Backend;
use BackendMenu;
use BackendAuth;
use Backend\Models\UserRole;
use Backend\Classes\WidgetManager;
use System\Classes\MailManager;
use Backend\Facades\Backend;
use Backend\Facades\BackendAuth;
use Backend\Facades\BackendMenu;
use Backend\Models\AccessLog;
use Backend\Models\UserRole;
use Exception;
use Illuminate\Support\Facades\Event;
use System\Classes\CombineAssets;
use System\Classes\MailManager;
use System\Classes\SettingsManager;
use System\Classes\UpdateManager;
use Winter\Storm\Support\Facades\Config;
use Winter\Storm\Support\Facades\Flash;
use Winter\Storm\Support\ModuleServiceProvider;

class ServiceProvider extends ModuleServiceProvider
Expand All @@ -25,6 +31,7 @@ public function register()
$this->registerMailer();
$this->registerAssetBundles();
$this->registerBackendPermissions();
$this->registerBackendUserEvents();

/*
* Backend specific
Expand Down Expand Up @@ -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
*/
Expand Down
16 changes: 0 additions & 16 deletions modules/backend/controllers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
8 changes: 4 additions & 4 deletions modules/backend/models/User.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php namespace Backend\Models;

use Mail;
use Event;
use Backend;
use BackendAuth;
use Backend\Facades\Backend;
use Backend\Facades\BackendAuth;
use Illuminate\Support\Facades\Event;
use Winter\Storm\Auth\Models\User as UserBase;
use Winter\Storm\Support\Facades\Mail;

/**
* Administrator user model
Expand Down

0 comments on commit ad02e34

Please sign in to comment.