src/Controller/v1/Role/InheritedRolesController.php
<?php
declare(strict_types = 1);
/**
* /src/Controller/v1/Role/InheritedRolesController.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Controller\v1\Role;
use App\Entity\Role;
use App\Enum\Role as RoleEnum;
use App\Security\RolesService;
use OpenApi\Attributes as OA;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\EnumRequirement;
use Symfony\Component\Security\Http\Attribute\IsGranted;
/**
* @package App\Controller\v1\Role
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
#[AsController]
#[OA\Tag(name: 'Role Management')]
class InheritedRolesController
{
public function __construct(
private readonly RolesService $rolesService,
) {
}
/**
* Endpoint action to return all inherited roles as an array for specified
* Role.
*/
#[Route(
path: '/v1/role/{role}/inherited',
requirements: [
'role' => new EnumRequirement(RoleEnum::class),
],
methods: [Request::METHOD_GET],
)]
#[IsGranted(RoleEnum::ADMIN->value)]
#[OA\Parameter(
name: 'Authorization',
description: 'Authorization header',
in: 'header',
required: true,
example: 'Bearer {token}',
allowReserved: true,
)]
#[OA\Response(
response: 200,
description: 'Inherited roles',
content: new JsonContent(
type: 'array',
items: new OA\Items(type: 'string', example: 'ROLE_USER'),
example: ['ROLE_USER', 'ROLE_LOGGED'],
),
)]
#[OA\Response(
response: 401,
description: 'Invalid token',
content: new JsonContent(
properties: [
new Property(property: 'code', type: 'integer'),
new Property(property: 'message', type: 'string'),
],
type: 'object',
example: [
'Token not found' => "{code: 401, message: 'JWT Token not found'}",
'Expired token' => "{code: 401, message: 'Expired JWT Token'}",
],
),
)]
#[OA\Response(
response: 403,
description: 'Access denied',
content: new JsonContent(
properties: [
new Property(property: 'code', type: 'integer'),
new Property(property: 'message', type: 'string'),
],
type: 'object',
example: [
'Access denied' => "{code: 403, message: 'Access denied'}",
],
),
)]
public function __invoke(Role $role): JsonResponse
{
return new JsonResponse($this->rolesService->getInheritedRoles([$role->getId()]));
}
}