2023-01-08 00:35:43 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\EventSubscriber\UserSystem;
|
|
|
|
|
|
|
|
use App\Entity\UserSystem\User;
|
2023-01-08 20:14:23 +01:00
|
|
|
use App\Services\LogSystem\EventCommentHelper;
|
2023-01-08 00:35:43 +01:00
|
|
|
use App\Services\UserSystem\PermissionSchemaUpdater;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2023-04-15 19:05:45 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
2023-01-08 00:35:43 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
|
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* The purpose of this event subscriber is to check if the permission schema of the current user is up-to-date and upgrade it automatically if needed.
|
2023-01-08 00:35:43 +01:00
|
|
|
*/
|
|
|
|
class UpgradePermissionsSchemaSubscriber implements EventSubscriberInterface
|
|
|
|
{
|
|
|
|
private Security $security;
|
|
|
|
private PermissionSchemaUpdater $permissionSchemaUpdater;
|
|
|
|
private EntityManagerInterface $entityManager;
|
2023-01-08 20:14:23 +01:00
|
|
|
private EventCommentHelper $eventCommentHelper;
|
2023-01-08 00:35:43 +01:00
|
|
|
|
2023-04-15 21:07:04 +02:00
|
|
|
public function __construct(Security $security, PermissionSchemaUpdater $permissionSchemaUpdater, EntityManagerInterface $entityManager, EventCommentHelper $eventCommentHelper)
|
2023-01-08 00:35:43 +01:00
|
|
|
{
|
|
|
|
$this->security = $security;
|
|
|
|
$this->permissionSchemaUpdater = $permissionSchemaUpdater;
|
|
|
|
$this->entityManager = $entityManager;
|
2023-01-08 20:14:23 +01:00
|
|
|
$this->eventCommentHelper = $eventCommentHelper;
|
2023-01-08 00:35:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onRequest(RequestEvent $event): void
|
|
|
|
{
|
|
|
|
if (!$event->isMainRequest()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->security->getUser();
|
|
|
|
if (null === $user) {
|
|
|
|
//Retrieve anonymous user
|
|
|
|
$user = $this->entityManager->getRepository(User::class)->getAnonymousUser();
|
|
|
|
}
|
|
|
|
|
2023-04-15 21:07:04 +02:00
|
|
|
/** @var Session $session */
|
|
|
|
$session = $event->getRequest()->getSession();
|
|
|
|
$flashBag = $session->getFlashBag();
|
|
|
|
|
2023-01-08 00:35:43 +01:00
|
|
|
if ($this->permissionSchemaUpdater->isSchemaUpdateNeeded($user)) {
|
2023-01-08 20:14:23 +01:00
|
|
|
$this->eventCommentHelper->setMessage('Automatic permission schema update');
|
2023-01-08 00:35:43 +01:00
|
|
|
$this->permissionSchemaUpdater->userUpgradeSchemaRecursively($user);
|
|
|
|
$this->entityManager->flush();
|
2023-04-15 21:07:04 +02:00
|
|
|
$flashBag->add('notice', 'user.permissions_schema_updated');
|
2023-01-08 00:35:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubscribedEvents(): array
|
|
|
|
{
|
|
|
|
return [KernelEvents::REQUEST => 'onRequest'];
|
|
|
|
}
|
|
|
|
}
|