Completed
Push — master ( b0e9fb...0c2496 )
by Tarmo
35s queued 13s
created

RolesController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Annotations as OA;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\Annotation\Route;
18
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
19
20
/**
21
 * Class RolesController
22
 *
23
 * @package App\Controller\v1\Profile
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class RolesController
27
{
28 17
    public function __construct(
29
        private RolesService $rolesService,
30
    ) {
31 17
    }
32
33
    /**
34
     * Endpoint action to get current user roles as an array.
35
     *
36
     * @OA\Parameter(
37
     *      name="Authorization",
38
     *      in="header",
39
     *      required=true,
40
     *      description="Authorization header",
41
     *      @OA\Schema(
42
     *          type="string",
43
     *          default="Bearer _your_jwt_here_",
44
     *      ),
45
     *  )
46
     * @OA\Response(
47
     *      response=200,
48
     *      description="User roles",
49
     *      @OA\Schema(
50
     *          type="array",
51
     *          @OA\Items(type="string"),
52
     *      ),
53
     *  )
54
     * @OA\Response(
55
     *      response=401,
56
     *      description="Invalid token",
57
     *      @OA\Schema(
58
     *          type="object",
59
     *          example={
60
     *              "Token not found": "{code: 401, message: 'JWT Token not found'}",
61
     *              "Expired token": "{code: 401, message: 'Expired JWT Token'}",
62
     *          },
63
     *          @OA\Property(property="code", type="integer", description="Error code"),
64
     *          @OA\Property(property="message", type="string", description="Error description"),
65
     *      ),
66
     *  )
67
     * @OA\Tag(name="Profile")
68
     */
69 10
    #[Route(
70
        path: '/v1/profile/roles',
71
        methods: [Request::METHOD_GET],
72
    )]
73
    #[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
74
    public function __invoke(User $loggedInUser): JsonResponse
75
    {
76 10
        return new JsonResponse($this->rolesService->getInheritedRoles($loggedInUser->getRoles()));
77
    }
78
}
79