ldc-lionor/app/Providers/EventServiceProvider.php

84 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2023-02-24 15:20:58 +00:00
<?php
namespace App\Providers;
2023-08-28 08:46:47 +00:00
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
2023-02-24 15:20:58 +00:00
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
2025-03-07 11:29:42 +01:00
use Illuminate\Support\Facades\Event;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
use Slides\Saml2\Events\SignedIn;
use Slides\Saml2\Events\SignedOut;
2023-02-24 15:20:58 +00:00
class EventServiceProvider extends ServiceProvider
{
/**
2023-08-28 08:46:47 +00:00
* The event to listener mappings for the application.
2023-02-24 15:20:58 +00:00
*
* @var array<class-string, array<int, class-string>>
*/
2023-08-28 08:46:47 +00:00
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
2023-02-24 15:20:58 +00:00
/**
2025-03-07 11:29:42 +01:00
* Determine if events and listeners should be automatically discovered.
2023-02-24 15:20:58 +00:00
*
2025-03-07 11:29:42 +01:00
* @return boolean
2023-02-24 15:20:58 +00:00
*/
2025-03-07 11:29:42 +01:00
public function shouldDiscoverEvents(): bool
2023-02-24 15:20:58 +00:00
{
2025-03-07 11:29:42 +01:00
return false;
2023-02-24 15:20:58 +00:00
}
2023-08-28 08:46:47 +00:00
/**
2025-03-07 11:29:42 +01:00
* Register any events for your application.
2023-08-28 08:46:47 +00:00
*
2025-03-07 11:29:42 +01:00
* @return void
2023-08-28 08:46:47 +00:00
*/
2025-03-07 11:29:42 +01:00
public function boot()
2023-08-28 08:46:47 +00:00
{
2025-03-07 11:29:42 +01:00
Event::listen('Slides\Saml2\Events\SignedIn', function (SignedIn $event) {
$messageId = $event->getAuth()->getLastMessageId();
$fromCache = Cache::get(config('cache.prefix') . "_saml_message_id_" . $messageId);
if ($fromCache) {
logger()->error(
'SAML MESSAGE ID HANDLER',
['error' => 'Message ID : ' . $messageId . " replayed !"]
);
abort(403);
} else {
Cache::put(config('cache.prefix') . "_saml_message_id_" . $messageId, $messageId, 28800);
// Cache for 8h.
}
$user = $event->getSaml2User();
$email = $user->getNameId();
$lUser = User::where('email', '=', $email)->first();
if (!$lUser) {
abort(403);
}
Auth::guard('backend')->login($lUser);
session(['sessionIndex' => $user->getSessionIndex()]);
$lUser->last_connection = now();
$lUser->saveQuietly();
});
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
Event::listen('Slides\Saml2\Events\SignedOut', function (SignedOut $event) {
Auth::guard('backend')->logout();
Session::save();
});
2023-08-28 08:46:47 +00:00
}
2023-02-24 15:20:58 +00:00
}