Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
72.73% |
16 / 22 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ApiRoleSubscriber | |
72.73% |
16 / 22 |
|
50.00% |
2 / 4 |
10.64 | |
0.00% |
0 / 1 |
getSubscribedEvents | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
prePersist | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setRoleApiAuthorized | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
5.20 | |||
preUpdate | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 |
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\Domain\User\Model\User; |
27 | use Doctrine\Common\EventSubscriber; |
28 | use Doctrine\ORM\Event\LifecycleEventArgs; |
29 | use Doctrine\ORM\Event\PreUpdateEventArgs; |
30 | |
31 | class ApiRoleSubscriber implements EventSubscriber |
32 | { |
33 | public function getSubscribedEvents() |
34 | { |
35 | return [ |
36 | 'prePersist', |
37 | 'preUpdate', |
38 | ]; |
39 | } |
40 | |
41 | /** |
42 | * PrePersist |
43 | * Add API_ROLE to User. |
44 | * |
45 | * @throws \Exception |
46 | */ |
47 | public function prePersist(LifecycleEventArgs $args): void |
48 | { |
49 | $user = $args->getObject(); |
50 | |
51 | if (!$user instanceof User) { |
52 | return; |
53 | } |
54 | $this->setRoleApiAuthorized($user); |
55 | } |
56 | |
57 | public function setRoleApiAuthorized(User $user): void |
58 | { |
59 | $isApiAuthorized = $user->getApiAuthorized(); |
60 | |
61 | if (null == $isApiAuthorized) { |
62 | return; |
63 | } |
64 | |
65 | $roles = $user->getRoles(); |
66 | |
67 | if ($isApiAuthorized) { |
68 | $roles[] = 'ROLE_API'; |
69 | } else { |
70 | $roles = array_diff($roles, ['API_ROLE']); |
71 | } |
72 | |
73 | $user->setRoles($roles); |
74 | } |
75 | |
76 | /** |
77 | * PrePersist |
78 | * Add API_ROLE to User. |
79 | * |
80 | * @throws \Exception |
81 | */ |
82 | public function preUpdate(PreUpdateEventArgs $args): void |
83 | { |
84 | $user = $args->getObject(); |
85 | |
86 | if (!$user instanceof User) { |
87 | return; |
88 | } |
89 | |
90 | if (!$args->hasChangedField('apiAuthorized')) { |
91 | return; |
92 | } |
93 | |
94 | $this->setRoleApiAuthorized($user); |
95 | } |
96 | } |