Code cleanup & fix frontend searching servers; closes #2100

This commit is contained in:
Dane Everitt 2020-07-06 21:25:00 -07:00
parent f0e18ba6f7
commit d3c749ac56
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 226 additions and 194 deletions

View file

@ -2,9 +2,11 @@
namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Http\Request;
use Webmozart\Assert\Assert;
use Illuminate\Support\Collection;
use Pterodactyl\Repositories\Repository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@ -15,6 +17,53 @@ use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
abstract class EloquentRepository extends Repository implements RepositoryInterface
{
/**
* @var bool
*/
protected $useRequestFilters = false;
/**
* Determines if the repository function should use filters off the request object
* present when returning results. This allows repository methods to be called in API
* context's such that we can pass through ?filter[name]=Dane&sort=desc for example.
*
* @param bool $usingFilters
* @return $this
*/
public function usingRequestFilters($usingFilters = true)
{
$this->useRequestFilters = $usingFilters;
return $this;
}
/**
* Returns the request instance.
*
* @return \Illuminate\Http\Request
*/
protected function request()
{
return $this->app->make(Request::class);
}
/**
* Paginate the response data based on the page para.
*
* @param \Illuminate\Database\Eloquent\Builder $instance
* @param int $default
*
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
protected function paginate(Builder $instance, int $default = 50)
{
if (! $this->useRequestFilters) {
return $instance->paginate($default);
}
return $instance->paginate($this->request()->query('per_page', $default));
}
/**
* Return an instance of the eloquent model bound to this
* repository instance.
@ -236,6 +285,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
* Return all records associated with the given model.
*
* @return \Illuminate\Support\Collection
* @deprecated Just use the model
*/
public function all(): Collection
{
@ -313,6 +363,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
* Get the amount of entries in the database.
*
* @return int
* @deprecated just use the count method off a model
*/
public function count(): int
{

View file

@ -2,7 +2,6 @@
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
@ -226,43 +225,6 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
];
}
/**
* Return a paginated list of servers that a user can access at a given level.
*
* @param \Pterodactyl\Models\User $user
* @param int $level
* @param bool|int $paginate
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection
*/
public function filterUserAccessServers(User $user, int $level, $paginate = 25)
{
$instance = $this->getBuilder()->select($this->getColumns())->with(['user', 'node', 'allocation']);
// If access level is set to owner, only display servers
// that the user owns.
if ($level === User::FILTER_LEVEL_OWNER) {
$instance->where('owner_id', $user->id);
}
// If set to all, display all servers they can access, including
// those they access as an admin. If set to subuser, only return
// the servers they can access because they are owner, or marked
// as a subuser of the server.
elseif (($level === User::FILTER_LEVEL_ALL && ! $user->root_admin) || $level === User::FILTER_LEVEL_SUBUSER) {
$instance->whereIn('id', $this->getUserAccessServers($user->id));
}
// If set to admin, only display the servers a user can access
// as an administrator (leaves out owned and subuser of).
elseif ($level === User::FILTER_LEVEL_ADMIN && $user->root_admin) {
$instance->whereNotIn('id', $this->getUserAccessServers($user->id));
}
$instance->search($this->getSearchTerm());
return $paginate ? $instance->paginate($paginate) : $instance->get();
}
/**
* Return a server by UUID.
*
@ -339,20 +301,6 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();
}
/**
* Return an array of server IDs that a given user can access based
* on owner and subuser permissions.
*
* @param int $user
* @return int[]
*/
private function getUserAccessServers(int $user): array
{
return $this->getBuilder()->select('id')->where('owner_id', $user)->union(
$this->app->make(SubuserRepository::class)->getBuilder()->select('server_id')->where('user_id', $user)
)->pluck('id')->all();
}
/**
* Get the amount of servers that are suspended.
*