Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/AccountController.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Auth\AuthManager;
8
use Illuminate\Http\JsonResponse;
9
use Pterodactyl\Facades\Activity;
10
use Pterodactyl\Services\Users\UserUpdateService;
11
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
12
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
13
use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;
14
15
class AccountController extends ClientApiController
16
{
17
/**
18
* AccountController constructor.
19
*/
20
public function __construct(private AuthManager $manager, private UserUpdateService $updateService)
21
{
22
parent::__construct();
23
}
24
25
public function index(Request $request): array
26
{
27
return $this->fractal->item($request->user())
28
->transformWith($this->getTransformer(AccountTransformer::class))
29
->toArray();
30
}
31
32
/**
33
* Update the authenticated user's email address.
34
*/
35
public function updateEmail(UpdateEmailRequest $request): JsonResponse
36
{
37
$original = $request->user()->email;
38
$this->updateService->handle($request->user(), $request->validated());
39
40
if ($original !== $request->input('email')) {
41
Activity::event('user:account.email-changed')
42
->property(['old' => $original, 'new' => $request->input('email')])
43
->log();
44
}
45
46
return new JsonResponse([], Response::HTTP_NO_CONTENT);
47
}
48
49
/**
50
* Update the authenticated user's password. All existing sessions will be logged
51
* out immediately.
52
*
53
* @throws \Throwable
54
*/
55
public function updatePassword(UpdatePasswordRequest $request): JsonResponse
56
{
57
$user = $this->updateService->handle($request->user(), $request->validated());
58
59
$guard = $this->manager->guard();
60
// If you do not update the user in the session you'll end up working with a
61
// cached copy of the user that does not include the updated password. Do this
62
// to correctly store the new user details in the guard and allow the logout
63
// other devices functionality to work.
64
$guard->setUser($user);
65
66
// This method doesn't exist in the stateless Sanctum world.
67
if (method_exists($guard, 'logoutOtherDevices')) { // @phpstan-ignore function.alreadyNarrowedType
68
$guard->logoutOtherDevices($request->input('password'));
69
}
70
71
Activity::event('user:account.password-changed')->log();
72
73
return new JsonResponse([], Response::HTTP_NO_CONTENT);
74
}
75
}
76
77