Path: blob/1.0-develop/app/Http/Controllers/Api/Client/ClientController.php
10279 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client;34use Pterodactyl\Models\Server;5use Pterodactyl\Models\Permission;6use Spatie\QueryBuilder\QueryBuilder;7use Spatie\QueryBuilder\AllowedFilter;8use Pterodactyl\Models\Filters\MultiFieldServerFilter;9use Pterodactyl\Transformers\Api\Client\ServerTransformer;10use Pterodactyl\Http\Requests\Api\Client\GetServersRequest;1112class ClientController extends ClientApiController13{14/**15* ClientController constructor.16*/17public function __construct()18{19parent::__construct();20}2122/**23* Return all the servers available to the client making the API24* request, including servers the user has access to as a subuser.25*/26public function index(GetServersRequest $request): array27{28$user = $request->user();29$transformer = $this->getTransformer(ServerTransformer::class);3031// Start the query builder and ensure we eager load any requested relationships from the request.32$builder = QueryBuilder::for(33Server::query()->with($this->getIncludesForTransformer($transformer, ['node']))34)->allowedFilters([35'uuid',36'name',37'description',38'external_id',39AllowedFilter::custom('*', new MultiFieldServerFilter()),40]);4142$type = $request->input('type');43// Either return all the servers the user has access to because they are an admin `?type=admin` or44// just return all the servers the user has access to because they are the owner or a subuser of the45// server. If ?type=admin-all is passed all servers on the system will be returned to the user, rather46// than only servers they can see because they are an admin.47if (in_array($type, ['admin', 'admin-all'])) {48// If they aren't an admin but want all the admin servers don't fail the request, just49// make it a query that will never return any results back.50if (!$user->root_admin) {51$builder->whereRaw('1 = 2');52} else {53$builder = $type === 'admin-all'54? $builder55: $builder->whereNotIn('servers.id', $user->accessibleServers()->pluck('id')->all());56}57} elseif ($type === 'owner') {58$builder = $builder->where('servers.owner_id', $user->id);59} else {60$builder = $builder->whereIn('servers.id', $user->accessibleServers()->pluck('id')->all());61}6263$servers = $builder->paginate(min($request->query('per_page', 50), 100))->appends($request->query());6465return $this->fractal->transformWith($transformer)->collection($servers)->toArray();66}6768/**69* Returns all the subuser permissions available on the system.70*/71public function permissions(): array72{73return [74'object' => 'system_permissions',75'attributes' => [76'permissions' => Permission::permissions(),77],78];79}80}818283