From d9a5aea4c2f1d705e9b89400ec7f28f6f759f372 Mon Sep 17 00:00:00 2001 From: JiaJia Ji Date: Thu, 21 Nov 2024 11:40:15 +0100 Subject: [PATCH] [Task]: Overwrite UserPerspectiveListener to store perspective in session (#606) --- config/services.yaml | 8 +- .../UserSessionPerspectiveListener.php | 84 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/EventListener/UserSessionPerspectiveListener.php diff --git a/config/services.yaml b/config/services.yaml index f7b66e32..5623e782 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -204,7 +204,13 @@ services: tags: - { name: 'kernel.event_subscriber', dispatcher: 'security.event_dispatcher.pimcore_admin' } - + Pimcore\Bundle\AdminBundle\EventListener\UserPerspectiveListener: + class: App\EventListener\UserSessionPerspectiveListener + calls: + - [ setLogger, [ '@logger' ] ] + tags: + - { name: kernel.event_subscriber } + - { name: monolog.logger, channel: admin } # --------------------------------------------------------- diff --git a/src/EventListener/UserSessionPerspectiveListener.php b/src/EventListener/UserSessionPerspectiveListener.php new file mode 100644 index 00000000..a5161fdd --- /dev/null +++ b/src/EventListener/UserSessionPerspectiveListener.php @@ -0,0 +1,84 @@ +get('perspective'); + $sessionPerspective = $request->getSession()->get('sessionPerspective'); + + if ($requestedPerspective) { + if ($requestedPerspective !== $sessionPerspective) { + $existingPerspectives = array_keys(\Pimcore\Bundle\AdminBundle\Perspective\Config::get()); + if (!in_array($requestedPerspective, $existingPerspectives)) { + $this->logger->warning('Requested perspective {perspective} for {user} does not exist.', [ + 'user' => $user->getName(), + 'perspective' => $requestedPerspective, + ]); + + $requestedPerspective = null; + } + } + } + + if (!$requestedPerspective || !$user->isAllowed($requestedPerspective, 'perspective')) { + $previouslyRequested = $requestedPerspective; + + // if the session perspective is not allowed anymore, set the first available + if ($sessionPerspective) { + $requestedPerspective = $user->isAllowed($sessionPerspective, 'perspective') + ? $sessionPerspective + : $user->getFirstAllowedPerspective(); + } else { + $requestedPerspective = 'default'; + } + + if ($previouslyRequested) { + $this->logger->warning('User {user} is not allowed requested perspective {requestedPerspective}. Falling back to {perspective}.', [ + 'user' => $user->getName(), + 'requestedPerspective' => $previouslyRequested, + 'perspective' => $requestedPerspective, + ]); + } else { + $this->logger->debug('Perspective for user {user} was not requested. Falling back to {perspective}.', [ + 'user' => $user->getName(), + 'perspective' => $requestedPerspective, + ]); + } + } + + if ($requestedPerspective !== $sessionPerspective) { + $request->getSession()->set('sessionPerspective', $requestedPerspective); + + $user->setActivePerspective($requestedPerspective); + $user->save(); + } elseif ($sessionPerspective) { + $user->setActivePerspective($sessionPerspective); + $user->save(); + } + } +}