Fix database management things to actually work correctly.

This commit is contained in:
Dane Everitt 2017-07-22 13:55:30 -05:00
parent 580e5ac569
commit 63e39fbe58
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 124 additions and 119 deletions

View file

@ -44,4 +44,12 @@ interface UserRepositoryInterface extends RepositoryInterface, SearchableInterfa
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function deleteIfNoServers($id);
/**
* Return all matching models for a user in a format that can be used for dropdowns.
*
* @param string $query
* @return \Illuminate\Database\Eloquent\Collection
*/
public function filterUsersByQuery($query);
}

View file

@ -63,6 +63,11 @@ class ServersController extends Controller
*/
protected $databaseRepository;
/**
* @var \Pterodactyl\Services\Database\CreationService
*/
protected $databaseCreationService;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
@ -97,6 +102,7 @@ class ServersController extends Controller
AllocationRepositoryInterface $allocationRepository,
ConfigRepository $config,
CreationService $service,
\Pterodactyl\Services\Database\CreationService $databaseCreationService,
DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepository $databaseHostRepository,
LocationRepositoryInterface $locationRepository,
@ -106,6 +112,7 @@ class ServersController extends Controller
) {
$this->allocationRepository = $allocationRepository;
$this->config = $config;
$this->databaseCreationService = $databaseCreationService;
$this->databaseRepository = $databaseRepository;
$this->databaseHostRepository = $databaseHostRepository;
$this->locationRepository = $locationRepository;
@ -583,20 +590,25 @@ class ServersController extends Controller
*/
public function newDatabase(Request $request, $id)
{
$repo = new DatabaseRepository;
try {
$repo->create($id, $request->only(['host', 'database', 'connection']));
Alert::success('A new database was assigned to this server successfully.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
}
$this->databaseCreationService->create($id, [
'database' => $request->input('database'),
'remote' => $request->input('remote'),
'database_host_id' => $request->input('database_host_id'),
]);
// $repo = new DatabaseRepository;
//
// try {
// $repo->create($id, $request->only(['host', 'database', 'connection']));
//
// Alert::success('A new database was assigned to this server successfully.')->flash();
// } catch (DisplayValidationException $ex) {
// return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
// } catch (DisplayException $ex) {
// Alert::danger($ex->getMessage())->flash();
// } catch (\Exception $ex) {
// Log::error($ex);
// Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
// }
return redirect()->route('admin.servers.view.database', $id)->withInput();
}

View file

@ -28,7 +28,7 @@ use Illuminate\Http\Request;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Models\User;
use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\Administrative\UserService;
use Pterodactyl\Services\UserService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
@ -41,7 +41,7 @@ class UserController extends Controller
protected $alert;
/**
* @var \Pterodactyl\Services\Administrative\UserService
* @var \Pterodactyl\Services\UserService
*/
protected $service;
@ -59,7 +59,7 @@ class UserController extends Controller
* UserController constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\Administrative\UserService $service
* @param \Pterodactyl\Services\UserService $service
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
* @param \Pterodactyl\Models\User $model
*/
@ -178,17 +178,11 @@ class UserController extends Controller
/**
* Get a JSON response of users on the system.
*
* @param \Illuminate\Http\Request $request
* @return \Pterodactyl\Models\User
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Collection
*/
public function json(Request $request)
{
return $this->model->search($request->input('q'))->all([
'id', 'email', 'username', 'name_first', 'name_last',
])->transform(function ($item) {
$item->md5 = md5(strtolower($item->email));
return $item;
});
return $this->repository->filterUsersByQuery($request->input('q'));
}
}

View file

@ -33,17 +33,10 @@ class DatabaseHostFormRequest extends AdminFormRequest
*/
public function rules()
{
$this->merge([
'node_id' => ($this->input('node_id') < 1) ? null : $this->input('node_id'),
'host' => gethostbyname($this->input('host')),
]);
$rules = app()->make(DatabaseHost::class)->getRules();
if ($this->method() === 'PATCH') {
$rules['host'] = $rules['host'] . ',' . $this->route()->parameter('host')->id;
if ($this->method() !== 'POST') {
return DatabaseHost::getUpdateRulesForId($this->route()->parameter('host')->id);
}
return $rules;
return DatabaseHost::getCreateRules();
}
}

View file

@ -24,11 +24,12 @@
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Database extends Model
class Database extends Model implements ValidableContract
{
use Eloquence, Validable;
@ -52,7 +53,7 @@ class Database extends Model
* @var array
*/
protected $fillable = [
'server_id', 'database_host_id', 'database', 'username', 'remote',
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote',
];
/**

View file

@ -27,8 +27,9 @@ namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class DatabaseHost extends Model
class DatabaseHost extends Model implements ValidableContract
{
use Eloquence, Validable;
@ -52,7 +53,7 @@ class DatabaseHost extends Model
* @var array
*/
protected $fillable = [
'name', 'host', 'port', 'username', 'max_databases', 'node_id',
'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id',
];
/**
@ -79,11 +80,12 @@ class DatabaseHost extends Model
'node_id' => 'sometimes|required',
];
/**
* Validation rules to assign to this model.
*
* @var array
*/
/**
* Validation rules to assign to this model.
*
* @var array
*/
// @todo the node_id field doesn't validate correctly if no node is provided in request
protected static $dataIntegrityRules = [
'name' => 'string|max:255',
'host' => 'ip|unique:database_hosts,host',

View file

@ -24,32 +24,32 @@
namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Database\DatabaseManager;
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
* @var \Illuminate\Database\DatabaseManager
*/
protected $connection;
protected $database;
/**
* DatabaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\ConnectionResolver $connection
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\DatabaseManager $database
*/
public function __construct(
Application $application,
ConnectionResolver $connection
DatabaseManager $database
) {
parent::__construct($application);
$this->connection = $connection;
$this->database = $database;
}
/**
@ -150,6 +150,6 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
*/
protected function runStatement($statement, $connection = null)
{
return $this->connection->connection($connection)->statement($statement);
return $this->database->connection($connection)->statement($statement);
}
}

View file

@ -93,4 +93,22 @@ class UserRepository extends SearchableRepository implements UserRepositoryInter
return $user->delete();
}
/**
* {@inheritdoc}
*/
public function filterUsersByQuery($query)
{
$this->withColumns([
'id', 'email', 'username', 'name_first', 'name_last',
]);
$instance = $this->getBuilder()->search($query)->get($this->getColumns());
return $instance->transform(function ($item) {
$item->md5 = md5(strtolower($item->email));
return $item;
});
}
}

View file

@ -24,8 +24,7 @@
namespace Pterodactyl\Services\Database;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -33,12 +32,7 @@ use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class CreationService
{
/**
* @var \Illuminate\Database\ConnectionResolver
*/
protected $connection;
/**
* @var \Illuminate\Database\ConnectionInterface
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
@ -60,20 +54,17 @@ class CreationService
/**
* CreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $database
* @param \Illuminate\Database\ConnectionResolver $connection
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
ConnectionInterface $database,
ConnectionResolver $connection,
DatabaseManager $database,
DynamicDatabaseConnection $dynamic,
DatabaseRepositoryInterface $repository,
Encrypter $encrypter
) {
$this->connection = $connection;
$this->database = $database;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
@ -83,6 +74,7 @@ class CreationService
/**
* Create a new database that is linked to a specific host.
*
* @param int $server
* @param array $data
* @return \Illuminate\Database\Eloquent\Model
*
@ -90,10 +82,11 @@ class CreationService
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function create(array $data)
public function create($server, array $data)
{
$data['database'] = sprintf('d%d_%s', $data['server_id'], $data['database']);
$data['username'] = sprintf('u%d_%s', $data['server_id'], str_random(10));
$data['server_id'] = $server;
$data['database'] = sprintf('d%d_%s', $server, $data['database']);
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
$data['password'] = $this->encrypter->encrypt(str_random(16));
$this->database->beginTransaction();

View file

@ -24,8 +24,8 @@
namespace Pterodactyl\Services\Database;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -54,14 +54,14 @@ class DatabaseHostService
/**
* DatabaseHostService constructor.
*
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
DatabaseHostRepositoryInterface $repository,
DatabaseManager $database,
DatabaseHostRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic,
Encrypter $encrypter
) {
@ -74,7 +74,7 @@ class DatabaseHostService
/**
* Create a new database host and persist it to the database.
*
* @param array $data
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Throwable
@ -106,11 +106,11 @@ class DatabaseHostService
/**
* Update a database host and persist to the database.
*
* @param int $id
* @param array $data
* @param int $id
* @param array $data
* @return mixed
*
* @throws \PDOException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function update($id, array $data)
{
@ -135,7 +135,7 @@ class DatabaseHostService
/**
* Delete a database host if it has no active databases attached to it.
*
* @param int $id
* @param int $id
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\DisplayException

View file

@ -117,7 +117,7 @@
<div class="form-group">
<label for="pNodeId" class="form-label">Linked Node</label>
<select name="node_id" id="pNodeId" class="form-control">
<option value="0">None</option>
<option value="null">None</option>
@foreach($locations as $location)
<optgroup label="{{ $location->short }}">
@foreach($location->nodes as $node)

View file

@ -91,8 +91,8 @@
<form action="{{ route('admin.servers.view.database', $server->id) }}" method="POST">
<div class="box-body">
<div class="form-group">
<label for="pDatabaseHost" class="control-label">Database Host</label>
<select id="pDatabaseHost" name="host" class="form-control">
<label for="pDatabaseHostId" class="control-label">Database Host</label>
<select id="pDatabaseHostId" name="database_host_id" class="form-control">
@foreach($hosts as $host)
<option value="{{ $host->id }}">{{ $host->name }}</option>
@endforeach
@ -107,8 +107,8 @@
</div>
</div>
<div class="form-group">
<label for="pConnections" class="control-label">Connections</label>
<input id="pConnections" type="text" name="connection" class="form-control" placeholder="%" value="%" />
<label for="pRemote" class="control-label">Connections</label>
<input id="pRemote" type="text" name="remote" class="form-control" value="%" />
<p class="text-muted small">This should reflect the IP address that connections are allowed from. Uses standard MySQL notation. If unsure leave as <code>%</code>.</p>
</div>
</div>

View file

@ -25,13 +25,14 @@
namespace Tests\Unit\Services\Database;
use Exception;
use Illuminate\Database\DatabaseManager;
use Mockery as m;
use Tests\TestCase;
use phpmock\phpunit\PHPMock;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Services\Database\CreationService;
use Illuminate\Database\ConnectionResolver;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -49,12 +50,7 @@ class CreationServiceTest extends TestCase
];
/**
* @var \Illuminate\Database\ConnectionResolver
*/
protected $connection;
/**
* @var \Illuminate\Database\ConnectionInterface
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
@ -85,8 +81,7 @@ class CreationServiceTest extends TestCase
{
parent::setUp();
$this->connection = m::mock(ConnectionResolver::class);
$this->database = m::mock(ConnectionInterface::class);
$this->database = m::mock(DatabaseManager::class);
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
$this->encrypter = m::mock(Encrypter::class);
$this->repository = m::mock(DatabaseRepositoryInterface::class);
@ -96,7 +91,6 @@ class CreationServiceTest extends TestCase
$this->service = new CreationService(
$this->database,
$this->connection,
$this->dynamic,
$this->repository,
$this->encrypter
@ -110,10 +104,8 @@ class CreationServiceTest extends TestCase
{
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with(
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic'
@ -131,16 +123,15 @@ class CreationServiceTest extends TestCase
$this->repository->shouldReceive('flush')->with('dynamic')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->create([
'server_id' => 1,
$response = $this->service->create(1, [
'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3,
]);
$this->assertNotEmpty($response);
$this->assertTrue(is_object($response), 'Assert that response is an object.');
$this->assertEquals(self::TEST_DATA['server_id'], $response->server_id);
$this->assertEquals(self::TEST_DATA['database'], $response->database);
$this->assertEquals(self::TEST_DATA['remote'], $response->remote);
$this->assertEquals(self::TEST_DATA['username'], $response->username);
@ -157,16 +148,13 @@ class CreationServiceTest extends TestCase
{
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with(
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andThrow(new Exception('Test Message'));
$this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andThrow(new Exception('Test Message'));
$this->repository->shouldNotReceive('dropDatabase');
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->service->create([
'server_id' => 1,
$this->service->create(1, [
'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3,
]);
}
@ -180,10 +168,7 @@ class CreationServiceTest extends TestCase
{
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with(
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic'
@ -197,9 +182,9 @@ class CreationServiceTest extends TestCase
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->service->create([
'server_id' => 1,
$this->service->create(1, [
'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3,
]);
}
@ -211,10 +196,7 @@ class CreationServiceTest extends TestCase
{
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with(
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic'
@ -226,9 +208,9 @@ class CreationServiceTest extends TestCase
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {
$this->service->create([
'server_id' => 1,
$this->service->create(1, [
'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3,
]);
} catch (Exception $ex) {

View file

@ -24,9 +24,11 @@
namespace Tests\Unit\Services\Administrative;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\DatabaseManager;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -72,8 +74,8 @@ class DatabaseHostServiceTest extends TestCase
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
$this->service = new DatabaseHostService(
$this->repository,
$this->database,
$this->repository,
$this->dynamic,
$this->encrypter
);