|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Security; |
|
5
|
|
|
|
|
6
|
|
|
use App\Repository\ApiTokenRepository; |
|
7
|
|
|
use App\Repository\UserRepository; |
|
8
|
|
|
use App\Entity\User; |
|
9
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
10
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
11
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
13
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
class UserProvider implements UserProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var UserRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
private $userRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ApiTokenRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
private $apiTokenRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TranslatorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $translator; |
|
31
|
|
|
|
|
32
|
10 |
|
public function __construct( |
|
33
|
|
|
UserRepository $userRepository, |
|
34
|
|
|
ApiTokenRepository $apiTokenRepository, |
|
35
|
|
|
TranslatorInterface $translator |
|
36
|
|
|
) |
|
37
|
|
|
{ |
|
38
|
10 |
|
$this->userRepository = $userRepository; |
|
39
|
10 |
|
$this->apiTokenRepository = $apiTokenRepository; |
|
40
|
10 |
|
$this->translator = $translator; |
|
41
|
10 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function loadUserByUsername($username): User |
|
47
|
|
|
{ |
|
48
|
2 |
|
$user = $this->userRepository->loadUserByUsername($username); |
|
49
|
|
|
|
|
50
|
2 |
|
if (!$user) { |
|
51
|
1 |
|
throw new UsernameNotFoundException($this->translator->trans('not_found_by_username', [ |
|
52
|
1 |
|
'username' => $username, |
|
53
|
1 |
|
], 'users')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $user; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $token |
|
61
|
|
|
* @return User |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function loadUserByToken(string $token): User |
|
64
|
|
|
{ |
|
65
|
1 |
|
$apiToken = $this->apiTokenRepository->findByToken($token); |
|
66
|
|
|
|
|
67
|
1 |
|
if (!$apiToken) { |
|
68
|
|
|
throw new UsernameNotFoundException($this->translator->trans('not_found_by_api_token', [ |
|
69
|
|
|
'token' => $token, |
|
70
|
|
|
], 'users')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $apiToken->getUser(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function refreshUser(UserInterface $user): ?User |
|
80
|
|
|
{ |
|
81
|
1 |
|
if (!$this->supportsClass(get_class($user))) { |
|
82
|
1 |
|
throw new UnsupportedUserException($this->translator->trans('unexpected_class', [ |
|
83
|
1 |
|
'expected_class' => User::class, |
|
84
|
1 |
|
'actual_class' => get_class($user), |
|
85
|
1 |
|
], 'exceptions')); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var $user User |
|
90
|
|
|
*/ |
|
91
|
|
|
if (null === $reloadedUser = $this->userRepository->find($user->getId())) { |
|
92
|
|
|
throw new UsernameNotFoundException($this->translator->trans('user_provider.reload.error', [ |
|
93
|
|
|
'user_id' => $user->getId(), |
|
94
|
|
|
], 'exceptions')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $reloadedUser; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function supportsClass($class) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$userClass = User::class; |
|
106
|
1 |
|
return $userClass === $class || is_subclass_of($class, $userClass); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |