Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.83% |
17 / 24 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
LinkUpdatedBySubscriber | |
70.83% |
17 / 24 |
|
66.67% |
2 / 3 |
11.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
preUpdate | |
63.16% |
12 / 19 |
|
0.00% |
0 / 1 |
9.45 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Application\Symfony\EventSubscriber\Doctrine; |
25 | |
26 | use App\Application\Symfony\Security\UserProvider; |
27 | use App\Domain\User\Model\User; |
28 | use Doctrine\Common\EventSubscriber; |
29 | use Doctrine\ORM\Event\LifecycleEventArgs; |
30 | use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; |
31 | |
32 | /** |
33 | * Class LinkUpdatedBySubscriber |
34 | * Link authenticated user to object. |
35 | */ |
36 | class LinkUpdatedBySubscriber implements EventSubscriber |
37 | { |
38 | /** |
39 | * @var UserProvider |
40 | */ |
41 | private $userProvider; |
42 | |
43 | /** |
44 | * @var bool |
45 | */ |
46 | private $linkAdmin; |
47 | |
48 | public function __construct( |
49 | UserProvider $userProvider, |
50 | bool $linkAdmin, |
51 | ) { |
52 | $this->userProvider = $userProvider; |
53 | $this->linkAdmin = $linkAdmin; |
54 | } |
55 | |
56 | public function getSubscribedEvents() |
57 | { |
58 | return [ |
59 | 'preUpdate', |
60 | ]; |
61 | } |
62 | |
63 | /** |
64 | * PrePersist. |
65 | * |
66 | * Link creator to object. |
67 | * That is to say that every time you will persist an object, |
68 | * the user will be added to related object. |
69 | * |
70 | * @throws \Exception |
71 | */ |
72 | public function preUpdate(LifecycleEventArgs $args): void |
73 | { |
74 | $em = $args->getEntityManager(); |
75 | $object = $args->getObject(); |
76 | $methods = \get_class_methods($object); |
77 | |
78 | $token = $this->userProvider->getToken(); |
79 | $user = $token ? $token->getUser() : null; |
80 | |
81 | // Only link if prerequisite are right : |
82 | // - Model has required methods |
83 | // - Connected user is a User Model (i.e. avoid anonymous users) |
84 | if ( |
85 | !\in_array('setUpdatedBy', $methods) |
86 | || !\in_array('getUpdatedBy', $methods) |
87 | || !$user instanceof User |
88 | ) { |
89 | return; |
90 | } |
91 | |
92 | // No need to link admin, then link logged user (even if it is an impersonated one) |
93 | if (!$this->linkAdmin) { |
94 | $object->setUpdatedBy($user->getFullName()); |
95 | |
96 | return; |
97 | } |
98 | |
99 | // We link admin, then check it original token |
100 | if ($token instanceof SwitchUserToken) { |
101 | /** @var User $originalUser */ |
102 | $originalUser = $token->getOriginalToken()->getUser(); |
103 | $originalUserId = $originalUser->getId()->toString(); |
104 | $originalUser = $em->find(User::class, $originalUserId); |
105 | $object->setUpdatedBy($originalUser->getFullName()); |
106 | |
107 | return; |
108 | } |
109 | |
110 | // Can't link admin, then link standard user |
111 | $object->setUpdatedBy($user->getFullName()); |
112 | } |
113 | } |