Path: blob/1.0-develop/app/Http/Controllers/Api/Client/AccountController.php
14052 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client;34use Illuminate\Http\Request;5use Illuminate\Http\Response;6use Illuminate\Auth\AuthManager;7use Illuminate\Http\JsonResponse;8use Pterodactyl\Facades\Activity;9use Illuminate\Support\Facades\RateLimiter;10use Pterodactyl\Services\Users\UserUpdateService;11use Pterodactyl\Transformers\Api\Client\AccountTransformer;12use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;13use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;14use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;1516class AccountController extends ClientApiController17{18/**19* The number of seconds that must elapse before the email change throttle resets.20*/21private const EMAIL_UPDATE_THROTTLE = 60 * 60 * 24;2223/**24* AccountController constructor.25*/26public function __construct(private AuthManager $manager, private UserUpdateService $updateService)27{28parent::__construct();29}3031public function index(Request $request): array32{33return $this->fractal->item($request->user())34->transformWith($this->getTransformer(AccountTransformer::class))35->toArray();36}3738/**39* Update the authenticated user's email address.40*/41public function updateEmail(UpdateEmailRequest $request): JsonResponse42{43$user = $request->user();44// Only allow a user to change their email three times in the span45// of 24 hours. This prevents malicious users from trying to find46// existing accounts in the system by constantly changing their email.47if (RateLimiter::tooManyAttempts($key = "user:update-email:{$user->uuid}", 3)) {48throw new TooManyRequestsHttpException(message: 'Your email address has been changed too many times today. Please try again later.');49}5051$original = $user->email;52if (mb_strtolower($original) !== mb_strtolower($request->validated('email'))) {53RateLimiter::hit($key, self::EMAIL_UPDATE_THROTTLE);5455$this->updateService->handle($user, $request->validated());5657Activity::event('user:account.email-changed')58->property(['old' => $original, 'new' => $request->validated('email')])59->log();60}6162return new JsonResponse([], Response::HTTP_NO_CONTENT);63}6465/**66* Update the authenticated user's password. All existing sessions will be logged67* out immediately.68*69* @throws \Throwable70*/71public function updatePassword(UpdatePasswordRequest $request): JsonResponse72{73$user = Activity::event('user:account.password-changed')->transaction(function () use ($request) {74return $this->updateService->handle($request->user(), $request->validated());75});7677$guard = $this->manager->guard();78// If you do not update the user in the session you'll end up working with a79// cached copy of the user that does not include the updated password. Do this80// to correctly store the new user details in the guard and allow the logout81// other devices functionality to work.82$guard->setUser($user);8384// This method doesn't exist in the stateless Sanctum world.85if (method_exists($guard, 'logoutOtherDevices')) { // @phpstan-ignore function.alreadyNarrowedType86$guard->logoutOtherDevices($request->input('password'));87}8889return new JsonResponse([], Response::HTTP_NO_CONTENT);90}91}929394