Skip to content

Commit a4d7170

Browse files
committed
Don't allow creation of a database with an identical name for the same server; closes pterodactyl#2447
1 parent 7b0f998 commit a4d7170

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Databases;
44

5+
use Webmozart\Assert\Assert;
6+
use Pterodactyl\Models\Server;
7+
use Illuminate\Validation\Rule;
58
use Pterodactyl\Models\Permission;
9+
use Illuminate\Database\Query\Builder;
610
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
711
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
812

@@ -21,9 +25,31 @@ public function permission(): string
2125
*/
2226
public function rules(): array
2327
{
28+
$server = $this->route()->parameter('server');
29+
30+
Assert::isInstanceOf($server, Server::class);
31+
2432
return [
25-
'database' => 'required|alpha_dash|min:3|max:48',
33+
'database' => [
34+
'required',
35+
'alpha_dash',
36+
'min:3',
37+
'max:48',
38+
// Yes, I am aware that you could have the same database name across two unique hosts. However,
39+
// I don't really care about that for this validation. We just want to make sure it is unique to
40+
// the server itself. No need for complexity.
41+
Rule::unique('databases', 'database')->where(function (Builder $query) use ($server) {
42+
$query->where('server_id', $server->id);
43+
}),
44+
],
2645
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
2746
];
2847
}
48+
49+
public function messages()
50+
{
51+
return [
52+
'database.unique' => 'The database name you have selected is already in use by this server.',
53+
];
54+
}
2955
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class ChangeUniqueDatabaseNameToAccountForServer extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('databases', function (Blueprint $table) {
17+
$table->dropUnique(['database_host_id', 'database']);
18+
});
19+
20+
Schema::table('databases', function (Blueprint $table) {
21+
$table->unique(['database_host_id', 'server_id', 'database']);
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::table('databases', function (Blueprint $table) {
33+
$table->dropUnique(['database_host_id', 'server_id', 'database']);
34+
});
35+
36+
Schema::table('databases', function (Blueprint $table) {
37+
$table->unique(['database_host_id', 'database']);
38+
});
39+
40+
}
41+
}

0 commit comments

Comments
 (0)