Skip to content

Commit 97dc051

Browse files
committed
Add database management back to front-end and begin some refactoring
Here we go again boys...
1 parent 2b80de0 commit 97dc051

32 files changed

+772
-405
lines changed

app/Contracts/Repository/DatabaseRepositoryInterface.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,35 @@
99

1010
namespace Pterodactyl\Contracts\Repository;
1111

12+
use Illuminate\Support\Collection;
13+
1214
interface DatabaseRepositoryInterface extends RepositoryInterface
1315
{
16+
const DEFAULT_CONNECTION_NAME = 'dynamic';
17+
18+
/**
19+
* Set the connection name to execute statements against.
20+
*
21+
* @param string $connection
22+
* @return $this
23+
*/
24+
public function setConnection(string $connection);
25+
26+
/**
27+
* Return the connection to execute statements aganist.
28+
*
29+
* @return string
30+
*/
31+
public function getConnection(): string;
32+
33+
/**
34+
* Return all of the databases belonging to a server.
35+
*
36+
* @param int $server
37+
* @return \Illuminate\Support\Collection
38+
*/
39+
public function getDatabasesForServer(int $server): Collection;
40+
1441
/**
1542
* Create a new database if it does not already exist on the host with
1643
* the provided details.
@@ -26,58 +53,52 @@ public function createIfNotExists(array $data);
2653
/**
2754
* Create a new database on a given connection.
2855
*
29-
* @param string $database
30-
* @param null|string $connection
56+
* @param string $database
3157
* @return bool
3258
*/
33-
public function createDatabase($database, $connection = null);
59+
public function createDatabase($database);
3460

3561
/**
3662
* Create a new database user on a given connection.
3763
*
38-
* @param string $username
39-
* @param string $remote
40-
* @param string $password
41-
* @param null|string $connection
64+
* @param string $username
65+
* @param string $remote
66+
* @param string $password
4267
* @return bool
4368
*/
44-
public function createUser($username, $remote, $password, $connection = null);
69+
public function createUser($username, $remote, $password);
4570

4671
/**
4772
* Give a specific user access to a given database.
4873
*
49-
* @param string $database
50-
* @param string $username
51-
* @param string $remote
52-
* @param null|string $connection
74+
* @param string $database
75+
* @param string $username
76+
* @param string $remote
5377
* @return bool
5478
*/
55-
public function assignUserToDatabase($database, $username, $remote, $connection = null);
79+
public function assignUserToDatabase($database, $username, $remote);
5680

5781
/**
5882
* Flush the privileges for a given connection.
5983
*
60-
* @param null|string $connection
6184
* @return mixed
6285
*/
63-
public function flush($connection = null);
86+
public function flush();
6487

6588
/**
6689
* Drop a given database on a specific connection.
6790
*
68-
* @param string $database
69-
* @param null|string $connection
91+
* @param string $database
7092
* @return bool
7193
*/
72-
public function dropDatabase($database, $connection = null);
94+
public function dropDatabase($database);
7395

7496
/**
7597
* Drop a given user on a specific connection.
7698
*
77-
* @param string $username
78-
* @param string $remote
79-
* @param null|string $connection
99+
* @param string $username
100+
* @param string $remote
80101
* @return mixed
81102
*/
82-
public function dropUser($username, $remote, $connection = null);
103+
public function dropUser($username, $remote);
83104
}

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99

1010
namespace Pterodactyl\Http\Controllers\Admin;
1111

12-
use Pterodactyl\Models\DatabaseHost;
12+
use PDOException;
13+
use Illuminate\View\View;
14+
use Illuminate\Http\RedirectResponse;
1315
use Prologue\Alerts\AlertsMessageBag;
1416
use Pterodactyl\Http\Controllers\Controller;
15-
use Pterodactyl\Services\Database\DatabaseHostService;
17+
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
1618
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
19+
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
20+
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
1721
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
1822
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
1923

@@ -22,49 +26,65 @@ class DatabaseController extends Controller
2226
/**
2327
* @var \Prologue\Alerts\AlertsMessageBag
2428
*/
25-
protected $alert;
29+
private $alert;
30+
31+
/**
32+
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService
33+
*/
34+
private $creationService;
35+
36+
/**
37+
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService
38+
*/
39+
private $deletionService;
2640

