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

74 lines
2.2 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;
2021-10-06 20:45:44 +00:00
use Illuminate\Auth\AuthManager;
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
{
2021-10-06 20:45:44 +00:00
/**
* @var \Illuminate\Auth\SessionGuard
*/
private $authManager;
2021-03-05 17:03:12 +00:00
private UserUpdateService $updateService;
/**
* AccountController constructor.
*/
2021-10-06 20:45:44 +00:00
public function __construct(AuthManager $authManager, UserUpdateService $updateService)
{
parent::__construct();
2021-10-06 20:45:44 +00:00
$this->authManager = $authManager;
2021-03-05 17:03:12 +00:00
$this->updateService = $updateService;
}
2021-03-05 17:03:12 +00:00
/**
2021-08-21 19:35:55 +00:00
* Gets information about the currently authenticated user.
2021-03-05 17:03:12 +00:00
*/
public function index(Request $request): array
{
return $this->fractal->item($request->user())
->transformWith(AccountTransformer::class)
->toArray();
}
/**
2018-06-17 23:53:24 +00:00
* Update the authenticated user's email address.
*/
2021-03-06 22:49:44 +00:00
public function updateEmail(UpdateEmailRequest $request): Response
{
2018-06-17 23:53:24 +00:00
$this->updateService->handle($request->user(), $request->validated());
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
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
*/
2021-03-06 22:49:44 +00:00
public function updatePassword(UpdatePasswordRequest $request): Response
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.
2021-10-06 20:45:44 +00:00
$this->authManager->setUser($user);
2018-06-17 23:53:24 +00:00
2021-10-06 20:45:44 +00:00
$this->authManager->logoutOtherDevices($request->input('password'));
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
}