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\ExpressionLanguage\Expression; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
20
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Vincent Chalamon <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class PublishableContextBuilder implements SerializerContextBuilderInterface |
26
|
|
|
{ |
27
|
|
|
private SerializerContextBuilderInterface $decorated; |
28
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
29
|
|
|
private ClassMetadataFactoryInterface $classMetadataFactory; |
30
|
|
|
private string $permission; |
31
|
|
|
|
32
|
|
|
public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker, ClassMetadataFactoryInterface $classMetadataFactory, string $permission) |
33
|
|
|
{ |
34
|
|
|
$this->decorated = $decorated; |
35
|
|
|
$this->authorizationChecker = $authorizationChecker; |
36
|
|
|
$this->classMetadataFactory = $classMetadataFactory; |
37
|
|
|
$this->permission = $permission; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array |
41
|
|
|
{ |
42
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
43
|
|
|
|
44
|
|
|
$serializerGroupsConfigured = isset($context['groups']) && \is_array($context['groups']); |
45
|
|
|
if (!$serializerGroupsConfigured && $this->classHasSerializerGroupsOnProperties($context['resource_class'])) { |
46
|
|
|
$context['groups'] = []; |
47
|
|
|
$serializerGroupsConfigured = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($serializerGroupsConfigured && $this->authorizationChecker->isGranted(new Expression($this->permission))) { |
51
|
|
|
array_push($context['groups'], ...[sprintf('%s:publishable', $context['resource_class'])]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $context; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function classHasSerializerGroupsOnProperties(string $resourceClass): bool |
58
|
|
|
{ |
59
|
|
|
$serializerAttributeMetadata = $this->classMetadataFactory->getMetadataFor($resourceClass)->getAttributesMetadata(); |
60
|
|
|
foreach ($serializerAttributeMetadata as $metadata) { |
61
|
|
|
if (\count($metadata->groups)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|