2018-06-05 23:00:01 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-17 16:53:24 -07:00
|
|
|
use Illuminate\Http\Response;
|
2021-08-21 13:35:55 -06:00
|
|
|
use Illuminate\Auth\SessionGuard;
|
2018-06-11 22:56:57 -07:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
2018-06-05 23:00:01 -07:00
|
|
|
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
|
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;
|
2018-06-05 23:00:01 -07:00
|
|
|
|
|
|
|
class AccountController extends ClientApiController
|
|
|
|
{
|
2021-08-21 13:35:55 -06:00
|
|
|
private SessionGuard $sessionGuard;
|
2021-03-05 10:03:12 -07:00
|
|
|
private UserUpdateService $updateService;
|
2019-12-28 12:07:42 -08:00
|
|
|
|
2018-06-11 22:56:57 -07:00
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
2021-08-21 13:35:55 -06:00
|
|
|
public function __construct(SessionGuard $sessionGuard, UserUpdateService $updateService)
|
2018-06-11 22:56:57 -07:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2021-08-21 13:35:55 -06:00
|
|
|
$this->sessionGuard = $sessionGuard;
|
2021-03-05 10:03:12 -07:00
|
|
|
$this->updateService = $updateService;
|
2018-06-11 22:56:57 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:03:12 -07:00
|
|
|
/**
|
2021-08-21 13:35:55 -06:00
|
|
|
* Gets information about the currently authenticated user.
|
2021-03-05 10:03:12 -07:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
|
*/
|
2018-06-05 23:00:01 -07:00
|
|
|
public function index(Request $request): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($request->user())
|
2021-08-07 13:06:45 -07:00
|
|
|
->transformWith(AccountTransformer::class)
|
2018-06-05 23:00:01 -07:00
|
|
|
->toArray();
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2021-03-06 15:49:44 -07:00
|
|
|
public function updateEmail(UpdateEmailRequest $request): Response
|
2018-06-11 22:56:57 -07:00
|
|
|
{
|
2018-06-17 16:53:24 -07:00
|
|
|
$this->updateService->handle($request->user(), $request->validated());
|
2018-06-11 22:56:57 -07:00
|
|
|
|
2021-03-05 10:03:12 -07:00
|
|
|
return $this->returnNoContent();
|
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
|
|
|
*
|
2021-08-15 17:37:12 -07:00
|
|
|
* @throws \Throwable
|
2018-06-17 16:53:24 -07:00
|
|
|
*/
|
2021-03-06 15:49:44 -07:00
|
|
|
public function updatePassword(UpdatePasswordRequest $request): Response
|
2018-06-17 16:53:24 -07:00
|
|
|
{
|
2021-08-15 17:37:12 -07: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 16:53:24 -07:00
|
|
|
|
2021-08-21 13:35:55 -06:00
|
|
|
$this->sessionGuard->logoutOtherDevices($request->input('password'));
|
2019-12-28 12:07:42 -08:00
|
|
|
|
2021-03-05 10:03:12 -07:00
|
|
|
return $this->returnNoContent();
|
2018-06-11 22:56:57 -07:00
|
|
|
}
|
2018-06-05 23:00:01 -07:00
|
|
|
}
|