Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
EncodePasswordSubscriber | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
9.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
prePersist | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
preUpdate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
encodePassword | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 |
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\Domain\User\Symfony\EventSubscriber\Doctrine; |
25 | |
26 | use App\Domain\User\Model\User; |
27 | use Doctrine\Common\EventSubscriber; |
28 | use Doctrine\ORM\Event\PrePersistEventArgs; |
29 | use Doctrine\ORM\Event\PreUpdateEventArgs; |
30 | use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
31 | |
32 | class EncodePasswordSubscriber implements EventSubscriber |
33 | { |
34 | /** |
35 | * @var UserPasswordHasherInterface |
36 | */ |
37 | private $passwordEncoder; |
38 | |
39 | public function __construct(UserPasswordHasherInterface $passwordEncoder) |
40 | { |
41 | $this->passwordEncoder = $passwordEncoder; |
42 | } |
43 | |
44 | public function getSubscribedEvents(): array |
45 | { |
46 | return [ |
47 | 'prePersist', |
48 | 'preUpdate', |
49 | ]; |
50 | } |
51 | |
52 | /** |
53 | * PrePersist |
54 | * - User : If plainPassword is set, hash it and set password. |
55 | * |
56 | * @throws \Exception |
57 | */ |
58 | public function prePersist(PrePersistEventArgs $args): void |
59 | { |
60 | if (!$args->getObject() instanceof User) { |
61 | return; |
62 | } |
63 | |
64 | $this->encodePassword($args); |
65 | } |
66 | |
67 | /** |
68 | * PreUpdate |
69 | * - User : If plainPassword is set, hash it and set password. |
70 | * |
71 | * @throws \Exception |
72 | */ |
73 | public function preUpdate(PreUpdateEventArgs $args): void |
74 | { |
75 | if (!$args->getObject() instanceof User) { |
76 | return; |
77 | } |
78 | |
79 | $this->encodePassword($args); |
80 | } |
81 | |
82 | /** |
83 | * Encode plainPassword in password field. |
84 | */ |
85 | public function encodePassword(PrePersistEventArgs|PreUpdateEventArgs $args): void |
86 | { |
87 | $model = $args->getObject(); |
88 | |
89 | if (!$model instanceof User) { |
90 | return; |
91 | } |
92 | |
93 | if (\is_null($model->getPlainPassword())) { |
94 | return; |
95 | } |
96 | |
97 | $model->setPassword($this->passwordEncoder->hashPassword($model, $model->getPlainPassword())); |
98 | $model->eraseCredentials(); |
99 | } |
100 | } |