Start ripping out old search functionality for models

This commit is contained in:
Dane Everitt 2020-09-13 11:29:47 -07:00
parent 3c7ffaaadb
commit 4dddcaebb0
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
16 changed files with 15 additions and 182 deletions

View file

@ -1,38 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository\Attributes;
interface SearchableInterface
{
/**
* Set the search term.
*
* @param string|null $term
* @return $this
* @deprecated
*/
public function search($term);
/**
* Set the search term to use when requesting all records from
* the model.
*
* @param string|null $term
* @return $this
*/
public function setSearchTerm(string $term = null);
/**
* Determine if a valid search term is set on this repository.
*
* @return bool
*/
public function hasSearchTerm(): bool;
/**
* Return the search term.
*
* @return string|null
*/
public function getSearchTerm();
}

View file

@ -4,9 +4,8 @@ namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface LocationRepositoryInterface extends RepositoryInterface, SearchableInterface interface LocationRepositoryInterface extends RepositoryInterface
{ {
/** /**
* Return locations with a count of nodes and servers attached to it. * Return locations with a count of nodes and servers attached to it.

View file

@ -8,7 +8,7 @@ use Illuminate\Support\LazyCollection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface; use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface NodeRepositoryInterface extends RepositoryInterface, SearchableInterface interface NodeRepositoryInterface extends RepositoryInterface
{ {
const THRESHOLD_PERCENTAGE_LOW = 75; const THRESHOLD_PERCENTAGE_LOW = 75;
const THRESHOLD_PERCENTAGE_MEDIUM = 90; const THRESHOLD_PERCENTAGE_MEDIUM = 90;

View file

@ -5,9 +5,8 @@ namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface ServerRepositoryInterface extends RepositoryInterface, SearchableInterface interface ServerRepositoryInterface extends RepositoryInterface
{ {
/** /**
* Returns a listing of all servers that exist including relationships. * Returns a listing of all servers that exist including relationships.

View file

@ -6,7 +6,7 @@ use Illuminate\Support\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface; use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
interface UserRepositoryInterface extends RepositoryInterface, SearchableInterface interface UserRepositoryInterface extends RepositoryInterface
{ {
/** /**
* Return all users with counts of servers and subusers of servers. * Return all users with counts of servers and subusers of servers.

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Models;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Pterodactyl\Models\Traits\Searchable;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
/** /**
@ -40,7 +39,6 @@ use Illuminate\Contracts\Encryption\Encrypter;
class Node extends Model class Node extends Model
{ {
use Notifiable; use Notifiable;
use Searchable;
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an
@ -95,18 +93,6 @@ class Node extends Model
'description', 'maintenance_mode', 'description', 'maintenance_mode',
]; ];
/**
* Fields that are searchable.
*
* @var array
*/
protected $searchableColumns = [
'name' => 10,
'fqdn' => 8,
'location.short' => 4,
'location.long' => 4,
];
/** /**
* @var array * @var array
*/ */
@ -216,7 +202,7 @@ class Node extends Model
*/ */
public function getDecryptedKey(): string public function getDecryptedKey(): string
{ {
return (string) Container::getInstance()->make(Encrypter::class)->decrypt( return (string)Container::getInstance()->make(Encrypter::class)->decrypt(
$this->daemon_token $this->daemon_token
); );
} }

View file

@ -3,7 +3,6 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Pterodactyl\Models\Traits\Searchable;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Znck\Eloquent\Traits\BelongsToThrough; use Znck\Eloquent\Traits\BelongsToThrough;
@ -58,7 +57,6 @@ class Server extends Model
{ {
use BelongsToThrough; use BelongsToThrough;
use Notifiable; use Notifiable;
use Searchable;
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an
@ -154,21 +152,6 @@ class Server extends Model
'backup_limit' => 'integer', 'backup_limit' => 'integer',
]; ];
/**
* Parameters for search querying.
*
* @var array
*/
protected $searchableColumns = [
'name' => 100,
'uuid' => 80,
'uuidShort' => 80,
'external_id' => 50,
'user.email' => 40,
'user.username' => 30,
'node.name' => 10,
];
/** /**
* Returns the format for server allocations when communicating with the Daemon. * Returns the format for server allocations when communicating with the Daemon.
* *

View file

@ -1,13 +0,0 @@
<?php
namespace Pterodactyl\Models\Traits;
use Pterodactyl\Extensions\Illuminate\Database\Eloquent\Builder;
trait Searchable
{
public function newEloquentBuilder($query)
{
return new Builder($query);
}
}

View file

@ -8,7 +8,6 @@ use Illuminate\Validation\Rules\In;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Models\Traits\Searchable;
use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Auth\Passwords\CanResetPassword;
use Pterodactyl\Traits\Helpers\AvailableLanguages; use Pterodactyl\Traits\Helpers\AvailableLanguages;
use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Foundation\Auth\Access\Authorizable;
@ -52,7 +51,6 @@ class User extends Model implements
use AvailableLanguages; use AvailableLanguages;
use CanResetPassword; use CanResetPassword;
use Notifiable; use Notifiable;
use Searchable;
const USER_LEVEL_USER = 0; const USER_LEVEL_USER = 0;
const USER_LEVEL_ADMIN = 1; const USER_LEVEL_ADMIN = 1;
@ -120,20 +118,6 @@ class User extends Model implements
*/ */
protected $hidden = ['password', 'remember_token', 'totp_secret', 'totp_authenticated_at']; protected $hidden = ['password', 'remember_token', 'totp_secret', 'totp_authenticated_at'];
/**
* Parameters for search querying.
*
* @var array
*/
protected $searchableColumns = [
'username' => 100,
'email' => 100,
'external_id' => 80,
'uuid' => 80,
'name_first' => 40,
'name_last' => 40,
];
/** /**
* Default values for specific fields in the database. * Default values for specific fields in the database.
* *

View file

@ -11,18 +11,6 @@ trait Searchable
*/ */
protected $searchTerm; protected $searchTerm;
/**
* Set the search term.
*
* @param string|null $term
* @return $this
* @deprecated
*/
public function search($term)
{
return $this->setSearchTerm($term);
}
/** /**
* Set the search term to use when requesting all records from * Set the search term to use when requesting all records from
* the model. * the model.
@ -41,24 +29,4 @@ trait Searchable
return $clone; return $clone;
} }
/**
* Determine if a valid search term is set on this repository.
*
* @return bool
*/
public function hasSearchTerm(): bool
{
return ! empty($this->searchTerm);
}
/**
* Return the search term.
*
* @return string|null
*/
public function getSearchTerm()
{
return $this->searchTerm;
}
} }

