diff --git a/config/event_subscribers.yaml b/config/event_subscribers.yaml index cbfc3dcd0..01afb6065 100644 --- a/config/event_subscribers.yaml +++ b/config/event_subscribers.yaml @@ -13,6 +13,10 @@ services: tags: [ 'kernel.event_subscriber' ] arguments: ['%pimcore_studio_backend.url_prefix%'] + Pimcore\Bundle\StudioBackendBundle\EventSubscriber\SessionCloseSubscriber: + tags: [ 'kernel.event_subscriber' ] + arguments: ['%pimcore_studio_backend.url_prefix%'] + Pimcore\Bundle\StudioBackendBundle\EventSubscriber\ApiExceptionSubscriber: tags: [ 'kernel.event_subscriber' ] arguments: ["%kernel.environment%", '%pimcore_studio_backend.url_prefix%'] \ No newline at end of file diff --git a/src/EventSubscriber/SessionCloseSubscriber.php b/src/EventSubscriber/SessionCloseSubscriber.php new file mode 100644 index 000000000..05a558467 --- /dev/null +++ b/src/EventSubscriber/SessionCloseSubscriber.php @@ -0,0 +1,70 @@ + 'onLoginSuccess', + KernelEvents::REQUEST => 'onKernelRequest', + ]; + } + + public function onLoginSuccess(LoginSuccessEvent $event): void + { + $request = $event->getRequest(); + if (!$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix)) { + return; + } + + $this->closeSessionWrite($request->getSession()); + } + + public function onKernelRequest(RequestEvent $event): void + { + $request = $event->getRequest(); + if (!$event->isMainRequest() || !$this->isStudioBackendPath($request->getPathInfo(), $this->urlPrefix)) { + return; + } + + $this->closeSessionWrite($request->getSession()); + } + + private function closeSessionWrite(SessionInterface $session): void + { + if ($session->isStarted()) { + $session->save(); + } + } +} diff --git a/src/Security/Authenticator/AdminTokenAuthenticator.php b/src/Security/Authenticator/AdminTokenAuthenticator.php index a0f38042b..355d64920 100644 --- a/src/Security/Authenticator/AdminTokenAuthenticator.php +++ b/src/Security/Authenticator/AdminTokenAuthenticator.php @@ -90,10 +90,6 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token, ); } - if (session_status() === PHP_SESSION_ACTIVE) { - session_write_close(); - } - return null; } diff --git a/src/Security/Service/SecurityService.php b/src/Security/Service/SecurityService.php index 1f769117e..dbbdc7c32 100644 --- a/src/Security/Service/SecurityService.php +++ b/src/Security/Service/SecurityService.php @@ -109,9 +109,4 @@ public function getSpecialDataObjectPermissions( $permission ); } - - public function isSessionWritable(): bool - { - return session_status() === PHP_SESSION_ACTIVE; - } } diff --git a/src/Security/Service/SecurityServiceInterface.php b/src/Security/Service/SecurityServiceInterface.php index c72f6e4f8..5b18055a2 100644 --- a/src/Security/Service/SecurityServiceInterface.php +++ b/src/Security/Service/SecurityServiceInterface.php @@ -56,6 +56,4 @@ public function getSpecialDataObjectPermissions( UserInterface $user, string $permission ): array; - - public function isSessionWritable(): bool; } diff --git a/src/Security/Voter/AuthorizationVoter.php b/src/Security/Voter/AuthorizationVoter.php index 3b406a2a7..0d4e76114 100644 --- a/src/Security/Voter/AuthorizationVoter.php +++ b/src/Security/Voter/AuthorizationVoter.php @@ -23,7 +23,7 @@ */ final class AuthorizationVoter extends Voter { - private const SUPPORTED_ATTRIBUTE = 'STUDIO_API'; + private const string SUPPORTED_ATTRIBUTE = 'STUDIO_API'; /** * {@inheritdoc} @@ -38,10 +38,6 @@ protected function supports(string $attribute, mixed $subject): bool */ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool { - if ($attribute !== self::SUPPORTED_ATTRIBUTE) { - return false; - } - - return true; + return $attribute === self::SUPPORTED_ATTRIBUTE; } } diff --git a/src/Security/Voter/UserPasswordVoter.php b/src/Security/Voter/UserPasswordVoter.php index 4ed0f5d60..b695bb625 100644 --- a/src/Security/Voter/UserPasswordVoter.php +++ b/src/Security/Voter/UserPasswordVoter.php @@ -51,12 +51,13 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter $userId = $this->getUserIdFromRequest(); $currentUser = $this->securityService->getCurrentUser(); + if ($userId === $currentUser->getId()) { // Allow user to update their own password return true; } - return $this->securityService->getCurrentUser()->isAllowed(UserPermissions::USER_MANAGEMENT->value); + return $currentUser->isAllowed(UserPermissions::USER_MANAGEMENT->value); } private function getUserIdFromRequest(): int