Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Task]: Overwrite UserPerspectiveListener to store perspective in session #606

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
Loading