2018-06-06 06:00:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-17 23:53:24 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-12-28 20:07:42 +00:00
|
|
|
use Illuminate\Auth\AuthManager;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 22:48:35 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2018-06-12 05:56:57 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
2018-06-06 06:00:01 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
|
2018-06-12 05:56:57 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
|
2018-06-17 23:53:24 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;
|
2018-06-06 06:00:01 +00:00
|
|
|
|
|
|
|
class AccountController extends ClientApiController
|
|
|
|
{
|
2018-06-12 05:56:57 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\UserUpdateService
|
|
|
|
*/
|
|
|
|
private $updateService;
|
|
|
|
|
2019-12-28 20:07:42 +00:00
|
|
|
/**
|
2022-05-22 20:05:58 +00:00
|
|
|
* @var \Illuminate\Auth\AuthManager
|
2019-12-28 20:07:42 +00:00
|
|
|
*/
|
2022-05-22 20:05:58 +00:00
|
|
|
private $manager;
|
2019-12-28 20:07:42 +00:00
|
|
|
|
2018-06-12 05:56:57 +00:00
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
2022-05-22 20:05:58 +00:00
|
|
|
public function __construct(AuthManager $manager, UserUpdateService $updateService)
|
2018-06-12 05:56:57 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->updateService = $updateService;
|
2022-05-22 20:05:58 +00:00
|
|
|
$this->manager = $manager;
|
2018-06-12 05:56:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 06:00:01 +00:00
|
|
|
public function index(Request $request): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($request->user())
|
|
|
|
->transformWith($this->getTransformer(AccountTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-06-12 05:56:57 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-17 23:53:24 +00:00
|
|
|
* Update the authenticated user's email address.
|
2018-06-12 05:56:57 +00:00
|
|
|
*/
|
2020-06-26 05:12:09 +00:00
|
|
|
public function updateEmail(UpdateEmailRequest $request): JsonResponse
|
2018-06-12 05:56:57 +00:00
|
|
|
{
|
2022-05-29 22:48:35 +00:00
|
|
|
$original = $request->user()->email;
|
2018-06-17 23:53:24 +00:00
|
|
|
$this->updateService->handle($request->user(), $request->validated());
|
2018-06-12 05:56:57 +00:00
|
|
|
|
2022-07-03 18:29:01 +00:00
|
|
|
if ($original !== $request->input('email')) {
|
|
|
|
Activity::event('user:account.email-changed')
|
|
|
|
->property(['old' => $original, 'new' => $request->input('email')])
|
|
|
|
->log();
|
|
|
|
}
|
2022-05-29 22:48:35 +00:00
|
|
|
|
2020-06-26 05:12:09 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2018-06-17 23:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-28 20:07:42 +00:00
|
|
|
* Update the authenticated user's password. All existing sessions will be logged
|
|
|
|
* out immediately.
|
2018-06-17 23:53:24 +00:00
|
|
|
*
|
2021-08-16 00:37:12 +00:00
|
|
|
* @throws \Throwable
|
2018-06-17 23:53:24 +00:00
|
|
|
*/
|
2020-06-26 05:12:09 +00:00
|
|
|
public function updatePassword(UpdatePasswordRequest $request): JsonResponse
|
2018-06-17 23:53:24 +00:00
|
|
|
{
|
2021-08-16 00:37:12 +00:00
|
|
|
$user = $this->updateService->handle($request->user(), $request->validated());
|
|
|
|
|
2022-05-22 20:05:58 +00:00
|
|
|
$guard = $this->manager->guard();
|
2021-08-16 00:37:12 +00:00
|
|
|
// 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.
|
2022-05-22 20:05:58 +00:00
|
|
|
$guard->setUser($user);
|
2018-06-17 23:53:24 +00:00
|
|
|
|
2022-05-22 20:05:58 +00:00
|
|
|
// This method doesn't exist in the stateless Sanctum world.
|
|
|
|
if (method_exists($guard, 'logoutOtherDevices')) {
|
|
|
|
$guard->logoutOtherDevices($request->input('password'));
|
|
|
|
}
|
2019-12-28 20:07:42 +00:00
|
|
|
|
2022-05-29 22:48:35 +00:00
|
|
|
Activity::event('user:account.password-changed')->log();
|
|
|
|
|
2020-06-26 05:12:09 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2018-06-12 05:56:57 +00:00
|
|
|
}
|
2018-06-06 06:00:01 +00:00
|
|
|
}
|