Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UpdateUserRoleAction | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\SuperAdmin; |
| 6 | |
| 7 | use App\Domain\Auth\Data\UserAuthData; |
| 8 | use App\Domain\Exception\ForbiddenException; |
| 9 | use App\Domain\SuperAdmin\Service\SuperAdminService; |
| 10 | use App\Renderer\JsonRenderer; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | |
| 14 | /** |
| 15 | * Update a user's role (promote/demote to admin or super_admin). |
| 16 | */ |
| 17 | final readonly class UpdateUserRoleAction |
| 18 | { |
| 19 | public function __construct( |
| 20 | private JsonRenderer $renderer, |
| 21 | private SuperAdminService $service, |
| 22 | ) {} |
| 23 | |
| 24 | /** |
| 25 | * @param array<string, string> $args |
| 26 | * @param ServerRequestInterface $request |
| 27 | * @param ResponseInterface $response |
| 28 | */ |
| 29 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface |
| 30 | { |
| 31 | $user = $request->getAttribute('user'); |
| 32 | if (!$user instanceof UserAuthData || $user->role !== 'super_admin') { |
| 33 | throw new ForbiddenException('Super admin access required'); |
| 34 | } |
| 35 | |
| 36 | $userId = (int)$args['userId']; |
| 37 | $data = (array)$request->getParsedBody(); |
| 38 | $role = $data['role'] ?? ''; |
| 39 | |
| 40 | $result = $this->service->updateUserRole($user->userId, $userId, (string)$role); |
| 41 | |
| 42 | return $this->renderer->json($response, [ |
| 43 | 'success' => true, |
| 44 | 'message' => "User role updated to {$result['role']}", |
| 45 | 'data' => $result, |
| 46 | ]); |
| 47 | } |
| 48 | } |