1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XiideaEasyAuditBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Xiidea <http://www.xiidea.net> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xiidea\EasyAuditBundle\Common; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
16
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
17
|
|
|
use Symfony\Component\Security\Core\Role\SwitchUserRole; |
18
|
|
|
use \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
19
|
|
|
|
20
|
|
|
class UserAwareComponent |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var TokenStorageInterface |
24
|
|
|
*/ |
25
|
|
|
private $tokenStorage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AuthorizationCheckerInterface |
29
|
|
|
*/ |
30
|
|
|
private $authChecker; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RequestStack |
34
|
|
|
*/ |
35
|
|
|
private $requestStack; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TokenStorageInterface $tokenStorage |
39
|
|
|
*/ |
40
|
|
|
public function setTokenStorage($tokenStorage) |
41
|
|
|
{ |
42
|
|
|
$this->tokenStorage = $tokenStorage; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get a user from the Security Context |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
* @throws \LogicException If SecurityBundle is not available |
51
|
|
|
*/ |
52
|
|
|
public function getUser() |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
if (null === $token = $this->tokenStorage->getToken()) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!is_object($user = $token->getUser())) { |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $user; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param AuthorizationCheckerInterface $authChecker |
68
|
|
|
*/ |
69
|
|
|
public function setAuthChecker($authChecker) |
70
|
|
|
{ |
71
|
|
|
$this->authChecker = $authChecker; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param RequestStack $requestStack |
76
|
|
|
*/ |
77
|
|
|
public function setRequestStack($requestStack) |
78
|
|
|
{ |
79
|
|
|
$this->requestStack = $requestStack; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
final protected function getImpersonatingUser() |
86
|
|
|
{ |
87
|
|
|
if (null === $token = $this->tokenStorage->getToken()) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->authChecker->isGranted('ROLE_PREVIOUS_ADMIN')) { |
92
|
|
|
return $this->getImpersonatingUserFromRole($token); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getUsername() |
102
|
|
|
{ |
103
|
|
|
$user = $this->getUser(); |
104
|
|
|
|
105
|
|
|
if(empty($user)) { |
106
|
|
|
return $this->getAnonymousUserName(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $user->getUsername(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getAnonymousUserName() |
116
|
|
|
{ |
117
|
|
|
$request = $this->getRequest(); |
118
|
|
|
|
119
|
|
|
if ($request && $request->getClientIp()) { |
|
|
|
|
120
|
|
|
return "Anonymous"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return "By Command"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param TokenInterface $token |
128
|
|
|
* @param null $user |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
protected function getImpersonatingUserFromRole($token, $user = null) |
132
|
|
|
{ |
133
|
|
|
foreach ($token->getRoles() as $role) { |
134
|
|
|
if ($role instanceof SwitchUserRole) { |
135
|
|
|
$user = $role->getSource()->getUser(); |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $user; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function getRequest() |
144
|
|
|
{ |
145
|
|
|
if($this->requestStack === null) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->requestStack->getCurrentRequest(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: