Skip to content

Commit

Permalink
[Task]: Overwrite UserPerspectiveListener to store perspective in ses…
Browse files Browse the repository at this point in the history
…sion (#606)
  • Loading branch information
kingjia90 authored Nov 21, 2024
1 parent 026442f commit d9a5aea
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }


# ---------------------------------------------------------
Expand Down
84 changes: 84 additions & 0 deletions src/EventListener/UserSessionPerspectiveListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace App\EventListener;

use Pimcore\Bundle\AdminBundle\EventListener\UserPerspectiveListener;
use Pimcore\Model\User;
use Symfony\Component\HttpFoundation\Request;

/**
* @internal
*/
class UserSessionPerspectiveListener extends UserPerspectiveListener
{
protected function setRequestedPerspective(User $user, Request $request): void
{
// update perspective settings
$requestedPerspective = $request->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();
}
}
}

0 comments on commit d9a5aea

Please sign in to comment.