From c0fc9125ed1da4bc8fcd6a1385bbb030a6d7af41 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 24 Sep 2020 19:31:42 -0700 Subject: [PATCH] Fix database naming conventions; closes #2404 --- .../Api/Client/Servers/Databases/StoreDatabaseRequest.php | 2 +- app/Models/Database.php | 2 +- app/Services/Databases/DatabaseManagementService.php | 5 ++++- .../components/server/databases/CreateDatabaseButton.tsx | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php b/app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php index ebff178c..3f2fe29e 100644 --- a/app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php +++ b/app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php @@ -22,7 +22,7 @@ class StoreDatabaseRequest extends ClientApiRequest implements ClientPermissions public function rules(): array { return [ - 'database' => 'required|alpha_dash|min:1|max:100', + 'database' => 'required|alpha_dash|min:3|max:48', 'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/', ]; } diff --git a/app/Models/Database.php b/app/Models/Database.php index 42fbb1ac..8e66219f 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -65,7 +65,7 @@ class Database extends Model public static $validationRules = [ 'server_id' => 'required|numeric|exists:servers,id', 'database_host_id' => 'required|exists:database_hosts,id', - 'database' => 'required|string|alpha_dash|between:3,100', + 'database' => 'required|string|alpha_dash|between:3,48', 'username' => 'string|alpha_dash|between:3,100', 'max_connections' => 'nullable|integer', 'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/', diff --git a/app/Services/Databases/DatabaseManagementService.php b/app/Services/Databases/DatabaseManagementService.php index 7de6e292..ab415b1a 100644 --- a/app/Services/Databases/DatabaseManagementService.php +++ b/app/Services/Databases/DatabaseManagementService.php @@ -104,9 +104,12 @@ class DatabaseManagementService } } + // Max of 48 characters, including the s123_ that we append to the front. + $truncatedName = substr($data['database'], 0, 48 - strlen("s{$server->id}_")); + $data = array_merge($data, [ 'server_id' => $server->id, - 'database' => sprintf('s%d_%s', $server->id, $data['database']), + 'database' => $truncatedName, 'username' => sprintf('u%d_%s', $server->id, str_random(10)), 'password' => $this->encrypter->encrypt( Utilities::randomStringWithSpecialCharacters(24) diff --git a/resources/scripts/components/server/databases/CreateDatabaseButton.tsx b/resources/scripts/components/server/databases/CreateDatabaseButton.tsx index 03eaf63c..044587e6 100644 --- a/resources/scripts/components/server/databases/CreateDatabaseButton.tsx +++ b/resources/scripts/components/server/databases/CreateDatabaseButton.tsx @@ -19,9 +19,9 @@ interface Values { const schema = object().shape({ databaseName: string() .required('A database name must be provided.') - .min(5, 'Database name must be at least 5 characters.') - .max(64, 'Database name must not exceed 64 characters.') - .matches(/^[A-Za-z0-9_\-.]{5,64}$/, 'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.'), + .min(3, 'Database name must be at least 3 characters.') + .max(48, 'Database name must not exceed 48 characters.') + .matches(/^[A-Za-z0-9_\-.]{3,48}$/, 'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.'), connectionsFrom: string() .required('A connection value must be provided.') .matches(/^([1-9]{1,3}|%)(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?$/, 'A valid connection address must be provided.'),