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\ContextBuilder; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
17
|
|
|
use Silverback\ApiComponentBundle\Entity\User\AbstractUser; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
20
|
|
|
|
21
|
|
|
class UserContextBuilder implements SerializerContextBuilderInterface |
22
|
|
|
{ |
23
|
|
|
private SerializerContextBuilderInterface $decorated; |
24
|
|
|
private AuthorizationCheckerInterface $authorizationChecker; |
25
|
|
|
|
26
|
5 |
|
public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker) |
27
|
|
|
{ |
28
|
5 |
|
$this->decorated = $decorated; |
29
|
5 |
|
$this->authorizationChecker = $authorizationChecker; |
30
|
5 |
|
} |
31
|
|
|
|
32
|
5 |
|
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array |
33
|
|
|
{ |
34
|
5 |
|
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); |
35
|
5 |
|
$resourceClass = @$context['resource_class'] ?? null; |
36
|
5 |
|
if (!is_subclass_of($resourceClass, AbstractUser::class)) { |
37
|
2 |
|
return $context; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$serializerGroupsConfigured = isset($context['groups']) && \is_array($context['groups']); |
41
|
3 |
|
if (!$serializerGroupsConfigured) { |
42
|
2 |
|
$context['groups'] = []; |
43
|
|
|
} |
44
|
3 |
|
$postfix = false === $normalization ? 'input' : 'output'; |
45
|
3 |
|
$context['groups'][] = sprintf('User:%s', $postfix); |
46
|
|
|
|
47
|
3 |
|
if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) { |
48
|
1 |
|
$context['groups'][] = 'User:super_admin'; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
return $context; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|