1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace App\EventSubscriber\UserSystem; |
22
|
|
|
|
23
|
|
|
use App\Entity\UserSystem\User; |
24
|
|
|
use App\Services\LogSystem\EventCommentHelper; |
25
|
|
|
use App\Services\UserSystem\PermissionSchemaUpdater; |
26
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
27
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
28
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
29
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
30
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
31
|
|
|
use Symfony\Component\Security\Core\Security; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 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. |
35
|
|
|
*/ |
36
|
|
|
class UpgradePermissionsSchemaSubscriber implements EventSubscriberInterface |
37
|
|
|
{ |
38
|
|
|
private Security $security; |
39
|
|
|
private PermissionSchemaUpdater $permissionSchemaUpdater; |
40
|
|
|
private EntityManagerInterface $entityManager; |
41
|
|
|
private FlashBagInterface $flashBag; |
42
|
|
|
private EventCommentHelper $eventCommentHelper; |
43
|
|
|
|
44
|
|
|
public function __construct(Security $security, PermissionSchemaUpdater $permissionSchemaUpdater, EntityManagerInterface $entityManager, FlashBagInterface $flashBag, EventCommentHelper $eventCommentHelper) |
45
|
|
|
{ |
46
|
|
|
$this->security = $security; |
47
|
|
|
$this->permissionSchemaUpdater = $permissionSchemaUpdater; |
48
|
|
|
$this->entityManager = $entityManager; |
49
|
|
|
$this->flashBag = $flashBag; |
50
|
|
|
$this->eventCommentHelper = $eventCommentHelper; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onRequest(RequestEvent $event): void |
54
|
|
|
{ |
55
|
|
|
if (!$event->isMainRequest()) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$user = $this->security->getUser(); |
60
|
|
|
if (null === $user) { |
61
|
|
|
//Retrieve anonymous user |
62
|
|
|
$user = $this->entityManager->getRepository(User::class)->getAnonymousUser(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->permissionSchemaUpdater->isSchemaUpdateNeeded($user)) { |
|
|
|
|
66
|
|
|
$this->eventCommentHelper->setMessage('Automatic permission schema update'); |
67
|
|
|
$this->permissionSchemaUpdater->userUpgradeSchemaRecursively($user); |
|
|
|
|
68
|
|
|
$this->entityManager->flush(); |
69
|
|
|
$this->flashBag->add('notice', 'user.permissions_schema_updated'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function getSubscribedEvents(): array |
74
|
|
|
{ |
75
|
|
|
return [KernelEvents::REQUEST => 'onRequest']; |
76
|
|
|
} |
77
|
|
|
} |