Skip to content

Commit

Permalink
Allow more values for remote field when creating a database; closes #…
Browse files Browse the repository at this point in the history
…3842
  • Loading branch information
DaneEveritt committed May 7, 2022
1 parent b07fdc1 commit c751ce7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Validation\Rule;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\Permission;
use Illuminate\Database\Query\Builder;
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
Expand All @@ -28,7 +29,7 @@ public function rules(): array
'database' => [
'required',
'alpha_dash',
'min:1',
'min:3',
'max:48',
// Yes, I am aware that you could have the same database name across two unique hosts. However,
// I don't really care about that for this validation. We just want to make sure it is unique to
Expand All @@ -38,7 +39,7 @@ public function rules(): array
->where('database', DatabaseManagementService::generateUniqueDatabaseName($this->input('database'), $server->id));
}),
],
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
'remote' => Database::getRulesForField('remote'),
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Database extends Model
'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}$/',
'remote' => 'required|string|regex:/^[\w\-\/.%:]+$/',
'password' => 'string',
];

Expand Down
10 changes: 10 additions & 0 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pterodactyl\Models;

use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Illuminate\Container\Container;
use Illuminate\Contracts\Validation\Factory;
Expand Down Expand Up @@ -111,6 +112,15 @@ public static function getRules()
return $rules;
}

/**
* Returns the rules for a specific field. If the field is not found an empty
* array is returned.
*/
public static function getRulesForField(string $field): array
{
return Arr::get(static::getRules(), $field) ?? [];
}

/**
* Returns the rules associated with the model, specifically for updating the given model
* rather than just creating it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ const schema = object().shape({
.required('A database name must be provided.')
.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(/^([0-9]{1,3}|%)(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?$/, 'A valid connection address must be provided.'),
.matches(/^[\w\-.]{3,48}$/, 'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.'),
connectionsFrom: string().matches(/^[\w\-/.%:]+$/, 'A valid host address must be provided.'),
});

export default () => {
Expand All @@ -36,7 +34,10 @@ export default () => {

const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
clearFlashes('database:create');
createServerDatabase(uuid, { ...values })
createServerDatabase(uuid, {
databaseName: values.databaseName,
connectionsFrom: values.connectionsFrom || '%',
})
.then(database => {
appendDatabase(database);
setVisible(false);
Expand All @@ -51,7 +52,7 @@ export default () => {
<>
<Formik
onSubmit={submit}
initialValues={{ databaseName: '', connectionsFrom: '%' }}
initialValues={{ databaseName: '', connectionsFrom: '' }}
validationSchema={schema}
>
{
Expand Down Expand Up @@ -81,7 +82,7 @@ export default () => {
id={'connections_from'}
name={'connectionsFrom'}
label={'Connections From'}
description={'Where connections should be allowed from. Use % for wildcards.'}
description={'Where connections should be allowed from. Leave blank to allow connections from anywhere.'}
/>
</div>
<div css={tw`flex flex-wrap justify-end mt-6`}>
Expand Down
Empty file modified storage/clockwork/.gitignore
100644 → 100755
Empty file.

0 comments on commit c751ce7

Please sign in to comment.