Skip to content

Commit 198a021

Browse files
committed
Add database host management to panel.
1 parent 5bbded2 commit 198a021

File tree

12 files changed

+602
-122
lines changed

12 files changed

+602
-122
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1313
* Table seeders for services now can be run during upgrades and will attempt to locate and update, or create new if not found in the database.
1414
* Many structural changes to the database and `Pterodactyl\Models` classes that would flood this changelog if they were all included. All required migrations included to handle database changes.
1515
* `[pre.4]` — Service pack files are now stored in the database rather than on the host system to make updates easier.
16+
* Clarified details for database hosts to prevent users entering invalid account details, as well as renamed tables and columns relating to it to keep things clearer.
1617

1718
### Fixed
1819
* Fixes potential bug with invalid CIDR notation (ex: `192.168.1.1/z`) when adding allocations that could cause over 4 million records to be created at once.
@@ -22,6 +23,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2223
### Added
2324
* Ability to assign multiple allocations at once when creating a new server.
2425
* New `humanReadable` macro on `File` facade that accepts a file path and returns a human readable size. (`File::humanReadable(path, precision)`)
26+
* Added ability to edit database host details after creation on the system.
2527

2628
### Deprecated
2729
* Old API calls to `Server::create` will fail due to changed data structure.

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
use Log;
2828
use Alert;
29-
use Pterodactyl\Models;
3029
use Illuminate\Http\Request;
30+
use Pterodactyl\Models\Database;
31+
use Pterodactyl\Models\Location;
32+
use Pterodactyl\Models\DatabaseHost;
3133
use Pterodactyl\Exceptions\DisplayException;
3234
use Pterodactyl\Http\Controllers\Controller;
3335
use Pterodactyl\Repositories\DatabaseRepository;
@@ -36,82 +38,98 @@
3638
class DatabaseController extends Controller
3739
{
3840
/**
39-
* Controller Constructor.
41+
* Display database host index.
42+
*
43+
* @param Request $request
44+
* @return \Illuminate\View\View
4045
*/
41-
public function __construct()
42-
{
43-
//
44-
}
45-
46-
public function getIndex(Request $request)
46+
public function index(Request $request)
4747
{
4848
return view('admin.databases.index', [
49-
'databases' => Models\Database::with('server')->paginate(50),
50-
'hosts' => Models\DatabaseServer::withCount('databases')->with('node')->paginate(20),
49+
'locations' => Location::with('nodes')->get(),
50+
'hosts' => DatabaseHost::withCount('databases')->with('node')->get(),
5151
]);
5252
}
5353

54-
public function getNew(Request $request)
54+
/**
55+
* Display database host to user.
56+
*
57+
* @param Request $request
58+
* @param int $id
59+
* @return \Illuminate\View\View
60+
*/
61+
public function view(Request $request, $id)
5562
{
56-
return view('admin.databases.new', [
57-
'nodes' => Models\Node::all()->load('location'),
63+
return view('admin.databases.view', [
64+
'locations' => Location::with('nodes')->get(),
65+
'host' => DatabaseHost::with('databases.server')->findOrFail($id),
5866
]);
5967
}
6068

61-
public function postNew(Request $request)
69+
/**
70+
* Handle post request to create database host.
71+
*
72+
* @param Request $request
73+
* @return \Illuminate\Response\RedirectResponse
74+
*/
75+
public function create(Request $request)
6276
{
77+
$repo = new DatabaseRepository;
78+
6379
try {
64-
$repo = new DatabaseRepository;
65-
$repo->add($request->only([
66-
'name',
67-
'host',
68-
'port',
69-
'username',
70-
'password',
71-
'linked_node',
80+
$host = $repo->add($request->intersect([
81+
'name', 'username', 'password',
82+
'host', 'port', 'node_id',
7283
]));
73-
Alert::success('Successfully added a new database server to the system.')->flash();
84+
Alert::success('Successfully created new database host on the system.')->flash();
7485

75-
return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']);
86+
return redirect()->route('admin.databases.view', $host->id);
87+
} catch (\PDOException $ex) {
88+
Alert::danger($ex->getMessage())->flash();
7689
} catch (DisplayValidationException $ex) {
77-
return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput();
78-
} catch (\Exception $ex) {
79-
if ($ex instanceof DisplayException || $ex instanceof \PDOException) {
80-
Alert::danger($ex->getMessage())->flash();
81-
} else {
82-
Log::error($ex);
83-
Alert::danger('An error occurred while attempting to delete this database server from the system.')->flash();
84-
}
85-
86-
return redirect()->route('admin.databases.new')->withInput();
87-
}
88-
}
89-
90-
public function deleteDatabase(Request $request, $id)
91-
{
92-
try {
93-
$repo = new DatabaseRepository;
94-
$repo->drop($id);
90+
return redirect()->route('admin.databases')->withErrors(json_decode($ex->getMessage()));
9591
} catch (\Exception $ex) {
9692
Log::error($ex);
97-
98-
return response()->json([
99-
'error' => ($ex instanceof DisplayException) ? $ex->getMessage() : 'An error occurred while attempting to delete this database from the system.',
100-
], 500);
93+
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash();
10194
}
95+
96+
return redirect()->route('admin.databases');
10297
}
10398

104-
public function deleteServer(Request $request, $id)
99+
/**
100+
* Handle post request to update a database host.
101+
*
102+
* @param Request $request
103+
* @param int $id
104+
* @return \Illuminate\Response\RedirectResponse
105+
*/
106+
public function update(Request $request, $id)
105107
{
108+
$repo = new DatabaseRepository;
109+
106110
try {
107-
$repo = new DatabaseRepository;
108-
$repo->delete($id);
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+
}
122+
} 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()));
109128
} catch (\Exception $ex) {
110129
Log::error($ex);
111-
112-
return response()->json([
113-
'error' => ($ex instanceof DisplayException) ? $ex->getMessage() : 'An error occurred while attempting to delete this database server from the system.',
114-
], 500);
130+
Alert::danger('An error was encountered while trying to process this request. This error has been logged.')->flash();
115131
}
132+
133+
return redirect()->route('admin.databases.view', $id);
116134
}
117135
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function viewDatabase(Request $request, $id)
212212
$server = Models\Server::where('installed', 1)->with('databases.host')->findOrFail($id);
213213

214214
return view('admin.servers.view.database', [
215-
'hosts' => Models\DatabaseServer::all(),
215+
'hosts' => Models\DatabaseHost::all(),
216216
'server' => $server,
217217
]);
218218
}

app/Http/Routes/AdminRoutes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ public function map(Router $router)
4343
'uses' => 'Admin\BaseController@getIndex',
4444
]);
4545

