Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/DatabaseController.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin;
4
5
use Illuminate\View\View;
6
use Pterodactyl\Models\DatabaseHost;
7
use Illuminate\Http\RedirectResponse;
8
use Prologue\Alerts\AlertsMessageBag;
9
use Pterodactyl\Http\Controllers\Controller;
10
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
11
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
12
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
13
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
14
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
15
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
16
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
17
18
class DatabaseController extends Controller
19
{
20
/**
21
* DatabaseController constructor.
22
*/
23
public function __construct(
24
private AlertsMessageBag $alert,
25
private DatabaseHostRepositoryInterface $repository,
26
private DatabaseRepositoryInterface $databaseRepository,
27
private HostCreationService $creationService,
28
private HostDeletionService $deletionService,
29
private HostUpdateService $updateService,
30
private LocationRepositoryInterface $locationRepository,
31
) {
32
}
33
34
/**
35
* Display database host index.
36
*/
37
public function index(): View
38
{
39
return view('admin.databases.index', [
40
'locations' => $this->locationRepository->getAllWithNodes(),
41
'hosts' => $this->repository->getWithViewDetails(),
42
]);
43
}
44
45
/**
46
* Display database host to user.
47
*
48
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
49
*/
50
public function view(int $host): View
51
{
52
return view('admin.databases.view', [
53
'locations' => $this->locationRepository->getAllWithNodes(),
54
'host' => $this->repository->find($host),
55
'databases' => $this->databaseRepository->getDatabasesForHost($host),
56
]);
57
}
58
59
/**
60
* Handle request to create a new database host.
61
*
62
* @throws \Throwable
63
*/
64
public function create(DatabaseHostFormRequest $request): RedirectResponse
65
{
66
try {
67
$host = $this->creationService->handle($request->normalize());
68
} catch (\Exception $exception) {
69
if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {
70
$this->alert->danger(
71
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
72
)->flash();
73
74
return redirect()->route('admin.databases')->withInput($request->validated());
75
} else {
76
throw $exception;
77
}
78
}
79
80
$this->alert->success('Successfully created a new database host on the system.')->flash();
81
82
return redirect()->route('admin.databases.view', $host->id);
83
}
84
85
/**
86
* Handle updating database host.
87
*
88
* @throws \Throwable
89
*/
90
public function update(DatabaseHostFormRequest $request, DatabaseHost $host): RedirectResponse
91
{
92
$redirect = redirect()->route('admin.databases.view', $host->id);
93
94
try {
95
$this->updateService->handle($host->id, $request->normalize());
96
$this->alert->success('Database host was updated successfully.')->flash();
97
} catch (\Exception $exception) {
98
// Catch any SQL related exceptions and display them back to the user, otherwise just
99
// throw the exception like normal and move on with it.
100
if ($exception instanceof \PDOException || $exception->getPrevious() instanceof \PDOException) {
101
$this->alert->danger(
102
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
103
)->flash();
104
105
return $redirect->withInput($request->normalize());
106
} else {
107
throw $exception;
108
}
109
}
110
111
return $redirect;
112
}
113
114
/**
115
* Handle request to delete a database host.
116
*
117
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
118
*/
119
public function delete(int $host): RedirectResponse
120
{
121
$this->deletionService->handle($host);
122
$this->alert->success('The requested database host has been deleted from the system.')->flash();
123
124
return redirect()->route('admin.databases');
125
}
126
}
127
128