View file

@ -289,12 +289,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/ */
public function all(): Collection public function all(): Collection
{ {
$instance = $this->getBuilder(); return $this->getBuilder()->get($this->getColumns());
if (is_subclass_of(get_called_class(), SearchableInterface::class) && $this->hasSearchTerm()) {
$instance = $instance->search($this->getSearchTerm());
}
return $instance->get($this->getColumns());
} }
/** /**
@ -305,12 +300,7 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
*/ */
public function paginated(int $perPage): LengthAwarePaginator public function paginated(int $perPage): LengthAwarePaginator
{ {
$instance = $this->getBuilder(); return $this->getBuilder()->paginate($perPage, $this->getColumns());
if (is_subclass_of(get_called_class(), SearchableInterface::class) && $this->hasSearchTerm()) {
$instance = $instance->search($this->getSearchTerm());
}
return $instance->paginate($perPage, $this->getColumns());
} }
/** /**

View file

@ -4,15 +4,12 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Location; use Pterodactyl\Models\Location;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
{ {
use Searchable;
/** /**
* Return the model backing this repository. * Return the model backing this repository.
* *

View file

@ -5,14 +5,11 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Mount; use Pterodactyl\Models\Mount;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
class MountRepository extends EloquentRepository class MountRepository extends EloquentRepository
{ {
use Searchable;
/** /**
* Return the model backing this repository. * Return the model backing this repository.
* *

View file

@ -5,14 +5,11 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Node; use Pterodactyl\Models\Node;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection; use Illuminate\Support\LazyCollection;
use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
class NodeRepository extends EloquentRepository implements NodeRepositoryInterface class NodeRepository extends EloquentRepository implements NodeRepositoryInterface
{ {
use Searchable;
/** /**
* Return the model backing this repository. * Return the model backing this repository.
* *
@ -92,13 +89,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
*/ */
public function getNodeListingData(): LengthAwarePaginator public function getNodeListingData(): LengthAwarePaginator
{ {
$instance = $this->getBuilder()->with('location')->withCount('servers'); return $this->getBuilder()->with('location')->withCount('servers')->paginate(25, $this->getColumns());
if ($this->hasSearchTerm()) {
$instance->search($this->getSearchTerm());
}
return $instance->paginate(25, $this->getColumns());
} }
/** /**

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
@ -13,8 +12,6 @@ use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ServerRepository extends EloquentRepository implements ServerRepositoryInterface class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
{ {
use Searchable;
/** /**
* Return the model backing this repository. * Return the model backing this repository.
* *
@ -33,9 +30,7 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
*/ */
public function getAllServers(int $paginate): LengthAwarePaginator public function getAllServers(int $paginate): LengthAwarePaginator
{ {
$instance = $this->getBuilder()->with('node', 'user', 'allocation')->search($this->getSearchTerm()); return $this->getBuilder()->with('node', 'user', 'allocation')->paginate($paginate, $this->getColumns());
return $instance->paginate($paginate, $this->getColumns());
} }
/** /**
@ -67,7 +62,7 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
if (! is_null($server) && is_null($node)) { if (! is_null($server) && is_null($node)) {
$instance = $instance->where('id', '=', $server); $instance = $instance->where('id', '=', $server);
} elseif (is_null($server) && ! is_null($node)) { } else if (is_null($server) && ! is_null($node)) {
$instance = $instance->where('node_id', '=', $node); $instance = $instance->where('node_id', '=', $node);
} }
@ -87,7 +82,7 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
if (! is_null($server) && is_null($node)) { if (! is_null($server) && is_null($node)) {
$instance = $instance->where('id', '=', $server); $instance = $instance->where('id', '=', $server);
} elseif (is_null($server) && ! is_null($node)) { } else if (is_null($server) && ! is_null($node)) {
$instance = $instance->where('node_id', '=', $node); $instance = $instance->where('node_id', '=', $node);
} }
@ -224,9 +219,9 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt
if (! empty($nodes) && ! empty($servers)) { if (! empty($nodes) && ! empty($servers)) {
$instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes); $instance->whereIn('id', $servers)->orWhereIn('node_id', $nodes);
} elseif (empty($nodes) && ! empty($servers)) { } else if (empty($nodes) && ! empty($servers)) {
$instance->whereIn('id', $servers); $instance->whereIn('id', $servers);
} elseif (! empty($nodes) && empty($servers)) { } else if (! empty($nodes) && empty($servers)) {
$instance->whereIn('node_id', $nodes); $instance->whereIn('node_id', $nodes);
} }

View file

@ -4,14 +4,11 @@ namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Repositories\Concerns\Searchable;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserRepository extends EloquentRepository implements UserRepositoryInterface class UserRepository extends EloquentRepository implements UserRepositoryInterface
{ {
use Searchable;
/** /**
* Return the model backing this repository. * Return the model backing this repository.
* *
@ -29,9 +26,7 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
*/ */
public function getAllUsersWithCounts(): LengthAwarePaginator public function getAllUsersWithCounts(): LengthAwarePaginator
{ {
return $this->getBuilder()->withCount('servers') return $this->getBuilder()->withCount('servers')->paginate(50, $this->getColumns());
->search($this->getSearchTerm())
->paginate(50, $this->getColumns());
} }
/** /**
@ -46,7 +41,7 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
'id', 'email', 'username', 'name_first', 'name_last', 'id', 'email', 'username', 'name_first', 'name_last',
]); ]);
$instance = $this->getBuilder()->search($query)->get($this->getColumns()); $instance = $this->getBuilder()->get($this->getColumns());
return $instance->transform(function ($item) { return $instance->transform(function ($item) {
$item->md5 = md5(strtolower($item->email)); $item->md5 = md5(strtolower($item->email));