getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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)) {
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type Symfony\Component\Security\Core\User\UserInterface and null; however, parameter $holder of App\Services\UserSystem\...:isSchemaUpdateNeeded() does only seem to accept App\Security\Interfaces\HasPermissionsInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        if ($this->permissionSchemaUpdater->isSchemaUpdateNeeded(/** @scrutinizer ignore-type */ $user)) {
Loading history...
66
            $this->eventCommentHelper->setMessage('Automatic permission schema update');
67
            $this->permissionSchemaUpdater->userUpgradeSchemaRecursively($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Services\UserSystem\...radeSchemaRecursively() does only seem to accept App\Entity\UserSystem\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $this->permissionSchemaUpdater->userUpgradeSchemaRecursively(/** @scrutinizer ignore-type */ $user);
Loading history...
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
}