1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
19
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
20
|
|
|
|
21
|
|
|
class AdminContextBuilder implements SerializerContextBuilderInterface |
22
|
|
|
{ |
23
|
|
|
private SerializerContextBuilderInterface $decorated; |
24
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
25
|
|
|
private ClassMetadataFactoryInterface $classMetadataFactory; |
26
|
|
|
|
27
|
|
|
public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker, ClassMetadataFactoryInterface $classMetadataFactory) |
28
|
|
|
{ |
29
|
|
|
$this->decorated = $decorated; |
30
|
|
|
$this->authorizationChecker = $authorizationChecker; |
31
|
|
|
$this->classMetadataFactory = $classMetadataFactory; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array |
35
|
|
|
{ |
36
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
37
|
|
|
|
38
|
|
|
$serializerGroupsConfigured = isset($context['groups']) && \is_array($context['groups']); |
39
|
|
|
if (!$serializerGroupsConfigured && $this->classHasSerializerGroupsOnProperties($context['resource_class'])) { |
40
|
|
|
$context['groups'] = []; |
41
|
|
|
$serializerGroupsConfigured = true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($serializerGroupsConfigured) { |
45
|
|
|
$context['groups'] = $this->getAllSerializationGroups($context['groups'], $normalization); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $context; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function classHasSerializerGroupsOnProperties(string $resourceClass): bool |
52
|
|
|
{ |
53
|
|
|
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($resourceClass); |
54
|
|
|
$serializerAttributeMetadata = $serializerClassMetadata->getAttributesMetadata(); |
55
|
|
|
foreach ($serializerAttributeMetadata as $metadata) { |
56
|
|
|
if (\count($metadata->groups)) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getAllSerializationGroups(array $groups, bool $normalization): array |
65
|
|
|
{ |
66
|
|
|
array_push($groups, ...$this->getSerializationGroups('default', $normalization)); |
67
|
|
|
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) { |
68
|
|
|
array_push($groups, ...$this->getSerializationGroups('admin', $normalization)); |
69
|
|
|
} |
70
|
|
|
if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) { |
71
|
|
|
array_push($groups, ...$this->getSerializationGroups('super_admin', $normalization)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $groups; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getSerializationGroups(string $groupName, bool $normalization): array |
78
|
|
|
{ |
79
|
|
|
return [$groupName, sprintf('%s_%s', $groupName, $normalization ? 'read' : 'write')]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|