misc_pterodactyl-panel/app/Http/Controllers/Api/Client/AccountController.php

78 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client;
use Illuminate\Http\Request;
2018-06-17 23:53:24 +00:00
use Illuminate\Http\Response;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Services\Users\UserUpdateService;
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
2018-06-17 23:53:24 +00:00
use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;
class AccountController extends ClientApiController
{
/**
* @var \Pterodactyl\Services\Users\UserUpdateService
*/
private $updateService;
/**
* @var \Illuminate\Auth\SessionGuard
*/
private $sessionGuard;
/**
* AccountController constructor.
*/
public function __construct(AuthManager $sessionGuard, UserUpdateService $updateService)
{
parent::__construct();
$this->updateService = $updateService;
$this->sessionGuard = $sessionGuard;
}
public function index(Request $request): array
{
return $this->fractal->item($request->user())
->transformWith($this->getTransformer(AccountTransformer::class))
->toArray();
}
/**
2018-06-17 23:53:24 +00:00
* Update the authenticated user's email address.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function updateEmail(UpdateEmailRequest $request): JsonResponse
{
2018-06-17 23:53:24 +00:00
$this->updateService->handle($request->user(), $request->validated());
return new JsonResponse([], Response::HTTP_NO_CONTENT);
2018-06-17 23:53:24 +00:00
}
/**
* Update the authenticated user's password. All existing sessions will be logged
* out immediately.
2018-06-17 23:53:24 +00:00
*
* @throws \Throwable
2018-06-17 23:53:24 +00:00
*/
public function updatePassword(UpdatePasswordRequest $request): JsonResponse
2018-06-17 23:53:24 +00:00
{
$user = $this->updateService->handle($request->user(), $request->validated());
// If you do not update the user in the session you'll end up working with a
// cached copy of the user that does not include the updated password. Do this
// to correctly store the new user details in the guard and allow the logout
// other devices functionality to work.
$this->sessionGuard->setUser($user);
2018-06-17 23:53:24 +00:00
2020-04-08 06:59:38 +00:00
$this->sessionGuard->logoutOtherDevices($request->input('password'));
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
}