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\ApiPlatform\Metadata\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Metadata\ApiResource; |
17
|
|
|
use ApiPlatform\Metadata\Get; |
18
|
|
|
use ApiPlatform\Metadata\HttpOperation; |
19
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
20
|
|
|
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
21
|
|
|
use Silverback\ApiComponentsBundle\DataProvider\StateProvider\UserStateProvider; |
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adds a /me endpoint. |
26
|
|
|
* |
27
|
|
|
* @author Daniel West <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class UserResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
30
|
|
|
{ |
31
|
|
|
private ResourceMetadataCollectionFactoryInterface $decorated; |
32
|
|
|
|
33
|
|
|
public function __construct(ResourceMetadataCollectionFactoryInterface $decorated) |
34
|
|
|
{ |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create(string $resourceClass): ResourceMetadataCollection |
39
|
|
|
{ |
40
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
41
|
|
|
if (!is_a($resourceClass, AbstractUser::class, true)) { |
42
|
|
|
return $resourceMetadata; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$newResources = []; |
46
|
|
|
/** @var ApiResource $resourceMetadatum */ |
47
|
|
|
foreach ($resourceMetadata as $resourceMetadatum) { |
48
|
|
|
$operations = $resourceMetadatum->getOperations(); |
49
|
|
|
if ($operations) { |
50
|
|
|
foreach ($operations as $operation) { |
51
|
|
|
if ($operation instanceof Get) { |
52
|
|
|
$newOperation = (new HttpOperation(HttpOperation::METHOD_GET, '/me.{_format}')) |
53
|
|
|
->withShortName($operation->getShortName()); |
54
|
|
|
$operations->add( |
55
|
|
|
'me', |
56
|
|
|
$newOperation |
57
|
|
|
->withName('me') |
58
|
|
|
->withSecurity('is_granted("IS_AUTHENTICATED_FULLY")') |
59
|
|
|
->withClass($operation->getClass()) |
60
|
|
|
->withProvider(UserStateProvider::class) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$newResources[] = $resourceMetadatum; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new ResourceMetadataCollection($resourceClass, $newResources); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|