1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components 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\ApiComponentsBundle\Serializer\ContextBuilder; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
19
|
|
|
use Silverback\ApiComponentsBundle\Serializer\MappingLoader\CwaResourceLoader; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Security; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class CwaResourceContextBuilder implements SerializerContextBuilderInterface |
28
|
|
|
{ |
29
|
|
|
private SerializerContextBuilderInterface $decorated; |
30
|
|
|
private RoleHierarchyInterface $roleHierarchy; |
31
|
|
|
private Security $security; |
32
|
|
|
|
33
|
|
|
public function __construct(SerializerContextBuilderInterface $decorated, RoleHierarchyInterface $roleHierarchy, Security $security) |
34
|
|
|
{ |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
$this->roleHierarchy = $roleHierarchy; |
37
|
|
|
$this->security = $security; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array |
41
|
|
|
{ |
42
|
|
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
43
|
|
|
if (!is_a($resourceClass = $context['resource_class'], AbstractComponent::class, true) && !is_a($resourceClass = $context['resource_class'], AbstractPageData::class, true)) { |
44
|
|
|
return $context; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$reflectionClass = new \ReflectionClass($resourceClass); |
48
|
|
|
$shortName = $reflectionClass->getShortName(); |
49
|
|
|
$componentNames = [$shortName]; |
50
|
|
|
while ($parent = $reflectionClass->getParentClass()) { |
51
|
|
|
$componentNames[] = $parent->getShortName(); |
52
|
|
|
$reflectionClass = $parent; |
53
|
|
|
} |
54
|
|
|
$rw = $normalization ? 'read' : 'write'; |
55
|
|
|
foreach ($componentNames as $componentName) { |
56
|
|
|
$context['groups'][] = sprintf('%s:%s:%s', $componentName, CwaResourceLoader::GROUP_NAME, $rw); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$user = $this->security->getUser(); |
60
|
|
|
if ($user) { |
61
|
|
|
$reachableRoles = $this->roleHierarchy->getReachableRoleNames($user->getRoles()); |
62
|
|
|
foreach ($reachableRoles as $reachableRole) { |
63
|
|
|
$context['groups'][] = sprintf('%s:%s:%s:%s', $shortName, CwaResourceLoader::GROUP_NAME, $rw, strtolower($reachableRole)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $context; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|