1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Controller/v1/User/UserGroupsController.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\Entity\UserGroup; |
13
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
14
|
|
|
use OpenApi\Attributes as OA; |
15
|
|
|
use OpenApi\Attributes\JsonContent; |
16
|
|
|
use OpenApi\Attributes\Property; |
17
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
21
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
22
|
|
|
use Symfony\Component\Routing\Requirement\Requirement; |
23
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
24
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class UserGroupsController |
28
|
|
|
* |
29
|
|
|
* @package App\Controller\v1\User |
30
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
#[AsController] |
33
|
|
|
class UserGroupsController |
34
|
|
|
{ |
35
|
15 |
|
public function __construct( |
36
|
|
|
private readonly SerializerInterface $serializer, |
37
|
|
|
) { |
38
|
15 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Endpoint action to fetch specified user user groups. |
42
|
|
|
*/ |
43
|
13 |
|
#[Route( |
44
|
|
|
path: '/v1/user/{user}/groups', |
45
|
|
|
requirements: [ |
46
|
|
|
'user' => Requirement::UUID_V1, |
47
|
|
|
], |
48
|
|
|
methods: [Request::METHOD_GET], |
49
|
|
|
)] |
50
|
|
|
#[IsGranted(new Expression('is_granted("IS_USER_HIMSELF", object) or "ROLE_ROOT" in role_names'), 'user')] |
51
|
|
|
#[OA\Tag(name: 'User Management')] |
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 groups', |
63
|
|
|
content: new JsonContent( |
64
|
|
|
type: 'array', |
65
|
|
|
items: new OA\Items( |
66
|
|
|
ref: new Model( |
67
|
|
|
type: UserGroup::class, |
68
|
|
|
groups: ['UserGroup', 'UserGroup.role'], |
69
|
|
|
), |
70
|
|
|
), |
71
|
|
|
), |
72
|
|
|
)] |
73
|
|
|
#[OA\Response( |
74
|
|
|
response: 401, |
75
|
|
|
description: 'Invalid token', |
76
|
|
|
content: new JsonContent( |
77
|
|
|
properties: [ |
78
|
|
|
new Property(property: 'code', type: 'integer'), |
79
|
|
|
new Property(property: 'message', type: 'string'), |
80
|
|
|
], |
81
|
|
|
type: 'object', |
82
|
|
|
example: [ |
83
|
|
|
'Token not found' => "{code: 401, message: 'JWT Token not found'}", |
84
|
|
|
'Expired token' => "{code: 401, message: 'Expired JWT Token'}", |
85
|
|
|
], |
86
|
|
|
), |
87
|
|
|
)] |
88
|
|
|
#[OA\Response( |
89
|
|
|
response: 403, |
90
|
|
|
description: 'Access denied', |
91
|
|
|
content: new JsonContent( |
92
|
|
|
properties: [ |
93
|
|
|
new Property(property: 'code', type: 'integer'), |
94
|
|
|
new Property(property: 'message', type: 'string'), |
95
|
|
|
], |
96
|
|
|
type: 'object', |
97
|
|
|
example: [ |
98
|
|
|
'Access denied' => "{code: 403, message: 'Access denied'}", |
99
|
|
|
], |
100
|
|
|
), |
101
|
|
|
)] |
102
|
|
|
public function __invoke(User $user): JsonResponse |
103
|
|
|
{ |
104
|
13 |
|
$groups = [ |
105
|
13 |
|
'groups' => [ |
106
|
13 |
|
UserGroup::SET_USER_GROUP_BASIC, |
107
|
13 |
|
], |
108
|
13 |
|
]; |
109
|
|
|
|
110
|
13 |
|
return new JsonResponse( |
111
|
13 |
|
$this->serializer->serialize($user->getUserGroups()->getValues(), 'json', $groups), |
112
|
13 |
|
json: true |
113
|
13 |
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|