12
12
use RZ \Roadiz \CoreBundle \Security \Authorization \Voter \RealmVoter ;
13
13
use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
14
14
use Symfony \Bundle \SecurityBundle \Security ;
15
+ use Symfony \Component \String \Slugger \AsciiSlugger ;
15
16
16
17
final class RealmResolver implements RealmResolverInterface
17
18
{
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
+ ) {
27
24
}
28
25
29
26
public function getRealms (?Node $ node ): array
@@ -49,9 +46,16 @@ public function denyUnlessGranted(RealmInterface $realm): void
49
46
}
50
47
}
51
48
49
+ private function getUserCacheKey (): string
50
+ {
51
+ return (new AsciiSlugger ())
52
+ ->slug ($ this ->security ->getUser ()?->getUserIdentifier() ?? 'anonymous ' )
53
+ ->__toString ();
54
+ }
55
+
52
56
public function getGrantedRealms (): array
53
57
{
54
- $ cacheItem = $ this ->cache ->getItem ('granted_realms ' );
58
+ $ cacheItem = $ this ->cache ->getItem ('granted_realms_ ' . $ this -> getUserCacheKey () );
55
59
if (!$ cacheItem ->isHit ()) {
56
60
$ allRealms = $ this ->managerRegistry ->getRepository (Realm::class)->findBy ([]);
57
61
$ cacheItem ->set (array_filter ($ allRealms , fn (RealmInterface $ realm ) => $ this ->isGranted ($ realm )));
@@ -63,7 +67,7 @@ public function getGrantedRealms(): array
63
67
64
68
public function getDeniedRealms (): array
65
69
{
66
- $ cacheItem = $ this ->cache ->getItem ('denied_realms ' );
70
+ $ cacheItem = $ this ->cache ->getItem ('denied_realms_ ' . $ this -> getUserCacheKey () );
67
71
if (!$ cacheItem ->isHit ()) {
68
72
$ allRealms = $ this ->managerRegistry ->getRepository (Realm::class)->findBy ([]);
69
73
$ cacheItem ->set (array_filter ($ allRealms , fn (RealmInterface $ realm ) => !$ this ->isGranted ($ realm )));
0 commit comments