RolesController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 64
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 53 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Controller/v1/Profile/RolesController.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\Security\RolesService;
13
use OpenApi\Attributes as OA;
14
use OpenApi\Attributes\JsonContent;
15
use OpenApi\Attributes\Property;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpKernel\Attribute\AsController;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
21
use Symfony\Component\Security\Http\Attribute\IsGranted;
22
23
/**
24
 * Class RolesController
25
 *
26
 * @package App\Controller\v1\Profile
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 */
29
#[AsController]
30
class RolesController
31
{
32 10
    public function __construct(
33
        private readonly RolesService $rolesService,
34
    ) {
35 10
    }
36
37
    /**
38
     * Endpoint action to get current user roles as an array.
39
     */
40 7
    #[Route(
41
        path: '/v1/profile/roles',
42
        methods: [Request::METHOD_GET],
43
    )]
44
    #[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
45
    #[OA\Parameter(
46
        name: 'Authorization',
47
        description: 'Authorization header',
48
        in: 'header',
49
        example: 'Bearer {token}',
50
    )]
51
    #[OA\Response(
52
        response: 200,
53
        description: 'Logged in user roles',
54
        content: new JsonContent(
55
            type: 'array',
56
            items: new OA\Items(type: 'string', example: 'ROLE_USER'),
57
            example: ['ROLE_USER', 'ROLE_LOGGED'],
58
        ),
59
    )]
60
    #[OA\Response(
61
        response: 401,
62
        description: 'Invalid token',
63
        content: new JsonContent(
64
            properties: [
65
                new Property(property: 'code', type: 'integer'),
66
                new Property(property: 'message', type: 'string'),
67
            ],
68
            type: 'object',
69
            example: [
70
                'Token not found' => "{code: 401, message: 'JWT Token not found'}",
71
                'Expired token' => "{code: 401, message: 'Expired JWT Token'}",
72
            ],
73
        ),
74
    )]
75
    #[OA\Response(
76
        response: 403,
77
        description: 'Access denied',
78
        content: new JsonContent(
79
            properties: [
80
                new Property(property: 'code', type: 'integer'),
81
                new Property(property: 'message', type: 'string'),
82
            ],
83
            type: 'object',
84
            example: [
85
                'Access denied' => "{code: 403, message: 'Access denied'}",
86
            ],
87
        ),
88
    )]
89
    #[OA\Tag(name: 'Profile')]
90
    public function __invoke(User $loggedInUser): JsonResponse
91
    {
92 7
        return new JsonResponse($this->rolesService->getInheritedRoles($loggedInUser->getRoles()));
93
    }
94
}
95