2019-11-03 20:20:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Services\Subusers\SubuserCreationService;
|
2019-11-03 20:20:11 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\SubuserTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\StoreSubuserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest;
|
2019-11-03 20:20:11 +00:00
|
|
|
|
|
|
|
class SubuserController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\SubuserRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Subusers\SubuserCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
2019-11-03 20:20:11 +00:00
|
|
|
/**
|
|
|
|
* SubuserController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\SubuserRepository $repository
|
2020-03-27 21:23:13 +00:00
|
|
|
* @param \Pterodactyl\Services\Subusers\SubuserCreationService $creationService
|
2019-11-03 20:20:11 +00:00
|
|
|
*/
|
2020-03-27 21:23:13 +00:00
|
|
|
public function __construct(
|
|
|
|
SubuserRepository $repository,
|
|
|
|
SubuserCreationService $creationService
|
|
|
|
) {
|
2019-11-03 20:20:11 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
2020-03-27 21:23:13 +00:00
|
|
|
$this->creationService = $creationService;
|
2019-11-03 20:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the users associated with this server instance.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(GetSubuserRequest $request, Server $server)
|
|
|
|
{
|
2019-12-28 20:03:19 +00:00
|
|
|
return $this->fractal->collection($server->subusers)
|
2019-11-03 20:20:11 +00:00
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2020-03-27 21:23:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new subuser for the given server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\StoreSubuserRequest $request
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function store(StoreSubuserRequest $request, Server $server)
|
|
|
|
{
|
|
|
|
$response = $this->creationService->handle(
|
|
|
|
$server, $request->input('email'), $this->getDefaultPermissions($request)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->fractal->item($response)
|
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a given subuser in the system for the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\UpdateSubuserRequest $request
|
2020-03-27 22:32:33 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-03-27 21:23:13 +00:00
|
|
|
* @return array
|
2020-03-27 22:32:33 +00:00
|
|
|
*
|
2020-03-27 21:23:13 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2020-03-27 22:32:33 +00:00
|
|
|
public function update(UpdateSubuserRequest $request, Server $server): array
|
2020-03-27 21:23:13 +00:00
|
|
|
{
|
|
|
|
$subuser = $request->subuser();
|
|
|
|
$this->repository->update($subuser->id, [
|
|
|
|
'permissions' => $this->getDefaultPermissions($request),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->fractal->item($subuser->refresh())
|
|
|
|
->transformWith($this->getTransformer(SubuserTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a subusers from a server's assignment.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\DeleteSubuserRequest $request
|
2020-03-27 22:32:33 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2020-03-27 21:23:13 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2020-03-27 22:32:33 +00:00
|
|
|
public function delete(DeleteSubuserRequest $request, Server $server)
|
2020-03-27 21:23:13 +00:00
|
|
|
{
|
|
|
|
$this->repository->delete($request->subuser()->id);
|
|
|
|
|
|
|
|
return JsonResponse::create([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default permissions for all subusers to ensure none are ever removed wrongly.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getDefaultPermissions(Request $request): array
|
|
|
|
{
|
2020-03-27 23:42:27 +00:00
|
|
|
return array_unique(array_merge($request->input('permissions') ?? [], ['websocket.*']));
|
2020-03-27 21:23:13 +00:00
|
|
|
}
|
2019-11-03 20:20:11 +00:00
|
|
|
}
|