/
IndexController.php
104 lines (97 loc) · 2.96 KB
/
IndexController.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
<?php
declare(strict_types = 1);
/**
* /src/Controller/v1/Profile/IndexController.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Controller\v1\Profile;
use App\Entity\User;
use App\Security\RolesService;
use App\Utils\JSON;
use JsonException;
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 IndexController
*
* @package App\Controller\v1\Profile
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class IndexController
{
public function __construct(
private SerializerInterface $serializer,
private RolesService $rolesService,
) {
}
/**
* Endpoint action to get current user profile data.
*
* @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 profile data",
* @OA\Schema(
* ref=@Model(
* type=User::class,
* groups={"set.UserProfile"},
* ),
* ),
* )
* @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\Tag(name="Profile")
*
* @throws JsonException
*/
#[Route(
path: '/v1/profile',
methods: [Request::METHOD_GET],
)]
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
public function __invoke(User $loggedInUser): JsonResponse
{
/** @var array<string, string|array<string, string>> $output */
$output = JSON::decode(
$this->serializer->serialize(
$loggedInUser,
'json',
[
'groups' => User::SET_USER_PROFILE,
]
),
true,
);
/** @var array<int, string> $roles */
$roles = $output['roles'];
$output['roles'] = $this->rolesService->getInheritedRoles($roles);
return new JsonResponse($output);
}
}