Skip to content

Commit 0111ca7

Browse files
committed
Push more changes to DBHost service.
Currently updating via the frontend is broken if you don't provide an actual node to attach it to.
1 parent cede747 commit 0111ca7

File tree

10 files changed

+459
-100
lines changed

10 files changed

+459
-100
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Extensions;
26+
27+
use Illuminate\Config\Repository as ConfigRepository;
28+
use Illuminate\Contracts\Encryption\Encrypter;
29+
use Pterodactyl\Models\DatabaseHost;
30+
31+
class DynamicDatabaseConnection
32+
{
33+
const DB_CHARSET = 'utf8';
34+
const DB_COLLATION = 'utf8_unicode_ci';
35+
const DB_DRIVER = 'mysql';
36+
37+
/**
38+
* @var \Illuminate\Config\Repository
39+
*/
40+
protected $config;
41+
42+
/**
43+
* @var \Illuminate\Contracts\Encryption\Encrypter
44+
*/
45+
protected $encrypter;
46+
47+
/**
48+
* @var \Pterodactyl\Models\DatabaseHost
49+
*/
50+
protected $model;
51+
52+
/**
53+
* DynamicDatabaseConnection constructor.
54+
*
55+
* @param \Illuminate\Config\Repository $config
56+
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
57+
* @param \Pterodactyl\Models\DatabaseHost $model
58+
*/
59+
public function __construct(
60+
ConfigRepository $config,
61+
Encrypter $encrypter,
62+
DatabaseHost $model
63+
) {
64+
$this->config = $config;
65+
$this->encrypter = $encrypter;
66+
$this->model = $model;
67+
}
68+
69+
/**
70+
* Adds a dynamic database connection entry to the runtime config.
71+
*
72+
* @param string $connection
73+
* @param \Pterodactyl\Models\DatabaseHost|int $host
74+
* @param string $database
75+
*/
76+
public function set($connection, $host, $database = 'mysql')
77+
{
78+
if (! $host instanceof DatabaseHost) {
79+
$host = $this->model->findOrFail($host);
80+
}
81+
82+
$this->config->set('database.connections.' . $connection, [
83+
'driver' => self::DB_DRIVER,
84+
'host' => $host->host,
85+
'port' => $host->port,
86+
'database' => $database,
87+
'username' => $host->username,
88+
'password' => $this->encrypter->decrypt($host->password),
89+
'charset' => self::DB_CHARSET,
90+
'collation' => self::DB_COLLATION,
91+
]);
92+
}
93+
}

app/Http/Controllers/Admin/BaseController.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,60 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Alert;
28-
use Settings;
29-
use Validator;
30-
use Illuminate\Http\Request;
27+
use Krucas\Settings\Settings;
28+
use Prologue\Alerts\AlertsMessageBag;
3129
use Pterodactyl\Http\Controllers\Controller;
30+
use Pterodactyl\Http\Requests\Admin\BaseFormRequest;
3231

