1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OsLabSecurityApiBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) OsLab <https://github.com/OsLab> |
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 OsLab\SecurityApiBundle\Security\User; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
15
|
|
|
use Symfony\Component\Security\Core\User\User; |
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
17
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
18
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class InMemoryApiUserProvider |
22
|
|
|
* |
23
|
|
|
* @author Michael COULLERET <[email protected]> |
24
|
|
|
* @author Florent DESPIERRES <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class InMemoryApiUserProvider implements UserProviderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $users; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor class. |
35
|
|
|
* |
36
|
|
|
* @param array $users The list of given users in security |
37
|
|
|
*/ |
38
|
30 |
|
public function __construct(array $users = []) |
39
|
|
|
{ |
40
|
30 |
|
$this->users = $users; |
41
|
30 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a username for a given api key. |
45
|
|
|
* |
46
|
|
|
* @param string $apiKey |
47
|
|
|
* |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
9 |
|
public function getUsernameByApiKey($apiKey) |
51
|
|
|
{ |
52
|
9 |
|
foreach ($this->users as $user) { |
53
|
6 |
|
if ($apiKey === $user->getPassword()) { |
54
|
5 |
|
return $user->getUsername(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
6 |
|
public function loadUserByUsername($username) |
65
|
|
|
{ |
66
|
6 |
|
$user = $this->getUser($username); |
67
|
|
|
|
68
|
3 |
|
return new User($username, null, $user->getRoles()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Adds a new User to the provider. |
73
|
|
|
* |
74
|
|
|
* @param UserInterface $user A UserInterface instance |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
* @throws \LogicException |
78
|
|
|
*/ |
79
|
6 |
|
public function createUser(UserInterface $user) |
80
|
|
|
{ |
81
|
6 |
|
if (isset($this->users[strtolower($user->getUsername())])) { |
82
|
3 |
|
throw new \LogicException('Another user with the same username already exists.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
$this->users[strtolower($user->getUsername())] = $user; |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
3 |
|
public function refreshUser(UserInterface $user) |
92
|
|
|
{ |
93
|
3 |
|
throw new UnsupportedUserException(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
3 |
|
public function supportsClass($class) |
100
|
|
|
{ |
101
|
3 |
|
return User::class === $class; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the user by given username. |
106
|
|
|
* |
107
|
|
|
* @param string $username The username |
108
|
|
|
* |
109
|
|
|
* @return User |
110
|
|
|
* @throws UsernameNotFoundException If user whose given username does not exist. |
111
|
|
|
*/ |
112
|
6 |
|
private function getUser($username) |
113
|
|
|
{ |
114
|
6 |
|
if (!isset($this->users[strtolower($username)])) { |
115
|
3 |
|
$exception = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
116
|
3 |
|
$exception->setUsername($username); |
117
|
|
|
|
118
|
3 |
|
throw $exception; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $this->users[strtolower($username)]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|