Repository interface improvements

This commit is contained in:
Dane Everitt 2017-07-15 11:52:34 -05:00
parent 1f4f6024cc
commit bc3366b10d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
23 changed files with 829 additions and 179 deletions

View file

@ -0,0 +1,51 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent\Attributes;
use Pterodactyl\Repositories\Eloquent\EloquentRepository;
use Pterodactyl\Contracts\Repository\Attributes\SearchableInterface;
abstract class SearchableRepository extends EloquentRepository implements SearchableInterface
{
/**
* @var bool|string
*/
protected $searchTerm = false;
/**
* {@inheritdoc}
*/
public function search($term)
{
if (empty($term)) {
return $this;
}
$clone = clone $this;
$clone->searchTerm = $term;
return $clone;
}
}

View file

@ -24,12 +24,12 @@
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Models\DatabaseHost;
class DatabaseHostRepository extends EloquentRepository implements DatabaseHostInterface
class DatabaseHostRepository extends EloquentRepository implements DatabaseHostRepositoryInterface
{
/**
* {@inheritdoc}

View file

@ -0,0 +1,155 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Database;
use Illuminate\Foundation\Application;
use Illuminate\Database\ConnectionResolver;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
{
/**
* @var \Illuminate\Database\ConnectionResolverInterface
*/
protected $connection;
/**
* DatabaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\ConnectionResolver $connection
*/
public function __construct(
Application $application,
ConnectionResolver $connection
) {
parent::__construct($application);
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function model()
{
return Database::class;
}
/**
* {@inheritdoc}
* @return bool|\Illuminate\Database\Eloquent\Model
*/
public function createIfNotExists(array $data)
{
$instance = $this->getBuilder()->where([
['server_id', $data['server_id']],
['database_host_id', $data['database_host_id']],
['database', $data['database']],
])->count();
if ($instance > 0) {
throw new DisplayException('A database with those details already exists for the specified server.');
}
return $this->create($data);
}
/**
* {@inheritdoc}
*/
public function createDatabase($database, $connection = null)
{
return $this->runStatement(
sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database), $connection
);
}
/**
* {@inheritdoc}
*/
public function createUser($username, $remote, $password, $connection = null)
{
return $this->runStatement(
sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password), $connection
);
}
/**
* {@inheritdoc}
*/
public function assignUserToDatabase($database, $username, $remote, $connection = null)
{
return $this->runStatement(
sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
$database, $username, $remote
),
$connection
);
}
/**
* {@inheritdoc}
*/
public function flush($connection = null)
{
return $this->runStatement('FLUSH PRIVILEGES', $connection);
}
/**
* {@inheritdoc}
*/
public function dropDatabase($database, $connection = null)
{
return $this->runStatement(
sprintf('DROP DATABASE IF EXISTS `%s`', $database), $connection
);
}
/**
* {@inheritdoc}
*/
public function dropUser($username, $remote, $connection = null)
{
return $this->runStatement(
sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote), $connection
);
}
/**
* Run the provided statement against the database on a given connection.
*
* @param string $statement
* @param null|string $connection
* @return bool
*/
protected function runStatement($statement, $connection = null)
{
return $this->connection->connection($connection)->statement($statement);
}
}

View file

@ -152,4 +152,12 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
{
// TODO: Implement massUpdate() method.
}
/**
* {@inheritdoc}
*/
public function all()
{
return $this->getBuilder()->get($this->getColumns());
}
}

View file

@ -0,0 +1,54 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
class ServerRepository extends SearchableRepository implements ServerRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return Server::class;
}
/**
* {@inheritdoc}
*/
public function getAllServers($paginate = 25)
{
$instance = $this->getBuilder()->with('node', 'user', 'allocation');
if ($this->searchTerm) {
$instance->search($this->searchTerm);
}
return $instance->paginate($paginate);
}
}

View file

@ -0,0 +1,60 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Models\Service;
class ServiceRepository extends EloquentRepository implements ServiceRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function model()
{
return Service::class;
}
/**
* {@inheritdoc}
*/
public function getWithOptions($id = null)
{
$instance = $this->getBuilder()->with('options.packs', 'options.variables');
if (! is_null($id)) {
$instance = $instance->find($id, $this->getColumns());
if (! $instance) {
throw new RecordNotFoundException();
}
return $instance;
}
return $instance->get($this->getColumns());
}
}

View file

@ -30,19 +30,15 @@ use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Models\User;
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
class UserRepository extends EloquentRepository implements UserRepositoryInterface
class UserRepository extends SearchableRepository implements UserRepositoryInterface
{
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var bool|array
*/
protected $searchTerm = false;
/**
* UserRepository constructor.
*
@ -64,21 +60,6 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
return User::class;
}
/**
* {@inheritdoc}
*/
public function search($term)
{
if (empty($term)) {
return $this;
}
$clone = clone $this;
$clone->searchTerm = $term;
return $clone;
}
/**
* {@inheritdoc}
*/