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
|
|
|
|
|
|
|
use Spatie\Fractal\Fractal;
|
|
|
|
use Illuminate\Http\Request;
|
2017-12-16 17:31:18 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-11-19 22:30:00 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-12-16 17:31:18 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
|
|
|
use Pterodactyl\Services\Users\UserCreationService;
|
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
2017-12-17 20:57:05 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Admin\UserTransformer;
|
2017-11-19 22:30:00 +00:00
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2018-01-20 01:58:57 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest;
|
|
|
|
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;
|
2017-12-16 17:31:18 +00:00
|
|
|
|
2017-11-19 22:30:00 +00:00
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2017-12-16 17:31:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\UserCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\UserDeletionService
|
|
|
|
*/
|
|
|
|
private $deletionService;
|
|
|
|
|
2017-11-19 22:30:00 +00:00
|
|
|
/**
|
|
|
|
* @var \Spatie\Fractal\Fractal
|
|
|
|
*/
|
|
|
|
private $fractal;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
2017-12-16 17:31:18 +00:00
|
|
|
|
2017-11-19 22:30:00 +00:00
|
|
|
/**
|
2017-12-16 17:31:18 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserUpdateService
|
2017-11-19 22:30:00 +00:00
|
|
|
*/
|
2017-12-16 17:31:18 +00:00
|
|
|
private $updateService;
|
2017-11-19 22:30:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
|
|
|
*
|
|
|
|
* @param \Spatie\Fractal\Fractal $fractal
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
2017-12-16 17:31:18 +00:00
|
|
|
* @param \Pterodactyl\Services\Users\UserCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Users\UserDeletionService $deletionService
|
|
|
|
* @param \Pterodactyl\Services\Users\UserUpdateService $updateService
|
2017-11-19 22:30:00 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Fractal $fractal,
|
2017-12-16 17:31:18 +00:00
|
|
|
UserRepositoryInterface $repository,
|
|
|
|
UserCreationService $creationService,
|
|
|
|
UserDeletionService $deletionService,
|
|
|
|
UserUpdateService $updateService
|
2017-11-19 22:30:00 +00:00
|
|
|
) {
|
2017-12-16 17:31:18 +00:00
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->deletionService = $deletionService;
|
2017-11-19 22:30:00 +00:00
|
|
|
$this->fractal = $fractal;
|
|
|
|
$this->repository = $repository;
|
2017-12-16 17:31:18 +00:00
|
|
|
$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
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest $request
|
2017-11-19 22:30:00 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-12 04:49:46 +00:00
|
|
|
public function index(GetUsersRequest $request): array
|
2017-11-19 22:30:00 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
$users = $this->repository->paginated(100);
|
2017-11-19 22:30:00 +00:00
|
|
|
|
2018-01-04 03:14:53 +00:00
|
|
|
return $this->fractal->collection($users)
|
2018-01-12 04:49:46 +00:00
|
|
|
->transformWith((new UserTransformer)->setKey($request->key()))
|
2017-11-19 22:30:00 +00:00
|
|
|
->withResourceName('user')
|
2018-01-04 03:14:53 +00:00
|
|
|
->paginateWith(new IlluminatePaginatorAdapter($users))
|
|
|
|
->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.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest $request
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
2017-12-16 17:31:18 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-12 04:49:46 +00:00
|
|
|
public function view(GetUserRequest $request, User $user): array
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
2018-01-04 03:14:53 +00:00
|
|
|
return $this->fractal->item($user)
|
2018-01-12 04:49:46 +00:00
|
|
|
->transformWith((new UserTransformer)->setKey($request->key()))
|
2018-01-04 03:14:53 +00:00
|
|
|
->withResourceName('user')
|
|
|
|
->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.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Users\UpdateUserRequest $request
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
2017-12-16 17:31:18 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-01-13 02:39:15 +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);
|
2018-01-13 02:39:15 +00:00
|
|
|
$collection = $this->updateService->handle($user, $request->validated());
|
2017-12-16 17:31:18 +00:00
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
if (! empty($collection->get('exceptions'))) {
|
|
|
|
foreach ($collection->get('exceptions') as $node => $exception) {
|
|
|
|
/** @var \GuzzleHttp\Exception\RequestException $exception */
|
|
|
|
/** @var \GuzzleHttp\Psr7\Response|null $response */
|
|
|
|
$response = method_exists($exception, 'getResponse') ? $exception->getResponse() : null;
|
|
|
|
$message = trans('admin/server.exceptions.daemon_exception', [
|
|
|
|
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$errors[] = ['message' => $message, 'node' => $node];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 21:11:44 +00:00
|
|
|
$response = $this->fractal->item($collection->get('model'))
|
2018-01-13 02:39:15 +00:00
|
|
|
->transformWith((new UserTransformer)->setKey($request->key()))
|
2018-01-01 21:11:44 +00:00
|
|
|
->withResourceName('user');
|
|
|
|
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
$response->addMeta([
|
2017-12-16 17:31:18 +00:00
|
|
|
'revocation_errors' => $errors,
|
2018-01-01 21:11:44 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->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.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest $request
|
2017-12-16 17:31:18 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @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-13 02:39:15 +00:00
|
|
|
->transformWith((new UserTransformer)->setKey($request->key()))
|
2017-12-16 17:31:18 +00:00
|
|
|
->withResourceName('user')
|
|
|
|
->addMeta([
|
|
|
|
'link' => route('api.admin.user.view', ['user' => $user->id]),
|
|
|
|
])
|
|
|
|
->respond(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request to delete a user from the Panel. Returns a HTTP/204 response
|
|
|
|
* on successful deletion.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Users\DeleteUserRequest $request
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
2017-12-16 17:31:18 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2018-01-13 02:39:15 +00:00
|
|
|
public function delete(DeleteUserRequest $request, User $user): Response
|
2017-12-16 17:31:18 +00:00
|
|
|
{
|
|
|
|
$this->deletionService->handle($user);
|
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
2017-11-19 22:30:00 +00:00
|
|
|
}
|