2018-06-05 23:00:01 -07:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-06-05 23:00:01 -07:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-06-05 23:00:01 -07:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-17 16:53:24 -07:00
|
|
|
use Illuminate\Http\Response;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Knuckles\Scribe\Attributes\BodyParam;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Endpoint;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Group;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
|
|
|
|
|
use Knuckles\Scribe\Attributes\Subgroup;
|
2026-10-20 15:55:48 -04:00
|
|
|
use Pterodactyl\Contracts\Users\ReplacesUserAvatars;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Contracts\Users\UpdatesUserEmails;
|
|
|
|
|
use Pterodactyl\Contracts\Users\UpdatesUserPasswords;
|
2026-10-11 21:01:23 -04:00
|
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource;
|
2026-11-05 15:10:39 -05:00
|
|
|
use Pterodactyl\Facades\Activity;
|
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\DeleteAvatarRequest;
|
2026-10-20 15:46:16 -04:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateAvatarRequest;
|
2018-06-11 22:56:57 -07:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
|
2018-06-17 16:53:24 -07:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;
|
2026-10-11 21:01:23 -04:00
|
|
|
use Pterodactyl\Http\Resources\Api\Client\AccountResource;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use Throwable;
|
2018-06-05 23:00:01 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
|
|
|
|
|
#[Subgroup('Account', 'View and update the authenticated user account.')]
|
2018-06-05 23:00:01 -07:00
|
|
|
class AccountController extends ClientApiController
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
private const array INVALID_PASSWORD_ERROR = [
|
|
|
|
|
'errors' => [
|
|
|
|
|
[
|
|
|
|
|
'code' => 'InvalidPasswordProvidedException',
|
|
|
|
|
'status' => '400',
|
|
|
|
|
'detail' => 'The password provided was invalid for this account.',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private const array THROTTLED_ERROR = [
|
|
|
|
|
'errors' => [
|
|
|
|
|
[
|
|
|
|
|
'code' => 'TooManyRequestsHttpException',
|
|
|
|
|
'status' => '429',
|
|
|
|
|
'detail' => 'Your email address has been changed too many times today. Please try again later.',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
2026-04-01 17:54:30 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Endpoint('Get account', 'Returns profile details for the authenticated user.')]
|
2026-10-11 21:01:23 -04:00
|
|
|
#[ResponseFromResource(AccountResource::class, User::class, resourceKey: 'user')]
|
|
|
|
|
public function index(Request $request): AccountResource
|
2018-06-05 23:00:01 -07:00
|
|
|
{
|
2026-10-11 21:01:23 -04:00
|
|
|
return AccountResource::make($request->user());
|
2018-06-05 23:00:01 -07:00
|
|
|
}
|
2018-06-11 22:56:57 -07:00
|
|
|
|
|
|
|
|
/**
|
2018-06-17 16:53:24 -07:00
|
|
|
* Update the authenticated user's email address.
|
2018-06-11 22:56:57 -07:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Endpoint('Update account email', "Updates the authenticated user's email address after verifying their current password.")]
|
|
|
|
|
#[BodyParam('password', 'string', 'The current account password.', required: true, example: 'current-password')]
|
|
|
|
|
#[ScribeResponse(status: 204, description: 'Email address updated.')]
|
|
|
|
|
#[ScribeResponse(self::INVALID_PASSWORD_ERROR, status: 400, description: 'The current password is invalid.')]
|
|
|
|
|
#[ScribeResponse(self::THROTTLED_ERROR, status: 429, description: 'The account has changed email too many times in the current throttle window.')]
|
|
|
|
|
public function updateEmail(UpdateEmailRequest $request, UpdatesUserEmails $emails): JsonResponse
|
2018-06-11 22:56:57 -07:00
|
|
|
{
|
2026-10-11 16:17:55 -04:00
|
|
|
$emails->update($request->user(), $request->string('email')->toString());
|
2022-05-29 18:48:35 -04:00
|
|
|
|
2020-06-25 22:12:09 -07:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2018-06-17 16:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-12-28 12:07:42 -08:00
|
|
|
* Update the authenticated user's password. All existing sessions will be logged
|
|
|
|
|
* out immediately.
|
2018-06-17 16:53:24 -07:00
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Throwable
|
2018-06-17 16:53:24 -07:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
#[Endpoint('Update account password', "Updates the authenticated user's password and revokes other active sessions where supported.")]
|
|
|
|
|
#[BodyParam('current_password', 'string', 'The current account password.', required: true, example: 'current-password')]
|
|
|
|
|
#[BodyParam('password_confirmation', 'string', 'Confirmation matching the new password.', required: true, example: 'correct-horse-battery-staple')]
|
|
|
|
|
#[ScribeResponse(status: 204, description: 'Password updated.')]
|
|
|
|
|
#[ScribeResponse(self::INVALID_PASSWORD_ERROR, status: 400, description: 'The current password is invalid.')]
|
|
|
|
|
public function updatePassword(UpdatePasswordRequest $request, UpdatesUserPasswords $passwords): JsonResponse
|
2018-06-17 16:53:24 -07:00
|
|
|
{
|
2026-10-11 16:17:55 -04:00
|
|
|
$passwords->update($request->user(), $request->string('password')->toString());
|
2019-12-28 12:07:42 -08:00
|
|
|
|
2020-06-25 22:12:09 -07:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2018-06-11 22:56:57 -07:00
|
|
|
}
|
2026-10-20 21:09:33 +02:00
|
|
|
|
2026-10-20 15:46:16 -04:00
|
|
|
#[Endpoint('Update account avatar', "Stores the uploaded image as the authenticated user's avatar and deletes the one it replaces.")]
|
|
|
|
|
#[BodyParam('avatar', 'file', 'PNG, JPEG, SVG, WebP, AVIF or ICO image of at most 10 MiB.', required: true)]
|
|
|
|
|
#[ResponseFromResource(AccountResource::class, User::class, description: 'Avatar updated.', resourceKey: 'user')]
|
2026-10-20 15:55:48 -04:00
|
|
|
public function updateAvatar(UpdateAvatarRequest $request, ReplacesUserAvatars $avatars): AccountResource
|
2026-10-20 21:09:33 +02:00
|
|
|
{
|
2026-11-05 15:10:39 -05:00
|
|
|
$user = $avatars->replace($request->user(), $request->avatar());
|
|
|
|
|
|
|
|
|
|
Activity::event('user:account.avatar-changed')->subject($user)->log();
|
|
|
|
|
|
|
|
|
|
return AccountResource::make($user);
|
2026-10-20 21:09:33 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-20 15:46:16 -04:00
|
|
|
#[Endpoint('Delete account avatar', "Deletes the authenticated user's avatar.")]
|
|
|
|
|
#[ScribeResponse(status: 204, description: 'Avatar deleted.')]
|
2026-11-05 15:10:39 -05:00
|
|
|
public function deleteAvatar(DeleteAvatarRequest $request, ReplacesUserAvatars $avatars): JsonResponse
|
2026-10-20 21:09:33 +02:00
|
|
|
{
|
2026-11-05 15:10:39 -05:00
|
|
|
$user = $avatars->replace($request->user(), null);
|
|
|
|
|
|
|
|
|
|
Activity::event('user:account.avatar-deleted')->subject($user)->log();
|
2026-10-20 21:09:33 +02:00
|
|
|
|
|
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
|
|
|
|
}
|
2018-06-05 23:00:01 -07:00
|
|
|
}
|