|
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\DependencyInjection\ContainerAwareInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
16
|
|
|
use Symfony\Component\Security\Core\Role\SwitchUserRole; |
|
17
|
|
|
use Xiidea\EasyAuditBundle\Traits\ServiceContainerGetterMethods; |
|
18
|
|
|
|
|
19
|
|
|
class UserAwareComponent implements ContainerAwareInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use ServiceContainerGetterMethods; |
|
22
|
|
|
use ContainerAwareTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getContainer() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->container; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get a user from the Security Context |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
* @throws \LogicException If SecurityBundle is not available |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getUser() |
|
39
|
|
|
{ |
|
40
|
|
|
if (!$this->getContainer()->has('security.token_storage')) { |
|
41
|
|
|
throw new \LogicException('The SecurityBundle is not registered in your application.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (null === $token = $this->getContainer()->get('security.token_storage')->getToken()) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!is_object($user = $token->getUser())) { |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $user; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
final protected function getImpersonatingUser() |
|
59
|
|
|
{ |
|
60
|
|
|
if (null === $token = $this->getContainer()->get('security.token_storage')->getToken()) { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($this->getContainer()->get('security.authorization_checker')->isGranted('ROLE_PREVIOUS_ADMIN')) { |
|
65
|
|
|
return $this->getImpersonatingUserFromRole($token); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getUsername() |
|
75
|
|
|
{ |
|
76
|
|
|
$user = $this->getUser(); |
|
77
|
|
|
|
|
78
|
|
|
if(empty($user)) { |
|
79
|
|
|
return $this->getAnonymousUserName(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $user->getUsername(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getAnonymousUserName() |
|
89
|
|
|
{ |
|
90
|
|
|
$request = $this->getRequest(); |
|
91
|
|
|
|
|
92
|
|
|
if ($request && $request->getClientIp()) { |
|
93
|
|
|
return "Anonymous"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return "By Command"; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $token |
|
101
|
|
|
* @param null $user |
|
102
|
|
|
* @return mixed |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function getImpersonatingUserFromRole($token, $user = null) |
|
105
|
|
|
{ |
|
106
|
|
|
foreach ($token->getRoles() as $role) { |
|
107
|
|
|
if ($role instanceof SwitchUserRole) { |
|
108
|
|
|
$user = $role->getSource()->getUser(); |
|
109
|
|
|
break; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $user; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|