1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Controller/v1/Profile/GroupsController.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\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\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\Security\Core\Authorization\Voter\AuthenticatedVoter; |
22
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
23
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class GroupsController |
27
|
|
|
* |
28
|
|
|
* @package App\Controller\v1\Profile |
29
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
#[AsController] |
32
|
|
|
class GroupsController |
33
|
|
|
{ |
34
|
10 |
|
public function __construct( |
35
|
|
|
private readonly SerializerInterface $serializer, |
36
|
|
|
) { |
37
|
10 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Endpoint action to get current user user groups. |
41
|
|
|
*/ |
42
|
7 |
|
#[Route( |
43
|
|
|
path: '/v1/profile/groups', |
44
|
|
|
methods: [Request::METHOD_GET], |
45
|
|
|
)] |
46
|
|
|
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)] |
47
|
|
|
#[OA\Parameter( |
48
|
|
|
name: 'Authorization', |
49
|
|
|
description: 'Authorization header', |
50
|
|
|
in: 'header', |
51
|
|
|
required: true, |
52
|
|
|
example: 'Bearer {token}', |
53
|
|
|
allowReserved: true, |
54
|
|
|
)] |
55
|
|
|
#[OA\Response( |
56
|
|
|
response: 200, |
57
|
|
|
description: 'List of logged in user user groups', |
58
|
|
|
content: new JsonContent( |
59
|
|
|
type: 'array', |
60
|
|
|
items: new OA\Items( |
61
|
|
|
ref: new Model( |
62
|
|
|
type: UserGroup::class, |
63
|
|
|
groups: ['set.UserProfileGroups'], |
64
|
|
|
), |
65
|
|
|
), |
66
|
|
|
), |
67
|
|
|
)] |
68
|
|
|
#[OA\Response( |
69
|
|
|
response: 401, |
70
|
|
|
description: 'Invalid token', |
71
|
|
|
content: new JsonContent( |
72
|
|
|
properties: [ |
73
|
|
|
new Property(property: 'code', type: 'integer'), |
74
|
|
|
new Property(property: 'message', type: 'string'), |
75
|
|
|
], |
76
|
|
|
type: 'object', |
77
|
|
|
example: [ |
78
|
|
|
'Token not found' => "{code: 401, message: 'JWT Token not found'}", |
79
|
|
|
'Expired token' => "{code: 401, message: 'Expired JWT Token'}", |
80
|
|
|
], |
81
|
|
|
), |
82
|
|
|
)] |
83
|
|
|
#[OA\Response( |
84
|
|
|
response: 403, |
85
|
|
|
description: 'Access denied', |
86
|
|
|
content: new JsonContent( |
87
|
|
|
properties: [ |
88
|
|
|
new Property(property: 'code', type: 'integer'), |
89
|
|
|
new Property(property: 'message', type: 'string'), |
90
|
|
|
], |
91
|
|
|
type: 'object', |
92
|
|
|
example: [ |
93
|
|
|
'Access denied' => "{code: 403, message: 'Access denied'}", |
94
|
|
|
], |
95
|
|
|
), |
96
|
|
|
)] |
97
|
|
|
#[OA\Tag(name: 'Profile')] |
98
|
|
|
public function __invoke(User $loggedInUser): JsonResponse |
99
|
|
|
{ |
100
|
7 |
|
return new JsonResponse( |
101
|
7 |
|
$this->serializer->serialize( |
102
|
7 |
|
$loggedInUser->getUserGroups()->toArray(), |
103
|
7 |
|
'json', |
104
|
7 |
|
[ |
105
|
7 |
|
'groups' => UserGroup::SET_USER_PROFILE_GROUPS, |
106
|
7 |
|
], |
107
|
7 |
|
), |
108
|
7 |
|
json: true, |
109
|
7 |
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|