diff --git a/Classes/Service/ImpersonateService.php b/Classes/Service/ImpersonateService.php index cd85cc8..9751dc3 100644 --- a/Classes/Service/ImpersonateService.php +++ b/Classes/Service/ImpersonateService.php @@ -57,49 +57,75 @@ class ImpersonateService public function impersonate($account): void { $currentAccount = $this->securityContext->getAccount(); - $this->session->putData('OriginalIdentity', $this->persistenceManager->getIdentifierByObject($currentAccount)); + $this->writeSession('OriginalIdentity', $this->persistenceManager->getIdentifierByObject($currentAccount)); $this->refreshTokens($account); - $this->session->putData('Impersonate', $this->persistenceManager->getIdentifierByObject($account)); + $this->writeSession('Impersonate', $this->persistenceManager->getIdentifierByObject($account)); } + /** + * @return void + * @throws SessionNotStartedException + */ public function restoreOriginalIdentity(): void { $account = $this->getOriginalIdentity(); $this->refreshTokens($account); - $this->session->putData('Impersonate', null); + $this->writeSession('Impersonate', null); } + /** + * @return Account|null + * @throws SessionNotStartedException + */ public function getImpersonation(): ?Account { - if ($this->session->getData('Impersonate') !== null) { - return $this->persistenceManager->getObjectByIdentifier($this->session->getData('Impersonate'), Account::class); + $impersonation = $this->getSessionData('Impersonate'); + if ($impersonation !== null) { + return $this->persistenceManager->getObjectByIdentifier($impersonation, Account::class); } return null; } + /** + * @return bool + * @throws SessionNotStartedException + */ public function isActive() { 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 { - if ($this->session->getData('OriginalIdentity') !== null) { - return $this->persistenceManager->getObjectByIdentifier($this->session->getData('OriginalIdentity'), Account::class); + $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 { - $roles = $this->getOriginalIdentity()->getRoles(); + $originalAccount = $this->getOriginalIdentity(); + $roles = $originalAccount ? $originalAccount->getRoles() : []; foreach ($roles as $role) { foreach ($this->policyService->getAllParentRoles($role) as $parentRole) { if (!in_array($parentRole, $roles)) { @@ -110,6 +136,10 @@ public function getOriginalIdentityRoles(): array return $roles; } + /** + * @param Account|null $account + * @return void + */ protected function refreshTokens(Account $account = null) { if ($account === null) { @@ -121,4 +151,26 @@ protected function refreshTokens(Account $account = null) $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; + } }