1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/silex-user package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AWurth\Silex\User\Provider; |
13
|
|
|
|
14
|
|
|
use AWurth\Silex\User\Model\UserInterface; |
15
|
|
|
use AWurth\Silex\User\Model\UserManagerInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
17
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
18
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface; |
19
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
20
|
|
|
|
21
|
|
|
class UserProvider implements UserProviderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var UserManagerInterface |
25
|
|
|
*/ |
26
|
|
|
protected $userManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param UserManagerInterface $userManager |
32
|
|
|
*/ |
33
|
|
|
public function __construct(UserManagerInterface $userManager) |
34
|
|
|
{ |
35
|
|
|
$this->userManager = $userManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function loadUserByUsername($username) |
42
|
|
|
{ |
43
|
|
|
$user = $this->findUser($username); |
44
|
|
|
|
45
|
|
|
if (null === $user) { |
46
|
|
|
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $user; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function refreshUser(SecurityUserInterface $user) |
56
|
|
|
{ |
57
|
|
|
$class = get_class($user); |
58
|
|
|
|
59
|
|
|
if (!$user instanceof UserInterface) { |
60
|
|
|
throw new UnsupportedUserException(sprintf('Expected an instance of AWurth\Silex\User\Entity\UserInterface, but got "%s".', $class)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$this->supportsClass($class)) { |
64
|
|
|
throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', $this->userManager->getClass(), $class)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function supportsClass($class) |
74
|
|
|
{ |
75
|
|
|
$userClass = $this->userManager->getClass(); |
76
|
|
|
|
77
|
|
|
return $userClass === $class || is_subclass_of($class, $userClass); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Finds a user by username. |
82
|
|
|
* |
83
|
|
|
* @param string $username |
84
|
|
|
* |
85
|
|
|
* @return UserInterface|null |
86
|
|
|
*/ |
87
|
|
|
protected function findUser($username) |
88
|
|
|
{ |
89
|
|
|
return $this->userManager->findUserByUsername($username); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|