ldc-legaulois/app/Providers/EventServiceProvider.php

85 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2020-07-23 10:29:36 +02:00
<?php
namespace App\Providers;
use App\Events\CustomerPasswordReset;
2024-07-12 16:33:26 +02:00
use App\Events\NewNotification;
use App\Listeners\CreateNotification;
2024-07-05 16:34:57 +02:00
use App\Listeners\PasswordHasReset;
2026-09-25 16:25:31 +02:00
use App\Models\User;
2020-07-23 10:29:36 +02:00
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
2026-09-25 16:25:31 +02:00
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Session;
use Slides\Saml2\Events\SignedIn;
2020-07-23 10:29:36 +02:00
class EventServiceProvider extends ServiceProvider
{
/**
2024-01-10 14:49:46 +01:00
* The event to listener mappings for the application.
2020-07-23 10:29:36 +02:00
*
2024-01-10 14:49:46 +01:00
* @var array<class-string, array<int, class-string>>
2020-07-23 10:29:36 +02:00
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
CustomerPasswordReset::class => [
2026-09-25 16:25:31 +02:00
PasswordHasReset::class,
2024-07-05 16:34:57 +02:00
],
2024-07-12 16:33:26 +02:00
NewNotification::class => [
CreateNotification::class,
],
2020-07-23 10:29:36 +02:00
];
2024-01-10 14:49:46 +01:00
/**
* Determine if events and listeners should be automatically discovered.
*/
public function shouldDiscoverEvents(): bool
{
return false;
}
/**
* Register any events for your application.
*/
public function boot(): void
{
Event::listen('Slides\Saml2\Events\SignedIn', function (SignedIn $event) {
$messageId = $event->getAuth()->getLastMessageId();
2026-09-25 16:25:31 +02:00
if (Cache::get(config('cache.prefix') . '_saml_message_id_' . $messageId)) {
logger()->error(
'SAML MESSAGE ID HANDLER',
2026-09-25 16:25:31 +02:00
['error' => 'Message ID : ' . $messageId . ' replayed !']
);
abort(403);
} else {
// Cache for 8h.
2026-09-25 16:25:31 +02:00
Cache::put(config('cache.prefix') . '_saml_message_id_' . $messageId, $messageId, 28800);
}
$user = $event->getSaml2User();
$email = $user->getNameId();
$lUser = User::query()->where('email', $email)->first();
2026-09-25 16:25:31 +02:00
if (! $lUser) {
abort(403);
}
Auth::guard('backend')->login($lUser);
session(['sessionIndex' => $user->getSessionIndex()]);
$lUser->last_connection = now();
$lUser->saveQuietly();
});
Event::listen('Slides\Saml2\Events\SignedOut', function () {
Auth::guard('backend')->logout();
Session::save();
});
}
2020-07-23 10:29:36 +02:00
}