Merge branch 'develop' into feature/server-mounts
This commit is contained in:
commit
295f09ca43
195 changed files with 5395 additions and 5417 deletions
|
@ -5,7 +5,6 @@ namespace Pterodactyl\Repositories\Eloquent;
|
|||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
||||
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
|
||||
|
@ -20,41 +19,6 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
return Allocation::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array of allocation IDs to be assigned to a specific server.
|
||||
*
|
||||
* @param int|null $server
|
||||
* @param array $ids
|
||||
* @return int
|
||||
*/
|
||||
public function assignAllocationsToServer(int $server = null, array $ids): int
|
||||
{
|
||||
return $this->getBuilder()->whereIn('id', $ids)->update(['server_id' => $server]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the allocations for a specific node.
|
||||
*
|
||||
* @param int $node
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getAllocationsForNode(int $node): Collection
|
||||
{
|
||||
return $this->getBuilder()->where('node_id', $node)->get($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the allocations for a node in a paginated format.
|
||||
*
|
||||
* @param int $node
|
||||
* @param int $perPage
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator
|
||||
{
|
||||
return $this->getBuilder()->where('node_id', $node)->paginate($perPage, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the unique IPs that exist for a given node.
|
||||
*
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Reference in a new issue