1 | <?php |
||
28 | class UserProvider implements UserProviderInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var UserManagerInterface |
||
32 | */ |
||
33 | protected $userManager; |
||
34 | |||
35 | /** |
||
36 | * Constructor. |
||
37 | */ |
||
38 | public function __construct(UserManagerInterface $userManager) |
||
39 | { |
||
40 | $this->userManager = $userManager; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function loadUserByUsername($username) |
||
47 | { |
||
48 | $user = $this->findUser($username); |
||
49 | |||
50 | if (!$user) { |
||
51 | throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
||
52 | } |
||
53 | |||
54 | return $user; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function refreshUser(SecurityUserInterface $user) |
||
61 | { |
||
62 | if (!$user instanceof UserInterface) { |
||
63 | throw new UnsupportedUserException(sprintf('Expected an instance of SWP\Bundle\UserBundle\Model\UserInterface, but got "%s".', get_class($user))); |
||
64 | } |
||
65 | |||
66 | if (!$this->supportsClass(get_class($user))) { |
||
67 | throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', $this->userManager->getClass(), get_class($user))); |
||
68 | } |
||
69 | |||
70 | if (null === $reloadedUser = $this->userManager->findUserBy(['id' => $user->getId()])) { |
||
71 | throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId())); |
||
72 | } |
||
73 | |||
74 | return $reloadedUser; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function supportsClass($class): bool |
||
81 | { |
||
82 | $userClass = $this->userManager->getClass(); |
||
83 | |||
84 | return $userClass === $class || is_subclass_of($class, $userClass) || User::class === $userClass || $class === BaseUser::class; |
||
|
|||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Finds a user by username. |
||
89 | * |
||
90 | * This method is meant to be an extension point for child classes. |
||
91 | * |
||
92 | * @param string $username |
||
93 | */ |
||
94 | protected function findUser($username): ?UserInterface |
||
98 | } |
||
99 |