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] Update cache after user save #875

Merged
merged 2 commits into from
Mar 7, 2025
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
7 changes: 3 additions & 4 deletions src/Security/Voter/UserPermissionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Bundle\StaticResolverBundle\Lib\CacheResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\CacheKeys;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use function in_array;
Expand All @@ -32,8 +33,6 @@
*/
final class UserPermissionVoter extends Voter
{
private const USER_PERMISSIONS_CACHE_KEY = 'studio_backend_user_permissions';

private array $userPermissions;

public function __construct(
Expand Down Expand Up @@ -70,7 +69,7 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
*/
private function getUserPermissions(): void
{
$userPermissions = $this->cacheResolver->load(self::USER_PERMISSIONS_CACHE_KEY);
$userPermissions = $this->cacheResolver->load(CacheKeys::USER_PERMISSIONS->value);

if ($userPermissions !== false && is_array($userPermissions)) {
$this->userPermissions = $userPermissions;
Expand All @@ -82,7 +81,7 @@ private function getUserPermissions(): void

$this->cacheResolver->save(
$userPermissions,
self::USER_PERMISSIONS_CACHE_KEY
CacheKeys::USER_PERMISSIONS->value
);

$this->userPermissions = $userPermissions;
Expand Down
8 changes: 7 additions & 1 deletion src/User/Service/UserUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\User\Service;

use JsonException;
use Pimcore\Bundle\StaticResolverBundle\Lib\CacheResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\Authentication\AuthenticationResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
Expand All @@ -28,6 +29,7 @@
use Pimcore\Bundle\StudioBackendBundle\User\MappedParameter\UpdateUserParameter;
use Pimcore\Bundle\StudioBackendBundle\User\Repository\UserRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Schema\KeyBinding;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\CacheKeys;
use function sprintf;
use function strlen;

Expand All @@ -40,7 +42,8 @@ public function __construct(
private readonly UserRepositoryInterface $userRepository,
private readonly SecurityServiceInterface $securityService,
private readonly UpdateServiceInterface $updateService,
private readonly AuthenticationResolverInterface $authenticationResolver
private readonly AuthenticationResolverInterface $authenticationResolver,
private readonly CacheResolverInterface $cacheResolver,
) {
}

Expand Down Expand Up @@ -88,6 +91,9 @@ public function updateUserById(UpdateUserParameter $updateUserParameter, int $us
$user = $this->updateService->updateDocumentWorkspaces($updateUserParameter->getDocumentWorkspaces(), $user);

$this->userRepository->updateUser($user);

$this->cacheResolver->remove(CacheKeys::USER_PERMISSIONS->value);

}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Util/Constant/CacheKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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 Pimcore\Bundle\StudioBackendBundle\Util\Constant;

enum CacheKeys: string
{
case USER_PERMISSIONS = 'studio_backend_user_permissions';
}
Loading