Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Users/UserController.php
10280 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Application\Users;34use Pterodactyl\Models\User;5use Illuminate\Http\JsonResponse;6use Spatie\QueryBuilder\QueryBuilder;7use Pterodactyl\Services\Users\UserUpdateService;8use Pterodactyl\Services\Users\UserCreationService;9use Pterodactyl\Services\Users\UserDeletionService;10use Pterodactyl\Transformers\Api\Application\UserTransformer;11use Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest;12use Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest;13use Pterodactyl\Http\Requests\Api\Application\Users\DeleteUserRequest;14use Pterodactyl\Http\Requests\Api\Application\Users\UpdateUserRequest;15use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;1617class UserController extends ApplicationApiController18{19/**20* UserController constructor.21*/22public function __construct(23private UserCreationService $creationService,24private UserDeletionService $deletionService,25private UserUpdateService $updateService,26) {27parent::__construct();28}2930/**31* Handle request to list all users on the panel. Returns a JSON-API representation32* of a collection of users including any defined relations passed in33* the request.34*/35public function index(GetUsersRequest $request): array36{37$users = QueryBuilder::for(User::query())38->allowedFilters(['email', 'uuid', 'username', 'external_id'])39->allowedSorts(['id', 'uuid'])40->paginate($request->query('per_page') ?? 50);4142return $this->fractal->collection($users)43->transformWith($this->getTransformer(UserTransformer::class))44->toArray();45}4647/**48* Handle a request to view a single user. Includes any relations that49* were defined in the request.50*/51public function view(GetUsersRequest $request, User $user): array52{53return $this->fractal->item($user)54->transformWith($this->getTransformer(UserTransformer::class))55->toArray();56}5758/**59* Update an existing user on the system and return the response. Returns the60* updated user model response on success. Supports handling of token revocation61* errors when switching a user from an admin to a normal user.62*63* Revocation errors are returned under the 'revocation_errors' key in the response64* meta. If there are no errors this is an empty array.65*66* @throws \Pterodactyl\Exceptions\Model\DataValidationException67* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException68*/69public function update(UpdateUserRequest $request, User $user): array70{71$this->updateService->setUserLevel(User::USER_LEVEL_ADMIN);72$user = $this->updateService->handle($user, $request->validated());7374$response = $this->fractal->item($user)75->transformWith($this->getTransformer(UserTransformer::class));7677return $response->toArray();78}7980/**81* Store a new user on the system. Returns the created user and an HTTP/20182* header on successful creation.83*84* @throws \Exception85* @throws \Pterodactyl\Exceptions\Model\DataValidationException86*/87public function store(StoreUserRequest $request): JsonResponse88{89$user = $this->creationService->handle($request->validated());9091return $this->fractal->item($user)92->transformWith($this->getTransformer(UserTransformer::class))93->addMeta([94'resource' => route('api.application.users.view', [95'user' => $user->id,96]),97])98->respond(201);99}100101/**102* Handle a request to delete a user from the Panel. Returns a HTTP/204 response103* on successful deletion.104*105* @throws \Pterodactyl\Exceptions\DisplayException106*/107public function delete(DeleteUserRequest $request, User $user): JsonResponse108{109$this->deletionService->handle($user);110111return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);112}113}114115116