3332
class BaseController extends Controller
3433
{
34+
/**
35+
* @var \Prologue\Alerts\AlertsMessageBag
36+
*/
37+
protected $alert;
38+
39+
/**
40+
* @var \Krucas\Settings\Settings
41+
*/
42+
protected $settings;
43+
44+
public function __construct(AlertsMessageBag $alert, Settings $settings)
45+
{
46+
$this->alert = $alert;
47+
$this->settings = $settings;
48+
}
49+
3550
/**
3651
* Return the admin index view.
3752
*
38-
* @param \Illuminate\Http\Request $request
3953
* @return \Illuminate\View\View
4054
*/
41-
public function getIndex(Request $request)
55+
public function getIndex()
4256
{
4357
return view('admin.index');
4458
}
4559

4660
/**
4761
* Return the admin settings view.
4862
*
49-
* @param \Illuminate\Http\Request $request
5063
* @return \Illuminate\View\View
5164
*/
52-
public function getSettings(Request $request)
65+
public function getSettings()
5366
{
5467
return view('admin.settings');
5568
}
5669

5770
/**
5871
* Handle settings post request.
5972
*
60-
* @param \Illuminate\Http\Request $request
73+
* @param \Pterodactyl\Http\Requests\Admin\BaseFormRequest $request
6174
* @return \Illuminate\Http\RedirectResponse
6275
*/
63-
public function postSettings(Request $request)
76+
public function postSettings(BaseFormRequest $request)
6477
{
65-
$validator = Validator::make($request->all(), [
66-
'company' => 'required|between:1,256',
67-
// 'default_language' => 'required|alpha_dash|min:2|max:5',
68-
]);
69-
70-
if ($validator->fails()) {
71-
return redirect()->route('admin.settings')->withErrors($validator->errors())->withInput();
72-
}
73-
74-
Settings::set('company', $request->input('company'));
75-
// Settings::set('default_language', $request->input('default_language'));
78+
$this->settings->set('company', $request->input('company'));
7679

77-
Alert::success('Settings have been successfully updated.')->flash();
80+
$this->alert->success('Settings have been successfully updated.')->flash();
7881

7982
return redirect()->route('admin.settings');
8083
}

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,112 +24,144 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Log;
28-
use Alert;
29-
use Illuminate\Http\Request;
30-
use Pterodactyl\Models\Database;
3127
use Pterodactyl\Models\Location;
3228
use Pterodactyl\Models\DatabaseHost;
33-
use Pterodactyl\Exceptions\DisplayException;
29+
use Prologue\Alerts\AlertsMessageBag;
3430
use Pterodactyl\Http\Controllers\Controller;
35-
use Pterodactyl\Repositories\DatabaseRepository;
36-
use Pterodactyl\Exceptions\DisplayValidationException;
31+
use Pterodactyl\Services\DatabaseHostService;
32+
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
3733

3834
class DatabaseController extends Controller
3935
{
36+
/**
37+
* @var \Prologue\Alerts\AlertsMessageBag
38+
*/
39+
protected $alert;
40+
41+
/**
42+
* @var \Pterodactyl\Models\DatabaseHost
43+
*/
44+
protected $hostModel;
45+
46+
/**
47+
* @var \Pterodactyl\Models\Location
48+
*/
49+
protected $locationModel;
50+
51+
/**
52+
* @var \Pterodactyl\Services\DatabaseHostService
53+
*/
54+
protected $service;
55+
56+
/**
57+
* DatabaseController constructor.
58+
*
59+
* @param \Prologue\Alerts\AlertsMessageBag $alert
60+
* @param \Pterodactyl\Models\DatabaseHost $hostModel
61+
* @param \Pterodactyl\Models\Location $locationModel
62+
* @param \Pterodactyl\Services\DatabaseHostService $service
63+
*/
64+
public function __construct(
65+
AlertsMessageBag $alert,
66+
DatabaseHost $hostModel,
67+
Location $locationModel,
68+
DatabaseHostService $service
69+
) {
70+
$this->alert = $alert;
71+
$this->hostModel = $hostModel;
72+
$this->locationModel = $locationModel;
73+
$this->service = $service;
74+
}
75+
4076
/**
4177
* Display database host index.
4278
*
43-
* @param \Illuminate\Http\Request $request
4479
* @return \Illuminate\View\View
4580
*/
46-
public function index(Request $request)
81+
public function index()
4782
{
4883
return view('admin.databases.index', [
49-
'locations' => Location::with('nodes')->get(),
50-
'hosts' => DatabaseHost::withCount('databases')->with('node')->get(),
84+
'locations' => $this->locationModel->with('nodes')->get(),
85+
'hosts' => $this->hostModel->withCount('databases')->with('node')->get(),
5186
]);
5287
}
5388

5489
/**
5590
* Display database host to user.
5691
*
57-
* @param \Illuminate\Http\Request $request
58-
* @param int $id
92+
* @param \Pterodactyl\Models\DatabaseHost $host
5993
* @return \Illuminate\View\View
6094
*/
61-
public function view(Request $request, $id)
95+
public function view(DatabaseHost $host)
6296
{
97+
$host->load('databases.server');
98+
6399
return view('admin.databases.view', [
64-
'locations' => Location::with('nodes')->get(),
65-
'host' => DatabaseHost::with('databases.server')->findOrFail($id),
100+
'locations' => $this->locationModel->with('nodes')->get(),
101+
'host' => $host,
66102
]);
67103
}
68104

69105
/**
70-
* Handle post request to create database host.
106+
* Handle request to create a new database host.
71107
*
72-
* @param \Illuminate\Http\Request $request
108+
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
73109
* @return \Illuminate\Http\RedirectResponse
110+
*
111+
* @throws \Throwable
74112
*/
75-
public function create(Request $request)
113+
public function create(DatabaseHostFormRequest $request)
76114
{
77-
$repo = new DatabaseRepository;
78-
79115
try {
80-
$host = $repo->add($request->intersect([
81-
'name', 'username', 'password',
82-
'host', 'port', 'node_id',
83-
]));
84-
Alert::success('Successfully created new database host on the system.')->flash();
116+
$host = $this->service->create($request->normalize());
117+
$this->alert->success('Successfully created a new database host on the system.')->flash();
85118

86119
return redirect()->route('admin.databases.view', $host->id);
87120
} catch (\PDOException $ex) {
88-
Alert::danger($ex->getMessage())->flash();
89-
} catch (DisplayValidationException $ex) {
90-
return redirect()->route('admin.databases')->withErrors(json_decode($ex->getMessage()));
91-
} catch (\Exception $ex) {
92-
Log::error($ex);
93-
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash();
121+
$this->alert->danger($ex->getMessage())->flash();
94122
}
95123

96124
return redirect()->route('admin.databases');
97125
}
98126

99127
/**
100-
* Handle post request to update a database host.
128+
* Handle updating database host.
101129
*
102-
* @param \Illuminate\Http\Request $request
103-
* @param int $id
130+
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
131+
* @param \Pterodactyl\Models\DatabaseHost $host
104132
* @return \Illuminate\Http\RedirectResponse
133+
*
134+
* @throws \Pterodactyl\Exceptions\DisplayException
105135
*/
106-
public function update(Request $request, $id)
136+
public function update(DatabaseHostFormRequest $request, DatabaseHost $host)
107137
{
108-
$repo = new DatabaseRepository;
138+
if ($request->input('action') === 'delete') {
139+
return $this->delete($host);
140+
}
109141

110142
try {
111-
if ($request->input('action') !== 'delete') {
112-
$host = $repo->update($id, $request->intersect([
113-
'name', 'username', 'password',
114-
'host', 'port', 'node_id',
115-
]));
116-
Alert::success('Database host was updated successfully.')->flash();
117-
} else {
118-
$repo->delete($id);
119-
120-
return redirect()->route('admin.databases');
121-
}
143+
$host = $this->service->update($host->id, $request->normalize());
144+
$this->alert->success('Database host was updated successfully.')->flash();
122145
} catch (\PDOException $ex) {
123-
Alert::danger($ex->getMessage())->flash();
124-
} catch (DisplayException $ex) {
125-
Alert::danger($ex->getMessage())->flash();
126-
} catch (DisplayValidationException $ex) {
127-
return redirect()->route('admin.databases.view', $id)->withErrors(json_decode($ex->getMessage()));
128-
} catch (\Exception $ex) {
129-
Log::error($ex);
130-
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash();
146+
$this->alert->danger($ex->getMessage())->flash();
131147
}
132148

133-
return redirect()->route('admin.databases.view', $id);
149+
return redirect()->route('admin.databases.view', $host->id);
150+
}
151+
152+
/**
153+
* Handle request to delete a database host.
154+
*
155+
* @param \Pterodactyl\Models\DatabaseHost $host
156+
* @return \Illuminate\Http\RedirectResponse
157+
*
158+
* @throws \Pterodactyl\Exceptions\DisplayException
159+
*/
160+
public function delete(DatabaseHost $host)
161+
{
162+
$this->service->delete($host->id);
163+
$this->alert->success('The requested database host has been deleted from the system.')->flash();
164+
165+
return redirect()->route('admin.databases');
134166
}
135167
}

0 commit comments

Comments
 (0)