Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LinkCreatorSubscriber | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
prePersist | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
8 |
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\Application\Traits\Model\CreatorTrait; |
28 | use App\Domain\User\Model\User; |
29 | use Doctrine\Common\EventSubscriber; |
30 | use Doctrine\ORM\Event\LifecycleEventArgs; |
31 | use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; |
32 | |
33 | /** |
34 | * Class LinkCreatorSubscriber |
35 | * Link authenticated user to object. |
36 | */ |
37 | class LinkCreatorSubscriber implements EventSubscriber |
38 | { |
39 | /** |
40 | * @var UserProvider |
41 | */ |
42 | private $userProvider; |
43 | |
44 | /** |
45 | * @var bool |
46 | */ |
47 | private $linkAdmin; |
48 | |
49 | public function __construct( |
50 | UserProvider $userProvider, |
51 | bool $linkAdmin, |
52 | ) { |
53 | $this->userProvider = $userProvider; |
54 | $this->linkAdmin = $linkAdmin; |
55 | } |
56 | |
57 | public function getSubscribedEvents() |
58 | { |
59 | return [ |
60 | 'prePersist', |
61 | ]; |
62 | } |
63 | |
64 | /** |
65 | * PrePersist. |
66 | * |
67 | * Link creator to object. |
68 | * That is to say that every time you will persist an object, |
69 | * the user will be added to related object. |
70 | * |
71 | * @throws \Exception |
72 | */ |
73 | public function prePersist(LifecycleEventArgs $args): void |
74 | { |
75 | $em = $args->getEntityManager(); |
76 | $object = $args->getObject(); |
77 | $uses = \class_uses($object); |
78 | $token = $this->userProvider->getToken(); |
79 | $user = $token ? $token->getUser() : null; |
80 | |
81 | // Only link if prerequisite are right : |
82 | // - Model has CreatorTrait |
83 | // - Connected user is a User Model (i.e. avoid anonymous users) |
84 | if ( |
85 | !\in_array(CreatorTrait::class, $uses) |
86 | || !$user instanceof User |
87 | ) { |
88 | return; |
89 | } |
90 | |
91 | // Skip creator linking if a creator is already set |
92 | if ( |
93 | \in_array(CreatorTrait::class, $uses) |
94 | && null !== $object->getCreator() |
95 | ) { |
96 | return; |
97 | } |
98 | |
99 | // No need to link admin, then link logged user (even if it is an impersonated one) |
100 | if (false === $this->linkAdmin) { |
101 | $object->setCreator($user); |
102 | |
103 | return; |
104 | } |
105 | |
106 | // We link admin, then check it original token |
107 | if ($token instanceof SwitchUserToken) { |
108 | /** @var User $originalUser */ |
109 | $originalUser = $token->getOriginalToken()->getUser(); |
110 | $originalUserId = $originalUser->getId()->toString(); |
111 | $originalUser = $em->find(User::class, $originalUserId); |
112 | $object->setCreator($originalUser); |
113 | |
114 | return; |
115 | } |
116 | |
117 | // Can't link admin, then link standard user |
118 | $object->setCreator($user); |
119 | } |
120 | } |