src/Controller/Api/User/Admin/UserBanApi.php
<?php
// SPDX-FileCopyrightText: 2023 /kbin contributors <https://kbin.pub/>
//
// SPDX-License-Identifier: AGPL-3.0-only
declare(strict_types=1);
namespace App\Controller\Api\User\Admin;
use App\Controller\Api\User\UserBaseApi;
use App\Entity\User;
use App\Kbin\User\DTO\UserBanResponseDto;
use App\Kbin\User\Factory\UserFactory;
use App\Kbin\User\UserBan;
use App\Kbin\User\UserUnban;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security;
use OpenApi\Attributes as OA;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class UserBanApi extends UserBaseApi
{
#[OA\Response(
response: 200,
description: 'User banned',
content: new Model(type: UserBanResponseDto::class),
headers: [
new OA\Header(
header: 'X-RateLimit-Remaining',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests left until you will be rate limited'
),
new OA\Header(
header: 'X-RateLimit-Retry-After',
schema: new OA\Schema(type: 'integer'),
description: 'Unix timestamp to retry the request after'
),
new OA\Header(
header: 'X-RateLimit-Limit',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests available'
),
]
)]
#[OA\Response(
response: 401,
description: 'Permission denied due to missing or expired token',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\UnauthorizedErrorSchema::class))
)]
#[OA\Response(
response: 403,
description: 'You are not authorized to ban this user',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\ForbiddenErrorSchema::class))
)]
#[OA\Response(
response: 404,
description: 'User not found',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\NotFoundErrorSchema::class))
)]
#[OA\Response(
response: 429,
description: 'You are being rate limited',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\TooManyRequestsErrorSchema::class)),
headers: [
new OA\Header(
header: 'X-RateLimit-Remaining',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests left until you will be rate limited'
),
new OA\Header(
header: 'X-RateLimit-Retry-After',
schema: new OA\Schema(type: 'integer'),
description: 'Unix timestamp to retry the request after'
),
new OA\Header(
header: 'X-RateLimit-Limit',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests available'
),
]
)]
#[OA\Parameter(
name: 'user_id',
in: 'path',
description: 'The user to ban',
schema: new OA\Schema(type: 'integer'),
)]
#[OA\Tag(name: 'admin/user')]
#[IsGranted('ROLE_ADMIN')]
#[Security(name: 'oauth2', scopes: ['admin:user:ban'])]
#[IsGranted('ROLE_OAUTH2_ADMIN:USER:BAN')]
/** Bans a user from the instance */
public function ban(
#[MapEntity(id: 'user_id')]
User $user,
UserBan $userBan,
UserFactory $factory,
RateLimiterFactory $apiModerateLimiter,
): JsonResponse {
$headers = $this->rateLimit($apiModerateLimiter);
$userBan($user);
// Response needs to be an array to insert isBanned
$response = $this->serializeUser($factory->createDto($user))->jsonSerialize();
$response['isBanned'] = $user->isBanned;
return new JsonResponse(
$response,
headers: $headers
);
}
#[OA\Response(
response: 200,
description: 'User unbanned',
content: new Model(type: UserBanResponseDto::class),
headers: [
new OA\Header(
header: 'X-RateLimit-Remaining',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests left until you will be rate limited'
),
new OA\Header(
header: 'X-RateLimit-Retry-After',
schema: new OA\Schema(type: 'integer'),
description: 'Unix timestamp to retry the request after'
),
new OA\Header(
header: 'X-RateLimit-Limit',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests available'
),
]
)]
#[OA\Response(
response: 401,
description: 'Permission denied due to missing or expired token',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\UnauthorizedErrorSchema::class))
)]
#[OA\Response(
response: 403,
description: 'You are not authorized to unban this user',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\ForbiddenErrorSchema::class))
)]
#[OA\Response(
response: 404,
description: 'User not found',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\NotFoundErrorSchema::class))
)]
#[OA\Response(
response: 429,
description: 'You are being rate limited',
content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\TooManyRequestsErrorSchema::class)),
headers: [
new OA\Header(
header: 'X-RateLimit-Remaining',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests left until you will be rate limited'
),
new OA\Header(
header: 'X-RateLimit-Retry-After',
schema: new OA\Schema(type: 'integer'),
description: 'Unix timestamp to retry the request after'
),
new OA\Header(
header: 'X-RateLimit-Limit',
schema: new OA\Schema(type: 'integer'),
description: 'Number of requests available'
),
]
)]
#[OA\Parameter(
name: 'user_id',
in: 'path',
description: 'The user to unban',
schema: new OA\Schema(type: 'integer'),
)]
#[OA\Tag(name: 'admin/user')]
#[IsGranted('ROLE_ADMIN')]
#[Security(name: 'oauth2', scopes: ['admin:user:ban'])]
#[IsGranted('ROLE_OAUTH2_ADMIN:USER:BAN')]
/** Unbans a user from the instance */
public function unban(
#[MapEntity(id: 'user_id')]
User $user,
UserUnban $userUnban,
UserFactory $factory,
RateLimiterFactory $apiModerateLimiter,
): JsonResponse {
$headers = $this->rateLimit($apiModerateLimiter);
$userUnban($user);
// Response needs to be an array to insert isBanned
$response = $this->serializeUser($factory->createDto($user))->jsonSerialize();
$response['isBanned'] = $user->isBanned;
return new JsonResponse(
$response,
headers: $headers
);
}
}