Fix server owner filtering; improve searching for servers; closes #2581

This commit is contained in:
Dane Everitt 2020-10-25 17:29:57 -07:00
parent 39dddba1d6
commit 092c942764
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 48 additions and 4 deletions

View file

@ -6,7 +6,9 @@ use Illuminate\Http\Request;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Spatie\QueryBuilder\AllowedFilter;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Models\Filters\AdminServerFilter;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
class ServerController extends Controller class ServerController extends Controller
@ -45,8 +47,10 @@ class ServerController extends Controller
public function index(Request $request) public function index(Request $request)
{ {
$servers = QueryBuilder::for(Server::query()->with('node', 'user', 'allocation')) $servers = QueryBuilder::for(Server::query()->with('node', 'user', 'allocation'))
->allowedFilters(['uuid', 'name', 'image']) ->allowedFilters([
->allowedSorts(['id', 'uuid']) AllowedFilter::exact('owner_id'),
AllowedFilter::custom('*', new AdminServerFilter),
])
->paginate(config()->get('pterodactyl.paginate.admin.servers')); ->paginate(config()->get('pterodactyl.paginate.admin.servers'));
return $this->view->make('admin.servers.index', ['servers' => $servers]); return $this->view->make('admin.servers.index', ['servers' => $servers]);

View file

@ -0,0 +1,40 @@
<?php
namespace Pterodactyl\Models\Filters;
use BadMethodCallException;
use Spatie\QueryBuilder\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
class AdminServerFilter implements Filter
{
/**
* A multi-column filter for the servers table that allows an administrative user to search
* across UUID, name, owner username, and owner email.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $value
* @param string $property
*/
public function __invoke(Builder $query, $value, string $property)
{
if ($query->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');
}
}

View file

@ -26,7 +26,7 @@
<div class="box-tools search01"> <div class="box-tools search01">
<form action="{{ route('admin.servers') }}" method="GET"> <form action="{{ route('admin.servers') }}" method="GET">
<div class="input-group input-group-sm"> <div class="input-group input-group-sm">
<input type="text" name="filter[name]" class="form-control pull-right" value="{{ request()->input('filter.name') }}" placeholder="Search Servers"> <input type="text" name="filter[*]" class="form-control pull-right" value="{{ request()->input('filter[*]') }}" placeholder="Search Servers">
<div class="input-group-btn"> <div class="input-group-btn">
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button> <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
<a href="{{ route('admin.servers.new') }}"><button type="button" class="btn btn-sm btn-primary" style="border-radius: 0 3px 3px 0;margin-left:-1px;">Create New</button></a> <a href="{{ route('admin.servers.new') }}"><button type="button" class="btn btn-sm btn-primary" style="border-radius: 0 3px 3px 0;margin-left:-1px;">Create New</button></a>

View file

@ -64,7 +64,7 @@
@endif @endif
</td> </td>
<td class="text-center"> <td class="text-center">
<a href="{{ route('admin.servers', ['query' => $user->email]) }}">{{ $user->servers_count }}</a> <a href="{{ route('admin.servers', ['filter[owner_id]' => $user->id]) }}">{{ $user->servers_count }}</a>
</td> </td>
<td class="text-center">{{ $user->subuser_of_count }}</td> <td class="text-center">{{ $user->subuser_of_count }}</td>
<td class="text-center"><img src="https://www.gravatar.com/avatar/{{ md5(strtolower($user->email)) }}?s=100" style="height:20px;" class="img-circle" /></td> <td class="text-center"><img src="https://www.gravatar.com/avatar/{{ md5(strtolower($user->email)) }}?s=100" style="height:20px;" class="img-circle" /></td>