action validation: users

This commit is contained in:
robert dennis
2026-10-12 05:18:58 -04:00
parent c8b13d4444
commit 6a573b762a
5 changed files with 24 additions and 76 deletions
+2 -18
View File
@@ -51,23 +51,6 @@ final readonly class CreateSshKey implements CreatesSshKeys
];
}
/**
* Validates SSH key input and returns the key it describes.
*
* @param array<string, mixed> $input
* @return array{name: string, public_key: string, fingerprint: string}
*/
public static function attributes(array $input): array
{
$input = Validator::make($input, self::rules())->safe();
return [
'name' => $input->string('name')->toString(),
'public_key' => $input->string('public_key')->toString(),
'fingerprint' => $input->string('fingerprint')->toString(),
];
}
/**
* Whether a value is a supported public key format that is safe to parse: not empty,
* no longer than the limit, without NUL bytes, and starting with a known key prefix.
@@ -93,7 +76,8 @@ final readonly class CreateSshKey implements CreatesSshKeys
/** @throws ValidationException */
public function create(User $user, string $name, string $publicKey, string $fingerprint): UserSSHKey
{
$key = $user->sshKeys()->create(self::attributes(['name' => $name, 'public_key' => $publicKey, 'fingerprint' => $fingerprint]));
$attributes = Validator::validate(['name' => $name, 'public_key' => $publicKey, 'fingerprint' => $fingerprint], self::rules());
$key = $user->sshKeys()->create($attributes);
Activity::event('user:ssh-key.create')->subject($user, $key)->property('fingerprint', $key->fingerprint)->log();
+3 -57
View File
@@ -56,62 +56,6 @@ final readonly class CreateUser implements CreatesUsers
];
}
/**
* Validates user input and returns the model attributes it describes. Given the user
* being updated, only the fields in the input are validated and returned, so the rest
* keep their stored values. A blank password is left out.
*
* @param array<string, mixed> $input
* @return array{email?: string, username?: string, name_first?: string, name_last?: string, external_id?: string|null, password?: string, root_admin?: bool, language?: string}
*/
public static function attributes(array $input, ?User $user = null): array
{
$rules = self::rules($user);
if ($user instanceof User) {
foreach ($rules as $field => $fieldRules) {
$rules[$field] = ['sometimes', ...$fieldRules];
}
}
$input = Validator::make($input, $rules)->safe();
$attributes = [];
if ($input->has('email')) {
$attributes['email'] = $input->string('email')->toString();
}
if ($input->has('username')) {
$attributes['username'] = $input->string('username')->toString();
}
if ($input->has('name_first')) {
$attributes['name_first'] = $input->string('name_first')->toString();
}
if ($input->has('name_last')) {
$attributes['name_last'] = $input->string('name_last')->toString();
}
if ($input->has('external_id')) {
$attributes['external_id'] = $input->filled('external_id') ? $input->string('external_id')->toString() : null;
}
// A blank password, spaces included, is no password at all.
if ($input->filled('password')) {
$attributes['password'] = $input->string('password')->toString();
}
if ($input->has('root_admin')) {
$attributes['root_admin'] = $input->boolean('root_admin');
}
if ($input->filled('language')) {
$attributes['language'] = $input->string('language')->toString();
}
return $attributes;
}
/**
* Create a new user on the system. When no password is provided a random one is
* set and a password reset token is generated for the account welcome email.
@@ -125,9 +69,11 @@ final readonly class CreateUser implements CreatesUsers
*/
public function create(array $input): User
{
$attributes = self::attributes($input);
$attributes = Validator::validate($input, self::rules());
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
// Blank fields take their defaults, so a blank password, spaces included, is no password at all.
$attributes = array_filter($attributes, filled(...));
$generateResetToken = ! isset($attributes['password']);
$attributes['password'] ??= Str::random(30);
+10 -1
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Users;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Pterodactyl\Contracts\Users\UpdatesUsers;
use Pterodactyl\Events\User\PasswordChanged;
use Pterodactyl\Models\User;
@@ -26,9 +27,17 @@ final readonly class UpdateUser implements UpdatesUsers
*/
public function update(User $user, array $input): User
{
$attributes = CreateUser::attributes($input, $user);
$rules = array_map(fn (array $rules): array => ['sometimes', ...$rules], CreateUser::rules($user));
$validated = Validator::validate($input, $rules);
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
// Blank fields change nothing, a blank password included, apart from a blank external id,
// which clears it.
$attributes = array_filter($validated, filled(...));
if (array_key_exists('external_id', $validated)) {
$attributes['external_id'] ??= null;
}
DB::transaction(function () use ($user, $attributes, $extensions): void {
$user->forceFill($attributes)->saveOrFail();
$this->extensions->save($user, $extensions);
+1
View File
@@ -10,6 +10,7 @@ parameters:
parseModelCastsMethod: true
stubFiles:
- tools/phpstan/stubs/EloquentHasAttributes.stub
- tools/phpstan/stubs/ValidatorFacade.stub
typeAliases:
ValidationRule: 'string|\Stringable|\Closure|\Illuminate\Contracts\Validation\Rule|\Illuminate\Contracts\Validation\ValidationRule'
ValidationRuleSet: 'array<int, ValidationRule>'
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Illuminate\Support\Facades;
/**
* @method static array<string, mixed> validate(array<mixed> $data, array<mixed> $rules, array<mixed> $messages = [], array<mixed> $attributes = [])
*/
class Validator extends Facade {}