/
GroupsController.php
106 lines (102 loc) · 3.2 KB
/
GroupsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types = 1);
/**
* /src/Controller/v1/Profile/GroupsController.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Controller\v1\Profile;
use App\Entity\User;
use App\Entity\UserGroup;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Class GroupsController
*
* @package App\Controller\v1\Profile
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class GroupsController
{
public function __construct(
private SerializerInterface $serializer,
) {
}
/**
* Endpoint action to get current user user groups.
*
* @OA\Parameter(
* name="Authorization",
* in="header",
* required=true,
* description="Authorization header",
* @OA\Schema(
* type="string",
* default="Bearer _your_jwt_here_",
* ),
* )
* @OA\Response(
* response=200,
* description="User groups",
* @OA\Schema(
* type="array",
* @OA\Items(
* ref=@Model(
* type=\App\Entity\UserGroup::class,
* groups={"set.UserProfileGroups"},
* ),
* ),
* ),
* )
* @OA\Response(
* response=401,
* description="Invalid token",
* @OA\Schema(
* type="object",
* example={
* "Token not found": "{code: 401, message: 'JWT Token not found'}",
* "Expired token": "{code: 401, message: 'Expired JWT Token'}",
* },
* @OA\Property(property="code", type="integer", description="Error code"),
* @OA\Property(property="message", type="string", description="Error description"),
* ),
* )
* @OA\Response(
* response=403,
* description="Access denied",
* @OA\Schema(
* type="403",
* example={
* "Access denied": "{code: 403, message: 'Access denied'}",
* },
* @OA\Property(property="code", type="integer", description="Error code"),
* @OA\Property(property="message", type="string", description="Error description"),
* ),
* )
* @OA\Tag(name="Profile")
*/
#[Route(
path: '/v1/profile/groups',
methods: [Request::METHOD_GET],
)]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(User $loggedInUser): JsonResponse
{
return new JsonResponse(
$this->serializer->serialize(
$loggedInUser->getUserGroups()->toArray(),
'json',
[
'groups' => UserGroup::SET_USER_PROFILE_GROUPS,
],
),
json: true,
);
}
}