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;
|
2019-12-28 12:07:42 -08:00
|
|
|
use Illuminate\Auth\AuthManager;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 18:48:35 -04:00
|
|
|
use Pterodactyl\Facades\Activity;
|
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
|
|
|
|
{
|
2018-06-11 22:56:57 -07:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\UserUpdateService
|
|
|
|
*/
|
|
|
|
private $updateService;
|
|
|
|
|
2019-12-28 12:07:42 -08:00
|
|
|
/**
|
2022-05-22 16:05:58 -04:00
|
|
|
* @var \Illuminate\Auth\AuthManager
|
2019-12-28 12:07:42 -08:00
|
|
|
*/
|
2022-05-22 16:05:58 -04:00
|
|
|
private $manager;
|
2019-12-28 12:07:42 -08:00
|
|
|
|
2018-06-11 22:56:57 -07:00
|
|
|
/**
|
|
|
|
* AccountController constructor.
|
|
|
|
*/
|
2022-05-22 16:05:58 -04:00
|
|
|
public function __construct(AuthManager $manager, UserUpdateService $updateService)
|
2018-06-11 22:56:57 -07:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->updateService = $updateService;
|
2022-05-22 16:05:58 -04:00
|
|
|
$this->manager = $manager;
|
2018-06-11 22:56:57 -07:00
|
|
|
}
|
|
|
|
|
2018-06-05 23:00:01 -07:00
|
|
|
public function index(Request $request): array
|
|
|
|
{
|
|
|
|
return $this->fractal->item($request->user())
|
|
|
|
->transformWith($this->getTransformer(AccountTransformer::class))
|
|
|
|
->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
|
|
|
*/
|
2020-06-25 22:12:09 -07:00
|
|
|
public function updateEmail(UpdateEmailRequest $request): JsonResponse
|
2018-06-11 22:56:57 -07:00
|
|
|
{
|
2022-05-29 18:48:35 -04:00
|
|
|
$original = $request->user()->email;
|
2018-06-17 16:53:24 -07:00
|
|
|
$this->updateService->handle($request->user(), $request->validated());
|
2018-06-11 22:56:57 -07:00
|
|
|
|
2022-07-03 14:29:01 -04:00
|
|
|
if ($original !== $request->input('email')) {
|
|
|
|
Activity::event('user:account.email-changed')
|
|
|
|
->property(['old' => $original, 'new' => $request->input('email')])
|
|
|
|
->log();
|
|
|
|
}
|
2022-05-29 18:48:35 -04:00
|
|
|
|
2020-06-25 22:12:09 -07:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
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
|
|
|
*/
|
2020-06-25 22:12:09 -07:00
|
|
|
public function updatePassword(UpdatePasswordRequest $request): JsonResponse
|
2018-06-17 16:53:24 -07:00
|
|
|
{
|
2021-08-15 17:37:12 -07:00
|
|
|
$user = $this->updateService->handle($request->user(), $request->validated());
|
|
|
|
|
2022-05-22 16:05:58 -04:00
|
|
|
$guard = $this->manager->guard();
|
2021-08-15 17:37:12 -07: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 16:05:58 -04:00
|
|
|
$guard->setUser($user);
|
2018-06-17 16:53:24 -07:00
|
|
|
|
2022-05-22 16:05:58 -04:00
|
|
|
// This method doesn't exist in the stateless Sanctum world.
|
|
|
|
if (method_exists($guard, 'logoutOtherDevices')) {
|
|
|
|
$guard->logoutOtherDevices($request->input('password'));
|
|
|
|
}
|
2019-12-28 12:07:42 -08:00
|
|
|
|
2022-05-29 18:48:35 -04:00
|
|
|
Activity::event('user:account.password-changed')->log();
|
|
|
|
|
2020-06-25 22:12:09 -07:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2018-06-11 22:56:57 -07:00
|
|
|
}
|
2018-06-05 23:00:01 -07:00
|
|
|
}
|