2018-02-25 21:30:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
2020-07-07 04:25:00 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-11-04 01:37:06 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-07-07 04:25:00 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2020-10-16 04:21:38 +00:00
|
|
|
use Spatie\QueryBuilder\AllowedFilter;
|
|
|
|
use Pterodactyl\Models\Filters\MultiFieldServerFilter;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\ServerTransformer;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\GetServersRequest;
|
2018-02-25 21:30:56 +00:00
|
|
|
|
|
|
|
class ClientController extends ClientApiController
|
|
|
|
{
|
2018-02-28 03:28:43 +00:00
|
|
|
/**
|
2019-09-06 04:41:20 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
2018-02-28 03:28:43 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClientController constructor.
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function __construct(ServerRepository $repository)
|
2018-02-28 03:28:43 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the servers available to the client making the API
|
|
|
|
* request, including servers the user has access to as a subuser.
|
|
|
|
*/
|
|
|
|
public function index(GetServersRequest $request): array
|
|
|
|
{
|
2020-07-07 04:25:00 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$transformer = $this->getTransformer(ServerTransformer::class);
|
|
|
|
|
|
|
|
// Start the query builder and ensure we eager load any requested relationships from the request.
|
2020-07-26 17:43:46 +00:00
|
|
|
$builder = QueryBuilder::for(
|
|
|
|
Server::query()->with($this->getIncludesForTransformer($transformer, ['node']))
|
2020-10-16 04:21:38 +00:00
|
|
|
)->allowedFilters([
|
|
|
|
'uuid',
|
|
|
|
'name',
|
|
|
|
'external_id',
|
2021-01-23 20:33:34 +00:00
|
|
|
AllowedFilter::custom('*', new MultiFieldServerFilter()),
|
2020-10-16 04:21:38 +00:00
|
|
|
]);
|
2020-07-07 04:25:00 +00:00
|
|
|
|
2020-10-16 04:21:38 +00:00
|
|
|
$type = $request->input('type');
|
2020-07-26 17:43:46 +00:00
|
|
|
// Either return all of the servers the user has access to because they are an admin `?type=admin` or
|
|
|
|
// just return all of the servers the user has access to because they are the owner or a subuser of the
|
2020-10-16 04:21:38 +00:00
|
|
|
// server. If ?type=admin-all is passed all servers on the system will be returned to the user, rather
|
|
|
|
// than only servers they can see because they are an admin.
|
|
|
|
if (in_array($type, ['admin', 'admin-all'])) {
|
|
|
|
// If they aren't an admin but want all the admin servers don't fail the request, just
|
|
|
|
// make it a query that will never return any results back.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$user->root_admin) {
|
2020-10-16 04:21:38 +00:00
|
|
|
$builder->whereRaw('1 = 2');
|
|
|
|
} else {
|
|
|
|
$builder = $type === 'admin-all'
|
|
|
|
? $builder
|
|
|
|
: $builder->whereNotIn('servers.id', $user->accessibleServers()->pluck('id')->all());
|
|
|
|
}
|
2021-01-23 20:09:16 +00:00
|
|
|
} elseif ($type === 'owner') {
|
2020-10-16 04:21:38 +00:00
|
|
|
$builder = $builder->where('servers.owner_id', $user->id);
|
2020-07-26 17:43:46 +00:00
|
|
|
} else {
|
2020-10-16 04:21:38 +00:00
|
|
|
$builder = $builder->whereIn('servers.id', $user->accessibleServers()->pluck('id')->all());
|
2019-08-03 19:44:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 04:25:00 +00:00
|
|
|
$servers = $builder->paginate(min($request->query('per_page', 50), 100))->appends($request->query());
|
2018-02-28 03:28:43 +00:00
|
|
|
|
2020-07-07 04:25:00 +00:00
|
|
|
return $this->fractal->transformWith($transformer)->collection($servers)->toArray();
|
2018-02-28 03:28:43 +00:00
|
|
|
}
|
2019-11-04 01:37:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the subuser permissions available on the system.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'object' => 'system_permissions',
|
|
|
|
'attributes' => [
|
2020-03-26 04:58:37 +00:00
|
|
|
'permissions' => Permission::permissions(),
|
2019-11-04 01:37:06 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-02-25 21:30:56 +00:00
|
|
|
}
|