Skip to content

Commit 8194ee7

Browse files
author
roadiz-ci
committed
Merge tag v2.2.8 into develop
1 parent 420a3cc commit 8194ee7

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Realm/RealmResolver.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\RealmVoter;
1313
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
1414
use Symfony\Bundle\SecurityBundle\Security;
15+
use Symfony\Component\String\Slugger\AsciiSlugger;
1516

1617
final class RealmResolver implements RealmResolverInterface
1718
{
18-
private ManagerRegistry $managerRegistry;
19-
private Security $security;
20-
private CacheItemPoolInterface $cache;
21-
22-
public function __construct(ManagerRegistry $managerRegistry, Security $security, CacheItemPoolInterface $cache)
23-
{
24-
$this->managerRegistry = $managerRegistry;
25-
$this->security = $security;
26-
$this->cache = $cache;
19+
public function __construct(
20+
private readonly ManagerRegistry $managerRegistry,
21+
private readonly Security $security,
22+
private readonly CacheItemPoolInterface $cache
23+
) {
2724
}
2825

2926
public function getRealms(?Node $node): array
@@ -49,9 +46,16 @@ public function denyUnlessGranted(RealmInterface $realm): void
4946
}
5047
}
5148

49+
private function getUserCacheKey(): string
50+
{
51+
return (new AsciiSlugger())
52+
->slug($this->security->getUser()?->getUserIdentifier() ?? 'anonymous')
53+
->__toString();
54+
}
55+
5256
public function getGrantedRealms(): array
5357
{
54-
$cacheItem = $this->cache->getItem('granted_realms');
58+
$cacheItem = $this->cache->getItem('granted_realms_' . $this->getUserCacheKey());
5559
if (!$cacheItem->isHit()) {
5660
$allRealms = $this->managerRegistry->getRepository(Realm::class)->findBy([]);
5761
$cacheItem->set(array_filter($allRealms, fn(RealmInterface $realm) => $this->isGranted($realm)));
@@ -63,7 +67,7 @@ public function getGrantedRealms(): array
6367

6468
public function getDeniedRealms(): array
6569
{
66-
$cacheItem = $this->cache->getItem('denied_realms');
70+
$cacheItem = $this->cache->getItem('denied_realms_' . $this->getUserCacheKey());
6771
if (!$cacheItem->isHit()) {
6872
$allRealms = $this->managerRegistry->getRepository(Realm::class)->findBy([]);
6973
$cacheItem->set(array_filter($allRealms, fn(RealmInterface $realm) => !$this->isGranted($realm)));

0 commit comments

Comments
 (0)