1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Controller/v1/Profile/IndexController.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Controller\v1\Profile; |
10
|
|
|
|
11
|
|
|
use App\Entity\User; |
12
|
|
|
use App\Security\RolesService; |
13
|
|
|
use App\Utils\JSON; |
14
|
|
|
use JsonException; |
15
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
16
|
|
|
use OpenApi\Attributes as OA; |
17
|
|
|
use OpenApi\Attributes\JsonContent; |
18
|
|
|
use OpenApi\Attributes\Property; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
22
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
23
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; |
24
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
25
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class IndexController |
29
|
|
|
* |
30
|
|
|
* @package App\Controller\v1\Profile |
31
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
#[AsController] |
34
|
|
|
class IndexController |
35
|
|
|
{ |
36
|
6 |
|
public function __construct( |
37
|
|
|
private readonly SerializerInterface $serializer, |
38
|
|
|
private readonly RolesService $rolesService, |
39
|
|
|
) { |
40
|
6 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Endpoint action to get current user profile data. |
44
|
|
|
* |
45
|
|
|
* @throws JsonException |
46
|
|
|
*/ |
47
|
3 |
|
#[Route( |
48
|
|
|
path: '/v1/profile', |
49
|
|
|
methods: [Request::METHOD_GET], |
50
|
|
|
)] |
51
|
|
|
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)] |
52
|
|
|
#[OA\Parameter( |
53
|
|
|
name: 'Authorization', |
54
|
|
|
description: 'Authorization header', |
55
|
|
|
in: 'header', |
56
|
|
|
required: true, |
57
|
|
|
example: 'Bearer {token}', |
58
|
|
|
allowReserved: true, |
59
|
|
|
)] |
60
|
|
|
#[OA\Response( |
61
|
|
|
response: 200, |
62
|
|
|
description: 'User profile data', |
63
|
|
|
content: new JsonContent( |
64
|
|
|
ref: new Model( |
65
|
|
|
type: User::class, |
66
|
|
|
groups: ['set.UserProfile'], |
67
|
|
|
), |
68
|
|
|
type: 'object', |
69
|
|
|
), |
70
|
|
|
)] |
71
|
|
|
#[OA\Response( |
72
|
|
|
response: 401, |
73
|
|
|
description: 'Invalid token', |
74
|
|
|
content: new JsonContent( |
75
|
|
|
properties: [ |
76
|
|
|
new Property(property: 'code', type: 'integer'), |
77
|
|
|
new Property(property: 'message', type: 'string'), |
78
|
|
|
], |
79
|
|
|
type: 'object', |
80
|
|
|
example: [ |
81
|
|
|
'Token not found' => "{code: 401, message: 'JWT Token not found'}", |
82
|
|
|
'Expired token' => "{code: 401, message: 'Expired JWT Token'}", |
83
|
|
|
], |
84
|
|
|
), |
85
|
|
|
)] |
86
|
|
|
#[OA\Response( |
87
|
|
|
response: 403, |
88
|
|
|
description: 'Access denied', |
89
|
|
|
content: new JsonContent( |
90
|
|
|
properties: [ |
91
|
|
|
new Property(property: 'code', type: 'integer'), |
92
|
|
|
new Property(property: 'message', type: 'string'), |
93
|
|
|
], |
94
|
|
|
type: 'object', |
95
|
|
|
example: [ |
96
|
|
|
'Access denied' => "{code: 403, message: 'Access denied'}", |
97
|
|
|
], |
98
|
|
|
), |
99
|
|
|
)] |
100
|
|
|
#[OA\Tag(name: 'Profile')] |
101
|
|
|
public function __invoke(User $loggedInUser): JsonResponse |
102
|
|
|
{ |
103
|
|
|
/** @var array<string, string|array<string, string>> $output */ |
104
|
3 |
|
$output = JSON::decode( |
105
|
3 |
|
$this->serializer->serialize( |
106
|
3 |
|
$loggedInUser, |
107
|
3 |
|
'json', |
108
|
3 |
|
[ |
109
|
3 |
|
'groups' => User::SET_USER_PROFILE, |
110
|
3 |
|
] |
111
|
3 |
|
), |
112
|
3 |
|
true, |
113
|
3 |
|
); |
114
|
|
|
|
115
|
|
|
/** @var array<int, string> $roles */ |
116
|
3 |
|
$roles = $output['roles']; |
117
|
|
|
|
118
|
3 |
|
$output['roles'] = $this->rolesService->getInheritedRoles($roles); |
119
|
|
|
|
120
|
3 |
|
return new JsonResponse($output); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|