1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle\User; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; |
6
|
|
|
use Symfony\Component\Security\Core\Role\SwitchUserRole; |
7
|
|
|
use Symfony\Component\Security\Core\Security; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface; |
9
|
|
|
|
10
|
|
|
class TokenStorageUserProvider implements UserProviderInterface |
11
|
|
|
{ |
12
|
|
|
private $security; |
13
|
|
|
|
14
|
|
|
public function __construct(Security $security) |
15
|
|
|
{ |
16
|
|
|
$this->security = $security; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getUser(): ?UserInterface |
20
|
|
|
{ |
21
|
|
|
$user = null; |
22
|
|
|
|
23
|
|
|
try { |
24
|
|
|
$token = $this->security->getToken(); |
25
|
|
|
} catch (\Exception $e) { |
26
|
|
|
$token = null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (null === $token) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$tokenUser = $token->getUser(); |
34
|
|
|
if ($tokenUser instanceof BaseUserInterface) { |
35
|
|
|
$impersonation = ''; |
36
|
|
|
if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) { |
37
|
|
|
$impersonatorUser = null; |
38
|
|
|
// Symfony > 4.3 |
39
|
|
|
if ($token instanceof SwitchUserToken) { |
40
|
|
|
$impersonatorUser = $token->getOriginalToken()->getUser(); |
41
|
|
|
} else { |
42
|
|
|
foreach ($this->security->getToken()->getRoles() as $role) { |
|
|
|
|
43
|
|
|
if ($role instanceof SwitchUserRole) { |
44
|
|
|
$impersonatorUser = $role->getSource()->getUser(); |
45
|
|
|
|
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (\is_object($impersonatorUser)) { |
52
|
|
|
$id = method_exists($impersonatorUser, 'getId') ? $impersonatorUser->getId() : null; |
53
|
|
|
$username = method_exists($impersonatorUser, 'getUsername') ? $impersonatorUser->getUsername() : (string) $impersonatorUser; |
54
|
|
|
$impersonation = ' [impersonator '.$username.':'.$id.']'; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
$id = method_exists($tokenUser, 'getId') ? $tokenUser->getId() : null; |
58
|
|
|
$user = new User($id, $tokenUser->getUsername().$impersonation); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $user; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.