/
ImpersonateService.php
175 lines (157 loc) · 4.49 KB
/
ImpersonateService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Neos\Neos\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\Security\Account;
use Neos\Flow\Security\Context;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Flow\Session\Exception\SessionNotStartedException;
use Neos\Flow\Session\SessionInterface;
/**
* Impersonate Service
*/
class ImpersonateService
{
/**
* @var Context
* @Flow\Inject
*/
protected $securityContext;
/**
* @var SessionInterface
* @Flow\Inject
*/
protected $session;
/**
* @var PersistenceManagerInterface
* @Flow\Inject
*/
protected $persistenceManager;
/**
* @var PolicyService
* @Flow\Inject
*/
protected $policyService;
/**
* @param Account $account
* @return void
* @throws SessionNotStartedException
*/
public function impersonate(Account $account): void
{
$currentAccount = $this->securityContext->getAccount();
$this->writeSession('OriginalIdentity', $this->persistenceManager->getIdentifierByObject($currentAccount));
$this->refreshTokens($account);
$this->writeSession('Impersonate', $this->persistenceManager->getIdentifierByObject($account));
}
/**
* @return void
* @throws SessionNotStartedException
*/
public function restoreOriginalIdentity(): void
{
$account = $this->getOriginalIdentity();
$this->refreshTokens($account);
$this->writeSession('Impersonate', null);
}
/**
* @return Account|null
* @throws SessionNotStartedException
*/
public function getImpersonation(): ?Account
{
$impersonation = $this->getSessionData('Impersonate');
if ($impersonation !== null) {
return $this->persistenceManager->getObjectByIdentifier($impersonation, Account::class);
}
return null;
}
/**
* @return bool
* @throws SessionNotStartedException
*/
public function isActive(): bool
{
return $this->getImpersonation() instanceof Account;
}
/**
* @return Account|null
*/
public function getCurrentUser(): ?Account
{
return $this->securityContext->getAccount();
}
/**
* @return Account|null
* @throws SessionNotStartedException
*/
public function getOriginalIdentity(): ?Account
{
$originalIdentity = $this->getSessionData('OriginalIdentity');
if ($originalIdentity !== null) {
return $this->persistenceManager->getObjectByIdentifier($originalIdentity, Account::class);
}
return $this->securityContext->getAccount();
}
/**
* @return array
* @throws SessionNotStartedException
*/
public function getOriginalIdentityRoles(): array
{
$originalAccount = $this->getOriginalIdentity();
$roles = $originalAccount ? $originalAccount->getRoles() : [];
foreach ($roles as $role) {
foreach ($this->policyService->getAllParentRoles($role) as $parentRole) {
if (!in_array($parentRole, $roles, true)) {
$roles[$parentRole->getIdentifier()] = $parentRole;
}
}
}
return $roles;
}
/**
* @param Account|null $account
* @return void
*/
protected function refreshTokens(Account $account = null): void
{
if ($account === null) {
return;
}
$tokens = $this->securityContext->getAuthenticationTokens();
foreach ($tokens as $token) {
$token->setAccount($account);
}
}
/**
* @param string $key
* @param string|null $value
* @return void
* @throws SessionNotStartedException
*/
protected function writeSession(string $key, ?string $value): void
{
if ($this->session->isStarted()) {
$this->session->putData($key, $value);
}
}
/**
* @param string $key
* @throws SessionNotStartedException
*/
protected function getSessionData(string $key): mixed
{
return $this->session->isStarted() && $this->session->hasKey($key) ? $this->session->getData($key) : null;
}
}