|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Controller/v1/User/UserRolesController.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Controller\v1\User; |
|
10
|
|
|
|
|
11
|
|
|
use App\Entity\User; |
|
12
|
|
|
use App\Security\RolesService; |
|
13
|
|
|
use OpenApi\Attributes as OA; |
|
14
|
|
|
use OpenApi\Attributes\JsonContent; |
|
15
|
|
|
use OpenApi\Attributes\Property; |
|
16
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
|
20
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
21
|
|
|
use Symfony\Component\Routing\Requirement\Requirement; |
|
22
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class UserRolesController |
|
26
|
|
|
* |
|
27
|
|
|
* @package App\Controller\v1\User |
|
28
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
#[AsController] |
|
31
|
|
|
class UserRolesController |
|
32
|
|
|
{ |
|
33
|
12 |
|
public function __construct( |
|
34
|
|
|
private readonly RolesService $rolesService, |
|
35
|
|
|
) { |
|
36
|
12 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Endpoint action to fetch specified user roles. |
|
40
|
|
|
*/ |
|
41
|
9 |
|
#[Route( |
|
42
|
|
|
path: '/v1/user/{user}/roles', |
|
43
|
|
|
requirements: [ |
|
44
|
|
|
'user' => Requirement::UUID_V1, |
|
45
|
|
|
], |
|
46
|
|
|
methods: [Request::METHOD_GET], |
|
47
|
|
|
)] |
|
48
|
|
|
#[IsGranted(new Expression('is_granted("IS_USER_HIMSELF", object) or "ROLE_ROOT" in role_names'), 'user')] |
|
49
|
|
|
#[OA\Tag(name: 'User Management')] |
|
50
|
|
|
#[OA\Parameter( |
|
51
|
|
|
name: 'Authorization', |
|
52
|
|
|
description: 'Authorization header', |
|
53
|
|
|
in: 'header', |
|
54
|
|
|
required: true, |
|
55
|
|
|
example: 'Bearer {token}', |
|
56
|
|
|
allowReserved: true, |
|
57
|
|
|
)] |
|
58
|
|
|
#[OA\Response( |
|
59
|
|
|
response: 200, |
|
60
|
|
|
description: 'Specified user roles', |
|
61
|
|
|
content: new JsonContent( |
|
62
|
|
|
type: 'array', |
|
63
|
|
|
items: new OA\Items(type: 'string', example: 'ROLE_USER'), |
|
64
|
|
|
example: ['ROLE_USER', 'ROLE_LOGGED'], |
|
65
|
|
|
), |
|
66
|
|
|
)] |
|
67
|
|
|
#[OA\Response( |
|
68
|
|
|
response: 401, |
|
69
|
|
|
description: 'Invalid token', |
|
70
|
|
|
content: new JsonContent( |
|
71
|
|
|
properties: [ |
|
72
|
|
|
new Property(property: 'code', type: 'integer'), |
|
73
|
|
|
new Property(property: 'message', type: 'string'), |
|
74
|
|
|
], |
|
75
|
|
|
type: 'object', |
|
76
|
|
|
example: [ |
|
77
|
|
|
'Token not found' => "{code: 401, message: 'JWT Token not found'}", |
|
78
|
|
|
'Expired token' => "{code: 401, message: 'Expired JWT Token'}", |
|
79
|
|
|
], |
|
80
|
|
|
), |
|
81
|
|
|
)] |
|
82
|
|
|
#[OA\Response( |
|
83
|
|
|
response: 403, |
|
84
|
|
|
description: 'Access denied', |
|
85
|
|
|
content: new JsonContent( |
|
86
|
|
|
properties: [ |
|
87
|
|
|
new Property(property: 'code', type: 'integer'), |
|
88
|
|
|
new Property(property: 'message', type: 'string'), |
|
89
|
|
|
], |
|
90
|
|
|
type: 'object', |
|
91
|
|
|
example: [ |
|
92
|
|
|
'Access denied' => "{code: 403, message: 'Access denied'}", |
|
93
|
|
|
], |
|
94
|
|
|
), |
|
95
|
|
|
)] |
|
96
|
|
|
public function __invoke(User $user): JsonResponse |
|
97
|
|
|
{ |
|
98
|
9 |
|
return new JsonResponse($this->rolesService->getInheritedRoles($user->getRoles())); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|