|
1
|
|
|
<?php |
|
2
|
|
|
namespace AppBundle\Security; |
|
3
|
|
|
|
|
4
|
|
|
use AppBundle\Entity\User; |
|
5
|
|
|
use AppBundle\Service\UserService; |
|
6
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
7
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
class FormUserProvider implements UserProviderInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var UserService */ |
|
14
|
|
|
private $userService; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(UserService $userService) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->userService = $userService; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $username Compound username [type:username] |
|
25
|
|
|
* |
|
26
|
|
|
* @return null|User |
|
27
|
|
|
*/ |
|
28
|
|
|
public function loadUserByUsername($username) |
|
29
|
|
|
{ |
|
30
|
|
|
list($type, $username) = explode(':', $username, 2); |
|
31
|
|
|
|
|
32
|
|
|
$user = null; |
|
|
|
|
|
|
33
|
|
|
switch ($type) { |
|
34
|
|
|
case UserCredentials::TYPE_ID: |
|
35
|
|
|
$user = $this->userService->getUserById($username); |
|
36
|
|
|
break; |
|
37
|
|
|
case UserCredentials::TYPE_NAME: |
|
38
|
|
|
$user = $this->userService->getUserByLogin($username); |
|
39
|
|
|
break; |
|
40
|
|
|
default: |
|
41
|
|
|
throw new \InvalidArgumentException('Invalid user credentials type'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($user === null) { |
|
45
|
|
|
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $user; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
* |
|
54
|
|
|
* @param UserInterface $user |
|
55
|
|
|
* |
|
56
|
|
|
* @throws UnsupportedUserException if the account is not supported |
|
57
|
|
|
* |
|
58
|
|
|
* @return UserInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function refreshUser(UserInterface $user) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$this->supportsClass(get_class($user))) { |
|
63
|
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $user; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Whether this provider supports the given user class. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $class |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function supportsClass($class) |
|
77
|
|
|
{ |
|
78
|
|
|
return $class === User::class; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.