Files
pterodactyl-panel/app/Http/Controllers/Api/Client/AccountController.php
T

108 lines
4.5 KiB
PHP
Raw Normal View History

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;
use Pterodactyl\Contracts\Users\UpdatesUserEmails;
use Pterodactyl\Contracts\Users\UpdatesUserPasswords;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Fractal;
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-05 18:10:18 -04:00
use Pterodactyl\Models\User;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
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-10-05 18:10:18 -04:00
* @return ApiPayload
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Get account', 'Returns profile details for the authenticated user.')]
#[ResponseFromTransformer(AccountTransformer::class, User::class, resourceKey: 'user')]
2018-06-05 23:00:01 -07:00
public function index(Request $request): array
{
2026-10-05 18:10:18 -04:00
return Fractal::item($request->user())
2018-06-05 23:00:01 -07:00
->transformWith($this->getTransformer(AccountTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
2018-06-05 23:00:01 -07:00
}
/**
2018-06-17 16:53:24 -07:00
* Update the authenticated user's email address.
*/
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
{
2026-10-05 18:10:18 -04:00
$email = JsonValueGuard::string($request->validated('email'));
2026-10-05 18:10:18 -04:00
$result = $emails->update($request->user(), $email);
2026-10-05 18:10:18 -04:00
if ($result['changed']) {
Activity::event('user:account.email-changed')
2026-10-05 18:10:18 -04:00
->property(['old' => $result['original'], 'new' => $result['email']])
->log();
}
2022-05-29 18:48:35 -04:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2018-06-17 16:53:24 -07: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-05 18:10:18 -04:00
$password = JsonValueGuard::string($request->validated('password'));
2026-10-05 18:10:18 -04:00
$user = $passwords->update($request->user(), $password);
2018-06-17 16:53:24 -07:00
2026-10-05 18:10:18 -04:00
Activity::event('user:account.password-changed')->subject($user)->log();
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
2018-06-05 23:00:01 -07:00
}