From 6a573b762a28cf3ffbe991fd179c8d920a4d7016 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:12:13 -0400 Subject: [PATCH] action validation: users --- app/Actions/Users/CreateSshKey.php | 20 +------- app/Actions/Users/CreateUser.php | 60 ++---------------------- app/Actions/Users/UpdateUser.php | 11 ++++- phpstan.neon | 1 + tools/phpstan/stubs/ValidatorFacade.stub | 8 ++++ 5 files changed, 24 insertions(+), 76 deletions(-) create mode 100644 tools/phpstan/stubs/ValidatorFacade.stub diff --git a/app/Actions/Users/CreateSshKey.php b/app/Actions/Users/CreateSshKey.php index 80e749f90..0b89b1b02 100644 --- a/app/Actions/Users/CreateSshKey.php +++ b/app/Actions/Users/CreateSshKey.php @@ -51,23 +51,6 @@ final readonly class CreateSshKey implements CreatesSshKeys ]; } - /** - * Validates SSH key input and returns the key it describes. - * - * @param array $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(); diff --git a/app/Actions/Users/CreateUser.php b/app/Actions/Users/CreateUser.php index d2601fbd5..50fdefc9c 100644 --- a/app/Actions/Users/CreateUser.php +++ b/app/Actions/Users/CreateUser.php @@ -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 $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); diff --git a/app/Actions/Users/UpdateUser.php b/app/Actions/Users/UpdateUser.php index f5380bdb1..e4da302c9 100644 --- a/app/Actions/Users/UpdateUser.php +++ b/app/Actions/Users/UpdateUser.php @@ -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); diff --git a/phpstan.neon b/phpstan.neon index 7983c1d4e..40bdb38a3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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' diff --git a/tools/phpstan/stubs/ValidatorFacade.stub b/tools/phpstan/stubs/ValidatorFacade.stub new file mode 100644 index 000000000..c801aab00 --- /dev/null +++ b/tools/phpstan/stubs/ValidatorFacade.stub @@ -0,0 +1,8 @@ + validate(array $data, array $rules, array $messages = [], array $attributes = []) + */ +class Validator extends Facade {}