1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\AuditorBundle\User; |
4
|
|
|
|
5
|
|
|
use DH\Auditor\Provider\Doctrine\Configuration; |
6
|
|
|
use DH\Auditor\User\User; |
7
|
|
|
use DH\Auditor\User\UserInterface as AuditorUserInterface; |
8
|
|
|
use DH\Auditor\User\UserProviderInterface; |
9
|
|
|
use Exception; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; |
11
|
|
|
use Symfony\Component\Security\Core\Role\SwitchUserRole; |
|
|
|
|
12
|
|
|
use Symfony\Component\Security\Core\Security; |
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
14
|
|
|
|
15
|
|
|
class UserProvider implements UserProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Security |
19
|
|
|
*/ |
20
|
|
|
private $security; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Configuration |
24
|
|
|
*/ |
25
|
|
|
private $configuration; |
26
|
|
|
|
27
|
|
|
public function __construct(Security $security, Configuration $configuration) |
28
|
|
|
{ |
29
|
|
|
$this->security = $security; |
30
|
|
|
$this->configuration = $configuration; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __invoke(): ?AuditorUserInterface |
34
|
|
|
{ |
35
|
|
|
$tokenUser = $this->getTokenUser(); |
36
|
|
|
$impersonatorUser = $this->getImpersonatorUser(); |
37
|
|
|
|
38
|
|
|
$identifier = null; |
39
|
|
|
$username = null; |
40
|
|
|
|
41
|
|
|
if (null !== $tokenUser && $tokenUser instanceof UserInterface) { |
42
|
|
|
if (method_exists($tokenUser, 'getId')) { |
43
|
|
|
$identifier = $tokenUser->getId(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$username = $tokenUser->getUsername(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (null !== $impersonatorUser && $impersonatorUser instanceof UserInterface) { |
50
|
|
|
$username .= sprintf('[impersonator %s]', $impersonatorUser->getUsername()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (null === $identifier && null === $username) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new User($identifier, $username); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return null|string|UserInterface |
62
|
|
|
*/ |
63
|
|
|
private function getTokenUser() |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$token = $this->security->getToken(); |
67
|
|
|
} catch (Exception $e) { |
68
|
|
|
$token = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (null === $token) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$tokenUser = $token->getUser(); |
76
|
|
|
if ($tokenUser instanceof UserInterface) { |
77
|
|
|
return $tokenUser; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return null|string|UserInterface |
85
|
|
|
*/ |
86
|
|
|
private function getImpersonatorUser() |
87
|
|
|
{ |
88
|
|
|
$token = $this->security->getToken(); |
89
|
|
|
|
90
|
|
|
// Symfony >= 5 |
91
|
|
|
if (class_exists(SwitchUserToken::class) && $token instanceof SwitchUserToken) { |
92
|
|
|
return $token->getOriginalToken()->getUser(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Symfony < 5 |
96
|
|
|
$roles = []; |
97
|
|
|
if (null !== $token) { |
98
|
|
|
$roles = method_exists($token, 'getRoleNames') ? $token->getRoleNames() : $token->getRoles(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($roles as $role) { |
102
|
|
|
if ($role instanceof SwitchUserRole) { |
103
|
|
|
return $role->getSource()->getUser(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths