Skip to content

Commit 63e39fb

Browse files
committed
Fix database management things to actually work correctly.
1 parent 580e5ac commit 63e39fb

File tree

14 files changed

+124
-119
lines changed

14 files changed

+124
-119
lines changed

app/Contracts/Repository/UserRepositoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public function getAllUsersWithCounts();
4444
* @throws \Pterodactyl\Exceptions\DisplayException
4545
*/
4646
public function deleteIfNoServers($id);
47+
48+
/**
49+
* Return all matching models for a user in a format that can be used for dropdowns.
50+
*
51+
* @param string $query
52+
* @return \Illuminate\Database\Eloquent\Collection
53+
*/
54+
public function filterUsersByQuery($query);
4755
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class ServersController extends Controller
6363
*/
6464
protected $databaseRepository;
6565

66+
/**
67+
* @var \Pterodactyl\Services\Database\CreationService
68+
*/
69+
protected $databaseCreationService;
70+
6671
/**
6772
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
6873
*/
@@ -97,6 +102,7 @@ public function __construct(
97102
AllocationRepositoryInterface $allocationRepository,
98103
ConfigRepository $config,
99104
CreationService $service,
105+
\Pterodactyl\Services\Database\CreationService $databaseCreationService,
100106
DatabaseRepositoryInterface $databaseRepository,
101107
DatabaseHostRepository $databaseHostRepository,
102108
LocationRepositoryInterface $locationRepository,
@@ -106,6 +112,7 @@ public function __construct(
106112
) {
107113
$this->allocationRepository = $allocationRepository;
108114
$this->config = $config;
115+
$this->databaseCreationService = $databaseCreationService;
109116
$this->databaseRepository = $databaseRepository;
110117
$this->databaseHostRepository = $databaseHostRepository;
111118
$this->locationRepository = $locationRepository;
@@ -583,20 +590,25 @@ public function saveStartup(Request $request, $id)
583590
*/
584591
public function newDatabase(Request $request, $id)
585592
{
586-
$repo = new DatabaseRepository;
587-
588-
try {
589-
$repo->create($id, $request->only(['host', 'database', 'connection']));
590-
591-
Alert::success('A new database was assigned to this server successfully.')->flash();
592-
} catch (DisplayValidationException $ex) {
593-
return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
594-
} catch (DisplayException $ex) {
595-
Alert::danger($ex->getMessage())->flash();
596-
} catch (\Exception $ex) {
597-
Log::error($ex);
598-
Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
599-
}
593+
$this->databaseCreationService->create($id, [
594+
'database' => $request->input('database'),
595+
'remote' => $request->input('remote'),
596+
'database_host_id' => $request->input('database_host_id'),
597+
]);
598+
// $repo = new DatabaseRepository;
599+
//
600+
// try {
601+
// $repo->create($id, $request->only(['host', 'database', 'connection']));
602+
//
603+
// Alert::success('A new database was assigned to this server successfully.')->flash();
604+
// } catch (DisplayValidationException $ex) {
605+
// return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
606+
// } catch (DisplayException $ex) {
607+
// Alert::danger($ex->getMessage())->flash();
608+
// } catch (\Exception $ex) {
609+
// Log::error($ex);
610+
// Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
611+
// }
600612

601613
return redirect()->route('admin.servers.view.database', $id)->withInput();
602614
}

app/Http/Controllers/Admin/UserController.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2929
use Pterodactyl\Models\User;
3030
use Prologue\Alerts\AlertsMessageBag;
31-
use Pterodactyl\Services\Administrative\UserService;
31+
use Pterodactyl\Services\UserService;
3232
use Pterodactyl\Exceptions\DisplayException;
3333
use Pterodactyl\Http\Controllers\Controller;
3434
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
@@ -41,7 +41,7 @@ class UserController extends Controller
4141
protected $alert;
4242

4343
/**
44-
* @var \Pterodactyl\Services\Administrative\UserService
44+
* @var \Pterodactyl\Services\UserService
4545
*/
4646
protected $service;
4747

@@ -59,7 +59,7 @@ class UserController extends Controller
5959
* UserController constructor.
6060
*
6161
* @param \Prologue\Alerts\AlertsMessageBag $alert
62-
* @param \Pterodactyl\Services\Administrative\UserService $service
62+
* @param \Pterodactyl\Services\UserService $service
6363
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
6464
* @param \Pterodactyl\Models\User $model
6565
*/
@@ -178,17 +178,11 @@ public function update(UserFormRequest $request, User $user)
178178
/**
179179
* Get a JSON response of users on the system.
180180
*
181-
* @param \Illuminate\Http\Request $request
182-
* @return \Pterodactyl\Models\User
181+
* @param \Illuminate\Http\Request $request
182+
* @return \Illuminate\Database\Eloquent\Collection
183183
*/
184184
public function json(Request $request)
185185
{
186-
return $this->model->search($request->input('q'))->all([
187-
'id', 'email', 'username', 'name_first', 'name_last',
188-
])->transform(function ($item) {
189-
$item->md5 = md5(strtolower($item->email));
190-
191-
return $item;
192-
});
186+
return $this->repository->filterUsersByQuery($request->input('q'));
193187
}
194188
}

app/Http/Requests/Admin/DatabaseHostFormRequest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,10 @@ class DatabaseHostFormRequest extends AdminFormRequest
3333
*/
3434
public function rules()
3535
{
36-
$this->merge([
37-
'node_id' => ($this->input('node_id') < 1) ? null : $this->input('node_id'),
38-
'host' => gethostbyname($this->input('host')),
39-
]);
40-
41-
$rules = app()->make(DatabaseHost::class)->getRules();
42-
43-
if ($this->method() === 'PATCH') {
44-
$rules['host'] = $rules['host'] . ',' . $this->route()->parameter('host')->id;
36+
if ($this->method() !== 'POST') {
37+
return DatabaseHost::getUpdateRulesForId($this->route()->parameter('host')->id);
4538
}
4639

47-
return $rules;
40+
return DatabaseHost::getCreateRules();
4841
}
4942
}

app/Models/Database.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424

2525
namespace Pterodactyl\Models;
2626

27-
use Illuminate\Database\Eloquent\Model;
2827
use Sofa\Eloquence\Eloquence;
2928
use Sofa\Eloquence\Validable;
29+
use Illuminate\Database\Eloquent\Model;
30+
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
3031

31-
class Database extends Model
32+
class Database extends Model implements ValidableContract
3233
{
3334
use Eloquence, Validable;
3435

@@ -52,7 +53,7 @@ class Database extends Model
5253
* @var array
5354
*/
5455
protected $fillable = [
55-
'server_id', 'database_host_id', 'database', 'username', 'remote',
56+
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote',
5657
];
5758

5859
/**

app/Models/DatabaseHost.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
use Sofa\Eloquence\Eloquence;
2828
use Sofa\Eloquence\Validable;
2929
use Illuminate\Database\Eloquent\Model;
30+
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
3031

31-
class DatabaseHost extends Model
32+
class DatabaseHost extends Model implements ValidableContract
3233
{
3334
use Eloquence, Validable;
3435

@@ -52,7 +53,7 @@ class DatabaseHost extends Model
5253
* @var array
5354
*/
5455
protected $fillable = [
55-
'name', 'host', 'port', 'username', 'max_databases', 'node_id',
56+
'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id',
5657
];
5758

5859
/**
@@ -79,11 +80,12 @@ class DatabaseHost extends Model
7980
'node_id' => 'sometimes|required',
8081
];
8182

82-
/**
83-
* Validation rules to assign to this model.
84-
*
85-
* @var array
86-
*/
83+
/**
84+
* Validation rules to assign to this model.
85+
*
86+
* @var array
87+
*/
88+
// @todo the node_id field doesn't validate correctly if no node is provided in request
8789
protected static $dataIntegrityRules = [
8890
'name' => 'string|max:255',
8991
'host' => 'ip|unique:database_hosts,host',

app/Repositories/Eloquent/DatabaseRepository.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@
2424

2525
namespace Pterodactyl\Repositories\Eloquent;
2626

27+
use Illuminate\Database\DatabaseManager;
2728
use Pterodactyl\Models\Database;
2829
use Illuminate\Foundation\Application;
29-
use Illuminate\Database\ConnectionResolver;
3030
use Pterodactyl\Exceptions\DisplayException;
3131
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
3232

3333
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
3434
{
3535
/**
36-
* @var \Illuminate\Database\ConnectionResolverInterface
36+
* @var \Illuminate\Database\DatabaseManager
3737
*/
38-
protected $connection;
38+
protected $database;
3939

4040
/**
4141
* DatabaseRepository constructor.
4242
*
43-
* @param \Illuminate\Foundation\Application $application
44-
* @param \Illuminate\Database\ConnectionResolver $connection
43+
* @param \Illuminate\Foundation\Application $application
44+
* @param \Illuminate\Database\DatabaseManager $database
4545
*/
4646
public function __construct(
4747
Application $application,
48-
ConnectionResolver $connection
48+
DatabaseManager $database
4949
) {
5050
parent::__construct($application);
5151

52-
$this->connection = $connection;
52+
$this->database = $database;
5353
}
5454

5555
/**
@@ -150,6 +150,6 @@ public function dropUser($username, $remote, $connection = null)
150150
*/
151151
protected function runStatement($statement, $connection = null)
152152
{
153-
return $this->connection->connection($connection)->statement($statement);
153+
return $this->database->connection($connection)->statement($statement);
154154
}
155155
}

app/Repositories/Eloquent/UserRepository.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,22 @@ public function deleteIfNoServers($id)
9393

9494
return $user->delete();
9595
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function filterUsersByQuery($query)
101+
{
102+
$this->withColumns([
103+
'id', 'email', 'username', 'name_first', 'name_last',
104+
]);
105+
106+
$instance = $this->getBuilder()->search($query)->get($this->getColumns());
107+
108+
return $instance->transform(function ($item) {
109+
$item->md5 = md5(strtolower($item->email));
110+
111+
return $item;
112+
});
113+
}
96114
}

app/Services/Database/CreationService.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@
2424

2525
namespace Pterodactyl\Services\Database;
2626

27-
use Illuminate\Database\ConnectionResolver;
28-
use Illuminate\Database\ConnectionInterface;
27+
use Illuminate\Database\DatabaseManager;
2928
use Illuminate\Contracts\Encryption\Encrypter;
3029
use Pterodactyl\Extensions\DynamicDatabaseConnection;
3130
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
3231

3332
class CreationService
3433
{
3534
/**
36-
* @var \Illuminate\Database\ConnectionResolver
37-
*/
38-
protected $connection;
39-
40-
/**
41-
* @var \Illuminate\Database\ConnectionInterface
35+
* @var \Illuminate\Database\DatabaseManager
4236
*/
4337
protected $database;
4438

@@ -60,20 +54,17 @@ class CreationService
6054
/**
6155
* CreationService constructor.
6256
*
63-
* @param \Illuminate\Database\ConnectionInterface $database
64-
* @param \Illuminate\Database\ConnectionResolver $connection
57+
* @param \Illuminate\Database\DatabaseManager $database
6558
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
6659
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
6760
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
6861
*/
6962
public function __construct(
70-
ConnectionInterface $database,
71-
ConnectionResolver $connection,
63+
DatabaseManager $database,
7264
DynamicDatabaseConnection $dynamic,
7365
DatabaseRepositoryInterface $repository,
7466
Encrypter $encrypter
7567
) {
76-
$this->connection = $connection;
7768
$this->database = $database;
7869
$this->dynamic = $dynamic;
7970
$this->encrypter = $encrypter;
@@ -83,17 +74,19 @@ public function __construct(
8374
/**
8475
* Create a new database that is linked to a specific host.
8576
*
77+
* @param int $server
8678
* @param array $data
8779
* @return \Illuminate\Database\Eloquent\Model
8880
*
8981
* @throws \Exception
9082
* @throws \Pterodactyl\Exceptions\DisplayException
9183
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
9284
*/
93-
public function create(array $data)
85+
public function create($server, array $data)
9486
{
95-
$data['database'] = sprintf('d%d_%s', $data['server_id'], $data['database']);
96-
$data['username'] = sprintf('u%d_%s', $data['server_id'], str_random(10));
87+
$data['server_id'] = $server;
88+
$data['database'] = sprintf('d%d_%s', $server, $data['database']);
89+
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
9790
$data['password'] = $this->encrypter->encrypt(str_random(16));
9891

9992
$this->database->beginTransaction();

0 commit comments

Comments
 (0)