2017-11-19 22:30:00 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Users;
|
2017-11-19 22:30:00 +00:00
|
|
|
|
2017-12-16 17:31:18 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2017-12-16 17:31:18 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-09-13 18:55:39 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2017-12-16 17:31:18 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
|
|
|
use Pterodactyl\Services\Users\UserCreationService;
|
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
2017-11-19 22:30:00 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\UserTransformer;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2021-01-04 18:50:43 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest;
|
2018-01-20 01:58:57 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\DeleteUserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\UpdateUserRequest;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2017-12-16 17:31:18 +00:00
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
class UserController extends ApplicationApiController
|
2017-11-19 22:30:00 +00:00
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private UserRepositoryInterface $repository;
|
|
|
|
private UserCreationService $creationService;
|
|
|
|
private UserDeletionService $deletionService;
|
|
|
|
private UserUpdateService $updateService;
|
2017-11-19 22:30:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-12-16 17:31:18 +00:00
|
|
|
UserRepositoryInterface $repository,
|
|
|
|
UserCreationService $creationService,
|
|
|
|
UserDeletionService $deletionService,
|
|
|
|
UserUpdateService $updateService
|
2021-01-04 18:50:43 +00:00
|
|
|
) {
|
2018-01-27 18:38:56 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2021-01-04 18:50:43 +00:00
|
|
|
$this->repository = $repository;
|
2017-12-16 17:31:18 +00:00
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->deletionService = $deletionService;
|
|
|
|
$this->updateService = $updateService;
|
2017-11-19 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-12 04:49:46 +00:00
|
|
|
* Handle request to list all users on the panel. Returns a JSON-API representation
|
2017-12-16 17:31:18 +00:00
|
|
|
* of a collection of users including any defined relations passed in
|
|
|
|
* the request.
|
2017-11-19 22:30:00 +00:00
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2017-11-19 22:30:00 +00:00
|
|
|
*/
|
2018-01-12 04:49:46 +00:00
|
|
|
public function index(GetUsersRequest $request): array
|
2017-11-19 22:30:00 +00:00
|
|
|
{
|
2021-01-04 18:50:43 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
2021-03-23 21:57:29 +00:00
|
|
|
if ($perPage < 1 || $perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
2021-01-04 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 18:55:39 +00:00
|
|
|
$users = QueryBuilder::for(User::query())
|
|
|
|
->allowedFilters(['email', 'uuid', 'username', 'external_id'])
|
|
|
|
->allowedSorts(['id', 'uuid'])
|
2021-01-04 18:50:43 +00:00
|
|
|
->paginate($perPage);
|
2017-11-19 22:30:00 +00:00
|
|
|
|
2018-01-04 03:14:53 +00:00
|
|
|
return $this->fractal->collection($users)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(UserTransformer::class))
|
2018-01-04 03:14:53 +00:00
|
|
|
->toArray();
|
2017-12-16 17:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request to view a single user. Includes any relations that
|
|
|
|
* were defined in the request.
|
2021-03-05 17:03:12 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2017-12-16 17:31:18 +00:00
|
|
|
*/
|
2021-01-04 18:50:43 +00:00
|
|
|
public function view(GetUserRequest $request, User $user): array
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
2020-09-13 18:55:39 +00:00
|
|
|
return $this->fractal->item($user)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(UserTransformer::class))
|
2018-01-04 03:14:53 +00:00
|
|
|
->toArray();
|
2017-11-19 22:30:00 +00:00
|
|
|
}
|
2017-12-16 17:31:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing user on the system and return the response. Returns the
|
|
|
|
* updated user model response on success. Supports handling of token revocation
|
|
|
|
* errors when switching a user from an admin to a normal user.
|
|
|
|
*
|
|
|
|
* Revocation errors are returned under the 'revocation_errors' key in the response
|
|
|
|
* meta. If there are no errors this is an empty array.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2017-12-16 17:31:18 +00:00
|
|
|
*/
|
2020-06-26 04:42:21 +00:00
|
|
|
public function update(UpdateUserRequest $request, User $user): array
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
|
|
|
$this->updateService->setUserLevel(User::USER_LEVEL_ADMIN);
|
2020-06-26 04:42:21 +00:00
|
|
|
$user = $this->updateService->handle($user, $request->validated());
|
2017-12-16 17:31:18 +00:00
|
|
|
|
2021-01-04 18:50:43 +00:00
|
|
|
return $this->fractal->item($user)
|
|
|
|
->transformWith($this->getTransformer(UserTransformer::class))
|
|
|
|
->toArray();
|
2017-12-16 17:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new user on the system. Returns the created user and a HTTP/201
|
|
|
|
* header on successful creation.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2018-01-13 02:39:15 +00:00
|
|
|
public function store(StoreUserRequest $request): JsonResponse
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
2018-01-13 02:39:15 +00:00
|
|
|
$user = $this->creationService->handle($request->validated());
|
2017-12-16 17:31:18 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($user)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(UserTransformer::class))
|
2017-12-16 17:31:18 +00:00
|
|
|
->addMeta([
|
2018-01-27 18:38:56 +00:00
|
|
|
'resource' => route('api.application.users.view', [
|
|
|
|
'user' => $user->id,
|
|
|
|
]),
|
2017-12-16 17:31:18 +00:00
|
|
|
])
|
|
|
|
->respond(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request to delete a user from the Panel. Returns a HTTP/204 response
|
|
|
|
* on successful deletion.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteUserRequest $request, User $user): Response
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
2020-09-13 18:55:39 +00:00
|
|
|
$this->deletionService->handle($user);
|
2017-12-16 17:31:18 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2017-12-16 17:31:18 +00:00
|
|
|
}
|
2017-11-19 22:30:00 +00:00
|
|
|
}
|