2741
/**
2842
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
2943
*/
30-
protected $locationRepository;
44+
private $locationRepository;
3145

3246
/**
3347
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
3448
*/
35-
protected $repository;
49+
private $repository;
3650

3751
/**
38-
* @var \Pterodactyl\Services\Database\DatabaseHostService
52+
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService
3953
*/
40-
protected $service;
54+
private $updateService;
4155

4256
/**
4357
* DatabaseController constructor.
4458
*
4559
* @param \Prologue\Alerts\AlertsMessageBag $alert
4660
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
47-
* @param \Pterodactyl\Services\Database\DatabaseHostService $service
61+
* @param \Pterodactyl\Services\Databases\Hosts\HostCreationService $creationService
62+
* @param \Pterodactyl\Services\Databases\Hosts\HostDeletionService $deletionService
63+
* @param \Pterodactyl\Services\Databases\Hosts\HostUpdateService $updateService
4864
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
4965
*/
5066
public function __construct(
5167
AlertsMessageBag $alert,
5268
DatabaseHostRepositoryInterface $repository,
53-
DatabaseHostService $service,
69+
HostCreationService $creationService,
70+
HostDeletionService $deletionService,
71+
HostUpdateService $updateService,
5472
LocationRepositoryInterface $locationRepository
5573
) {
5674
$this->alert = $alert;
75+
$this->creationService = $creationService;
76+
$this->deletionService = $deletionService;
5777
$this->repository = $repository;
58-
$this->service = $service;
5978
$this->locationRepository = $locationRepository;
79+
$this->updateService = $updateService;
6080
}
6181

