Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Users/UserController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Application\Users;
4
5
use Pterodactyl\Models\User;
6
use Illuminate\Http\JsonResponse;
7
use Spatie\QueryBuilder\QueryBuilder;
8
use Pterodactyl\Services\Users\UserUpdateService;
9
use Pterodactyl\Services\Users\UserCreationService;
10
use Pterodactyl\Services\Users\UserDeletionService;
11
use Pterodactyl\Transformers\Api\Application\UserTransformer;
12
use Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest;
13
use Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest;
14
use Pterodactyl\Http\Requests\Api\Application\Users\DeleteUserRequest;
15
use Pterodactyl\Http\Requests\Api\Application\Users\UpdateUserRequest;
16
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
17
18
class UserController extends ApplicationApiController
19
{
20
/**
21
* UserController constructor.
22
*/
23
public function __construct(
24
private UserCreationService $creationService,
25
private UserDeletionService $deletionService,
26
private UserUpdateService $updateService,
27
) {
28
parent::__construct();
29
}
30
31
/**
32
* Handle request to list all users on the panel. Returns a JSON-API representation
33
* of a collection of users including any defined relations passed in
34
* the request.
35
*/
36
public function index(GetUsersRequest $request): array
37
{
38
$users = QueryBuilder::for(User::query())
39
->allowedFilters(['email', 'uuid', 'username', 'external_id'])
40
->allowedSorts(['id', 'uuid'])
41
->paginate($request->query('per_page') ?? 50);
42
43
return $this->fractal->collection($users)
44
->transformWith($this->getTransformer(UserTransformer::class))
45
->toArray();
46
}
47
48
/**
49
* Handle a request to view a single user. Includes any relations that
50
* were defined in the request.
51
*/
52
public function view(GetUsersRequest $request, User $user): array
53
{
54
return $this->fractal->item($user)
55
->transformWith($this->getTransformer(UserTransformer::class))
56
->toArray();
57
}
58
59
/**
60
* Update an existing user on the system and return the response. Returns the
61
* updated user model response on success. Supports handling of token revocation
62
* errors when switching a user from an admin to a normal user.
63
*
64
* Revocation errors are returned under the 'revocation_errors' key in the response
65
* meta. If there are no errors this is an empty array.
66
*
67
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
68
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
69
*/
70
public function update(UpdateUserRequest $request, User $user): array
71
{
72
$this->updateService->setUserLevel(User::USER_LEVEL_ADMIN);
73
$user = $this->updateService->handle($user, $request->validated());
74
75
$response = $this->fractal->item($user)
76
->transformWith($this->getTransformer(UserTransformer::class));
77
78
return $response->toArray();
79
}
80
81
/**
82
* Store a new user on the system. Returns the created user and an HTTP/201
83
* header on successful creation.
84
*
85
* @throws \Exception
86
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
87
*/
88
public function store(StoreUserRequest $request): JsonResponse
89
{
90
$user = $this->creationService->handle($request->validated());
91
92
return $this->fractal->item($user)
93
->transformWith($this->getTransformer(UserTransformer::class))
94
->addMeta([
95
'resource' => route('api.application.users.view', [
96
'user' => $user->id,
97
]),
98
])
99
->respond(201);
100
}
101
102
/**
103
* Handle a request to delete a user from the Panel. Returns a HTTP/204 response
104
* on successful deletion.
105
*
106
* @throws \Pterodactyl\Exceptions\DisplayException
107
*/
108
public function delete(DeleteUserRequest $request, User $user): JsonResponse
109
{
110
$this->deletionService->handle($user);
111
112
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
113
}
114
}
115
116