1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher User Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2021 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @Copyright 2021 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\UserBundle\Security; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\CoreBundle\Model\User; |
20
|
|
|
use SWP\Bundle\UserBundle\Model\User as BaseUser; |
21
|
|
|
use SWP\Bundle\UserBundle\Model\UserInterface; |
22
|
|
|
use SWP\Bundle\UserBundle\Model\UserManagerInterface; |
23
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
24
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
25
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface; |
26
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
27
|
|
|
|
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 |
95
|
|
|
{ |
96
|
|
|
return $this->userManager->findUserByUsername($username); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|