Files
panel/app/Http/Controllers/Api/Client/ClientController.php

95 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Client;
2025-09-24 13:34:19 +02:00
use App\Http\Requests\Api\Client\GetServersRequest;
2024-03-12 22:39:16 -04:00
use App\Models\Filters\MultiFieldServerFilter;
2025-09-24 13:34:19 +02:00
use App\Models\Server;
use App\Models\Subuser;
2024-03-12 22:39:16 -04:00
use App\Transformers\Api\Client\ServerTransformer;
use Dedoc\Scramble\Attributes\Group;
2025-07-18 13:49:26 +02:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
2025-09-24 13:34:19 +02:00
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
#[Group('Base')]
class ClientController extends ClientApiController
{
/**
* ClientController constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* List servers
*
* Return all the servers available to the client making the API
* request, including servers the user has access to as a subuser.
*
* @return array<array-key, mixed>
*/
public function index(GetServersRequest $request): array
{
$user = $request->user();
$transformer = $this->getTransformer(ServerTransformer::class);
2025-07-18 13:49:26 +02:00
/** @var Builder<Model> $query */
$query = Server::query()->with($this->getIncludesForTransformer($transformer, ['node']));
// Start the query builder and ensure we eager load any requested relationships from the request.
2025-07-18 13:49:26 +02:00
$builder = QueryBuilder::for($query)->allowedFilters([
'uuid',
'name',
'description',
'external_id',
2021-01-23 12:33:34 -08:00
AllowedFilter::custom('*', new MultiFieldServerFilter()),
]);
$type = $request->input('type');
// Either return all the servers the user has access to because they are an admin `?type=admin` or
// just return all the servers the user has access to because they are the owner or a subuser of the
// 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.
Admin Roles (#502) * add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
2024-09-21 12:27:41 +02:00
if (!$user->isRootAdmin()) {
$builder->whereRaw('1 = 2');
} else {
$builder = $type === 'admin-all'
? $builder
: $builder->whereNotIn('servers.id', $user->directAccessibleServers()->pluck('id')->all());
}
} elseif ($type === 'owner') {
$builder = $builder->where('servers.owner_id', $user->id);
} else {
$builder = $builder->whereIn('servers.id', $user->directAccessibleServers()->pluck('id')->all());
}
2024-03-17 13:46:01 -04:00
$servers = $builder->paginate(min($request->query('per_page', '50'), 100))->appends($request->query());
return $this->fractal->transformWith($transformer)->collection($servers)->toArray();
}
/**
* List subuser permissions
*
* Returns all the subuser permissions available on the system.
*
* @return array{object: string, attributes: array{permissions: string[]}}
*/
public function permissions(): array
{
return [
'object' => 'system_permissions',
'attributes' => [
'permissions' => Subuser::allPermissionKeys(),
],
];
}
}