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