6282
/**
6383
* Display database host index.
6484
*
6585
* @return \Illuminate\View\View
6686
*/
67-
public function index()
87+
public function index(): View
6888
{
6989
return view('admin.databases.index', [
7090
'locations' => $this->locationRepository->getAllWithNodes(),
@@ -80,7 +100,7 @@ public function index()
80100
*
81101
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
82102
*/
83-
public function view($host)
103+
public function view($host): View
84104
{
85105
return view('admin.databases.view', [
86106
'locations' => $this->locationRepository->getAllWithNodes(),
@@ -94,42 +114,41 @@ public function view($host)
94114
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
95115
* @return \Illuminate\Http\RedirectResponse
96116
*
97-
* @throws \Throwable
117+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
118+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
98119
*/
99-
public function create(DatabaseHostFormRequest $request)
120+
public function create(DatabaseHostFormRequest $request): RedirectResponse
100121
{
101122
try {
102-
$host = $this->service->create($request->normalize());
103-
$this->alert->success('Successfully created a new database host on the system.')->flash();
104-
105-
return redirect()->route('admin.databases.view', $host->id);
106-
} catch (\PDOException $ex) {
123+
$host = $this->creationService->handle($request->normalize());
124+
} catch (PDOException $ex) {
107125
$this->alert->danger($ex->getMessage())->flash();
126+
127+
return redirect()->route('admin.databases');
108128
}
109129

110-
return redirect()->route('admin.databases');
130+
$this->alert->success('Successfully created a new database host on the system.')->flash();
131+
132+
return redirect()->route('admin.databases.view', $host->id);
111133
}
112134

113135
/**
114136
* Handle updating database host.
115137
*
116138
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
117-
* @param \Pterodactyl\Models\DatabaseHost $host
139+
* @param int $host
118140
* @return \Illuminate\Http\RedirectResponse
119141
*
120142
* @throws \Pterodactyl\Exceptions\DisplayException
121143
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
144+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
122145
*/
123-
public function update(DatabaseHostFormRequest $request, DatabaseHost $host)
146+
public function update(DatabaseHostFormRequest $request, int $host): RedirectResponse
124147
{
125-
if ($request->input('action') === 'delete') {
126-
return $this->delete($host);
127-
}
128-
129148
try {
130-
$host = $this->service->update($host->id, $request->normalize());
149+
$host = $this->updateService->handle($host, $request->normalize());
131150
$this->alert->success('Database host was updated successfully.')->flash();
132-
} catch (\PDOException $ex) {
151+
} catch (PDOException $ex) {
133152
$this->alert->danger($ex->getMessage())->flash();
134153
}
135154

@@ -139,14 +158,14 @@ public function update(DatabaseHostFormRequest $request, DatabaseHost $host)
139158
/**
140159
* Handle request to delete a database host.
141160
*
142-
* @param \Pterodactyl\Models\DatabaseHost $host
161+
* @param int $host
143162
* @return \Illuminate\Http\RedirectResponse
144163
*
145-
* @throws \Pterodactyl\Exceptions\DisplayException
164+
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
146165
*/
147-
public function delete(DatabaseHost $host)
166+
public function delete(int $host): RedirectResponse
148167
{
149-
$this->service->delete($host->id);
168+
$this->deletionService->handle($host);
150169
$this->alert->success('The requested database host has been deleted from the system.')->flash();
151170

152171
return redirect()->route('admin.databases');

app/Http/Controllers/Admin/ServersController.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
use Pterodactyl\Services\Servers\ReinstallServerService;
2323
use Pterodactyl\Services\Servers\ContainerRebuildService;
2424
use Pterodactyl\Services\Servers\BuildModificationService;
25-
use Pterodactyl\Services\Database\DatabaseManagementService;
25+
use Pterodactyl\Services\Databases\DatabasePasswordService;
2626
use Pterodactyl\Services\Servers\DetailsModificationService;
2727
use Pterodactyl\Services\Servers\StartupModificationService;
2828
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
2929
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
3030
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
31+
use Pterodactyl\Services\Databases\DatabaseManagementService;
3132
use Illuminate\Contracts\Config\Repository as ConfigRepository;
3233
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
3334
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@@ -67,10 +68,15 @@ class ServersController extends Controller
6768
protected $databaseRepository;
6869

6970
/**
70-
* @var \Pterodactyl\Services\Database\DatabaseManagementService
71+
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
7172
*/
7273
protected $databaseManagementService;
7374

75+
/**
76+
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
77+
*/
78+
protected $databasePasswordService;
79+
7480
/**
7581
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
7682
*/
@@ -135,7 +141,8 @@ class ServersController extends Controller
135141
* @param \Illuminate\Contracts\Config\Repository $config
136142
* @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService
137143
* @param \Pterodactyl\Services\Servers\ServerCreationService $service
138-
* @param \Pterodactyl\Services\Database\DatabaseManagementService $databaseManagementService
144+
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
145+
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService
139146
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
140147
* @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository
141148
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
@@ -156,6 +163,7 @@ public function __construct(
156163
ContainerRebuildService $containerRebuildService,
157164
ServerCreationService $service,
158165
DatabaseManagementService $databaseManagementService,
166+
DatabasePasswordService $databasePasswordService,
159167
DatabaseRepositoryInterface $databaseRepository,
160168
DatabaseHostRepository $databaseHostRepository,
161169
ServerDeletionService $deletionService,
@@ -173,9 +181,10 @@ public function __construct(
173181
$this->buildModificationService = $buildModificationService;
174182
$this->config = $config;
175183
$this->containerRebuildService = $containerRebuildService;
184+
$this->databaseHostRepository = $databaseHostRepository;
176185
$this->databaseManagementService = $databaseManagementService;
186+
$this->databasePasswordService = $databasePasswordService;
177187
$this->databaseRepository = $databaseRepository;
178-
$this->databaseHostRepository = $databaseHostRepository;
179188
$this->detailsModificationService = $detailsModificationService;
180189
$this->deletionService = $deletionService;
181190
$this->locationRepository = $locationRepository;
@@ -609,7 +618,7 @@ public function resetDatabasePassword(Request $request, $server)
609618
['id', '=', $request->input('database')],
610619
]);
611620

612-
$this->databaseManagementService->changePassword($database->id, str_random(20));
621+
$this->databasePasswordService->handle($database, str_random(20));
613622

614623
return response('', 204);
615624
}

0 commit comments

Comments
 (0)