From 092c942764e7bb8b116e8c1b1d3923e31a8b890f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 25 Oct 2020 17:29:57 -0700 Subject: [PATCH] Fix server owner filtering; improve searching for servers; closes #2581 --- .../Admin/Servers/ServerController.php | 8 +++- app/Models/Filters/AdminServerFilter.php | 40 +++++++++++++++++++ resources/views/admin/servers/index.blade.php | 2 +- resources/views/admin/users/index.blade.php | 2 +- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 app/Models/Filters/AdminServerFilter.php diff --git a/app/Http/Controllers/Admin/Servers/ServerController.php b/app/Http/Controllers/Admin/Servers/ServerController.php index a0b73f552..c8e54b1c3 100644 --- a/app/Http/Controllers/Admin/Servers/ServerController.php +++ b/app/Http/Controllers/Admin/Servers/ServerController.php @@ -6,7 +6,9 @@ use Illuminate\Http\Request; use Pterodactyl\Models\Server; use Spatie\QueryBuilder\QueryBuilder; use Illuminate\Contracts\View\Factory; +use Spatie\QueryBuilder\AllowedFilter; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Models\Filters\AdminServerFilter; use Pterodactyl\Repositories\Eloquent\ServerRepository; class ServerController extends Controller @@ -45,8 +47,10 @@ class ServerController extends Controller public function index(Request $request) { $servers = QueryBuilder::for(Server::query()->with('node', 'user', 'allocation')) - ->allowedFilters(['uuid', 'name', 'image']) - ->allowedSorts(['id', 'uuid']) + ->allowedFilters([ + AllowedFilter::exact('owner_id'), + AllowedFilter::custom('*', new AdminServerFilter), + ]) ->paginate(config()->get('pterodactyl.paginate.admin.servers')); return $this->view->make('admin.servers.index', ['servers' => $servers]); diff --git a/app/Models/Filters/AdminServerFilter.php b/app/Models/Filters/AdminServerFilter.php new file mode 100644 index 000000000..d523e48e0 --- /dev/null +++ b/app/Models/Filters/AdminServerFilter.php @@ -0,0 +1,40 @@ +getQuery()->from !== 'servers') { + throw new BadMethodCallException( + 'Cannot use the AdminServerFilter against a non-server model.' + ); + } + $query + ->select('servers.*') + ->leftJoin('users', 'users.id', '=', 'servers.owner_id') + ->where(function (Builder $builder) use ($value) { + $builder->where('servers.uuid', $value) + ->orWhere('servers.uuid', 'LIKE', "$value%") + ->orWhere('servers.uuidShort', $value) + ->orWhere('servers.external_id', $value) + ->orWhereRaw('LOWER(users.username) LIKE ?', ["%$value%"]) + ->orWhereRaw('LOWER(users.email) LIKE ?', ["$value%"]) + ->orWhereRaw('LOWER(servers.name) LIKE ?', ["%$value%"]); + }) + ->groupBy('servers.id'); + } +} diff --git a/resources/views/admin/servers/index.blade.php b/resources/views/admin/servers/index.blade.php index f76ab1c33..b37044103 100644 --- a/resources/views/admin/servers/index.blade.php +++ b/resources/views/admin/servers/index.blade.php @@ -26,7 +26,7 @@
- +
diff --git a/resources/views/admin/users/index.blade.php b/resources/views/admin/users/index.blade.php index 98efd1176..efde3ba4c 100644 --- a/resources/views/admin/users/index.blade.php +++ b/resources/views/admin/users/index.blade.php @@ -64,7 +64,7 @@ @endif - {{ $user->servers_count }} + {{ $user->servers_count }} {{ $user->subuser_of_count }}