46+
$router->group([
47+
'prefix' => 'admin/databases',
48+
'middleware' => [
49+
'auth',
50+
'admin',
51+
'csrf',
52+
],
53+
], function () use ($router) {
54+
$router->get('/', [
55+
'as' => 'admin.databases',
56+
'uses' => 'Admin\DatabaseController@index',
57+
]);
58+
59+
$router->post('/', 'Admin\DatabaseController@create');
60+
61+
$router->get('/view/{id}', [
62+
'as' => 'admin.databases.view',
63+
'uses' => 'Admin\DatabaseController@view',
64+
]);
65+
66+
$router->post('/view/{id}', 'Admin\DatabaseController@update');
67+
});
68+
4669
$router->group([
4770
'prefix' => 'admin/locations',
4871
'middleware' => [

app/Models/Database.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ class Database extends Model
4343
protected $hidden = ['password'];
4444

4545
/**
46-
* Fields that are not mass assignable.
46+
* Fields that are mass assignable.
4747
*
4848
* @var array
4949
*/
50-
protected $guarded = ['id', 'created_at', 'updated_at'];
50+
protected $fillable = [
51+
'server_id', 'database_host_id', 'database', 'username', 'remote',
52+
];
5153

5254
/**
5355
* Cast values to correct type.
@@ -56,7 +58,7 @@ class Database extends Model
5658
*/
5759
protected $casts = [
5860
'server_id' => 'integer',
59-
'db_server' => 'integer',
61+
'database_host_id' => 'integer',
6062
];
6163

6264
/**
@@ -66,7 +68,7 @@ class Database extends Model
6668
*/
6769
public function host()
6870
{
69-
return $this->belongsTo(DatabaseServer::class, 'db_server');
71+
return $this->belongsTo(DatabaseHost::class);
7072
}
7173

7274
/**
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626

2727
use Illuminate\Database\Eloquent\Model;
2828

29-
class DatabaseServer extends Model
29+
class DatabaseHost extends Model
3030
{
3131
/**
3232
* The table associated with the model.
3333
*
3434
* @var string
3535
*/
36-
protected $table = 'database_servers';
36+
protected $table = 'database_hosts';
3737

3838
/**
3939
* The attributes excluded from the model's JSON form.
@@ -43,11 +43,13 @@ class DatabaseServer extends Model
4343
protected $hidden = ['password'];
4444

4545
/**
46-
* Fields that are not mass assignable.
46+
* Fields that are mass assignable.
4747
*
4848
* @var array
4949
*/
50-
protected $guarded = ['id', 'created_at', 'updated_at'];
50+
protected $fillable = [
51+
'name', 'host', 'port', 'username', 'max_databases', 'node_id',
52+
];
5153

5254
/**
5355
* Cast values to correct type.
@@ -56,8 +58,8 @@ class DatabaseServer extends Model
5658
*/
5759
protected $casts = [
5860
'id' => 'integer',
59-
'server_id' => 'integer',
60-
'db_server' => 'integer',
61+
'max_databases' => 'integer',
62+
'node_id' => 'integer',
6163
];
6264

6365
/**
@@ -67,7 +69,7 @@ class DatabaseServer extends Model
6769
*/
6870
public function node()
6971
{
70-
return $this->belongsTo(Node::class, 'linked_node');
72+
return $this->belongsTo(Node::class);
7173
}
7274

7375
/**
@@ -77,6 +79,6 @@ public function node()
7779
*/
7880
public function databases()
7981
{
80-
return $this->hasMany(Database::class, 'db_server');
82+
return $this->hasMany(Database::class);
8183
}
8284
}

0 commit comments

Comments
 (0)