Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
UserProvider | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAuthenticatedUser | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
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\Security; |
25 | |
26 | use App\Domain\User\Model; |
27 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
28 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
29 | use Symfony\Component\Security\Core\User\UserInterface; |
30 | |
31 | class UserProvider |
32 | { |
33 | /** |
34 | * @var TokenStorageInterface |
35 | */ |
36 | private $tokenStorage; |
37 | |
38 | public function __construct(TokenStorageInterface $tokenStorage) |
39 | { |
40 | $this->tokenStorage = $tokenStorage; |
41 | } |
42 | |
43 | /** |
44 | * Get token. |
45 | */ |
46 | public function getToken(): ?TokenInterface |
47 | { |
48 | return $this->tokenStorage->getToken(); |
49 | } |
50 | |
51 | /** |
52 | * Get the authenticated user |
53 | * - User isn't under protected route: return null |
54 | * - User is anonymous: return null |
55 | * - User is connected: return UserInterface instance. |
56 | */ |
57 | public function getAuthenticatedUser(): ?Model\User |
58 | { |
59 | $token = $this->tokenStorage->getToken(); |
60 | if (null === $token) { |
61 | return null; |
62 | } |
63 | |
64 | /** @var Model\User|string $user */ |
65 | $user = $token->getUser(); |
66 | if (!\is_object($user)) { |
67 | // e.g. anonymous authentication |
68 | return null; |
69 | } |
70 | |
71 | return $user; |
72 | } |
73 | } |