Skip to content

Commit 72ecac5

Browse files
authored
Merge pull request pterodactyl#1963 from Sir3lit/maxconn
Add Max Concurrent Connections for database users
2 parents 0385d2a + 56a0989 commit 72ecac5

File tree

12 files changed

+68
-7
lines changed

12 files changed

+68
-7
lines changed

app/Contracts/Repository/DatabaseRepositoryInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ public function createDatabase(string $database): bool;
6868
* @param string $username
6969
* @param string $remote
7070
* @param string $password
71+
* @param $max_connections
7172
* @return bool
7273
*/
73-
public function createUser(string $username, string $remote, string $password): bool;
74+
public function createUser(string $username, string $remote, string $password, string $max_connections): bool;
7475

7576
/**
7677
* Give a specific user access to a given database.

app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function rules(): array
2525
$query->where('database_host_id', $this->input('database_host_id') ?? 0);
2626
}),
2727
],
28+
'max_connections' => 'nullable',
2829
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
2930
'database_host_id' => 'required|integer|exists:database_hosts,id',
3031
];

app/Models/Database.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Database extends Model
3030
* @var array
3131
*/
3232
protected $fillable = [
33-
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote',
33+
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote', 'max_connections',
3434
];
3535

3636
/**
@@ -41,6 +41,7 @@ class Database extends Model
4141
protected $casts = [
4242
'server_id' => 'integer',
4343
'database_host_id' => 'integer',
44+
'max_connections' => 'integer',
4445
];
4546

4647
/**
@@ -51,6 +52,7 @@ class Database extends Model
5152
'database_host_id' => 'required|exists:database_hosts,id',
5253
'database' => 'required|string|alpha_dash|between:3,100',
5354
'username' => 'string|alpha_dash|between:3,100',
55+
'max_connections' => 'nullable|integer',
5456
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
5557
'password' => 'string',
5658
];

app/Repositories/Eloquent/DatabaseRepository.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,16 @@ public function createDatabase(string $database): bool
135135
* @param string $username
136136
* @param string $remote
137137
* @param string $password
138+
* @param $max_connections
138139
* @return bool
139140
*/
140-
public function createUser(string $username, string $remote, string $password): bool
141+
public function createUser(string $username, string $remote, string $password, $max_connections): bool
141142
{
142-
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
143+
if (!$max_connections) {
144+
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
145+
} else {
146+
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\' WITH MAX_USER_CONNECTIONS %s', $username, $remote, $password, $max_connections));
147+
}
143148
}
144149

145150
/**

app/Services/Databases/DatabaseManagementService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public function create($server, array $data)
8484
$this->repository->createUser(
8585
$database->username,
8686
$database->remote,
87-
$this->encrypter->decrypt($database->password)
87+
$this->encrypter->decrypt($database->password),
88+
$database->max_connections
8889
);
8990
$this->repository->assignUserToDatabase(
9091
$database->database,

app/Services/Databases/DatabasePasswordService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function handle(Database $database): string
7171
]);
7272

7373
$this->repository->dropUser($database->username, $database->remote);
74-
$this->repository->createUser($database->username, $database->remote, $password);
74+
$this->repository->createUser($database->username, $database->remote, $password, $database->max_connections);
7575
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
7676
$this->repository->flush();
7777
});

app/Transformers/Api/Application/ServerDatabaseTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function transform(Database $model): array
5656
'database' => $model->database,
5757
'username' => $model->username,
5858
'remote' => $model->remote,
59+
'max_connections' => $model->max_connections,
5960
'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->created_at)
6061
->setTimezone(config('app.timezone'))
6162
->toIso8601String(),

app/Transformers/Api/Client/DatabaseTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function transform(Database $model): array
5858
'name' => $model->database,
5959
'username' => $model->username,
6060
'connections_from' => $model->remote,
61+
'max_connections' => $model->max_connections,
6162
];
6263
}
6364

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddMaxConnectionsColumnToDatabasesTable 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->integer('max_connections')->nullable()->default(0)->after('password');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('databases', function (Blueprint $table) {
29+
$table->dropColumn('max_connections');
30+
});
31+
}
32+
}

resources/scripts/components/server/databases/DatabaseRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default ({ database, className }: Props) => {
5151
addError({ key: 'database:delete', message: httpErrorToHuman(error) });
5252
});
5353
};
54-
54+
5555
return (
5656
<React.Fragment>
5757
<Formik

0 commit comments

Comments
 (0)