1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ForumBundle\Serializer; |
6
|
|
|
|
7
|
|
|
use ApiPlatform\State\SerializerContextBuilderInterface; |
8
|
|
|
use ProjetNormandie\ForumBundle\Entity\Category; |
9
|
|
|
use ProjetNormandie\ForumBundle\Entity\Forum; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
12
|
|
|
|
13
|
|
|
final class ForumContextBuilder implements SerializerContextBuilderInterface |
14
|
|
|
{ |
15
|
|
|
private SerializerContextBuilderInterface $decorated; |
16
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
SerializerContextBuilderInterface $decorated, |
20
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
21
|
|
|
) { |
22
|
|
|
$this->decorated = $decorated; |
23
|
|
|
$this->authorizationChecker = $authorizationChecker; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array |
27
|
|
|
{ |
28
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
29
|
|
|
$resourceClass = $context['resource_class'] ?? null; |
30
|
|
|
|
31
|
|
|
// Gestion du endpoint GetHome pour Category |
32
|
|
|
if ( |
33
|
|
|
$resourceClass === Category::class |
34
|
|
|
&& str_contains($request->getRequestUri(), '/forum_category/get-home') |
35
|
|
|
&& isset($context['groups']) |
36
|
|
|
&& true === $normalization |
37
|
|
|
) { |
38
|
|
|
// Groups de base pour tous (guests et users) |
39
|
|
|
$baseGroups = [ |
40
|
|
|
'category:read', |
41
|
|
|
'category:forums', |
42
|
|
|
'forum:read', |
43
|
|
|
'forum:last-message', |
44
|
|
|
'message:read', |
45
|
|
|
'message:user', |
46
|
|
|
'user:read:minimal', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
// Si l'utilisateur est connecté, ajouter les groups spécifiques |
50
|
|
|
if ($this->authorizationChecker->isGranted('ROLE_USER')) { |
51
|
|
|
$baseGroups[] = 'forum:read-status'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$context['groups'] = $baseGroups; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $context; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|