Files
pterodactyl-panel/app/Actions/Users/CreateUser.php
T
2026-10-12 05:18:55 -04:00

140 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Users;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Pterodactyl\Contracts\Users\CreatesUsers;
use Pterodactyl\Models\User;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Rules\UserEmail;
use Pterodactyl\Rules\Username;
use Pterodactyl\Services\Extensions\ExtensionFields;
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
use Throwable;
final readonly class CreateUser implements CreatesUsers
{
/**
* Rules every new user password must pass, wherever it is set.
*
* @var list<string>
*/
public const array PASSWORD = ['string', 'min:8'];
public function __construct(private ExtensionFields $extensions) {}
/**
* A user's fields. Pass the user being updated so its own email, username and external
* id are not taken for duplicates.
*
* @return NormalizedValidationRules
*/
public static function rules(?User $user = null): array
{
return [
'email' => ['required', 'email:strict', 'between:1,191', new UserEmail, Rule::unique('users', 'email')->ignore($user)],
'external_id' => ['sometimes', 'nullable', 'string', 'max:191', Rule::unique('users', 'external_id')->ignore($user)],
'username' => ['required', 'between:1,191', Rule::unique('users', 'username')->ignore($user), new Username],
'name_first' => ['required', 'string', 'between:1,191'],
'name_last' => ['required', 'string', 'between:1,191'],
'password' => ['sometimes', 'nullable', ...self::PASSWORD],
'root_admin' => ['boolean'],
'language' => ['string', new In(array_keys((new User)->getAvailableLanguages()))],
];
}
/**
* 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 ActionInput $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;
}
$password = $input->string('password')->toString();
if ($password !== '') {
$attributes['password'] = $password;
}
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.
*
* The UUID and password hash are handled by the model itself (HasVersion4Uuids
* and the hashed password cast).
*
* @param ActionInput $input
*
* @throws Throwable
*/
public function create(array $input): User
{
$attributes = self::attributes($input);
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
$generateResetToken = ! isset($attributes['password']);
$attributes['password'] ??= Str::random(30);
[$user, $token] = DB::transaction(function () use ($attributes, $generateResetToken, $extensions): array {
$user = User::query()->create($attributes);
$this->extensions->save($user, $extensions);
return [$user->refresh(), $generateResetToken ? Password::createToken($user) : null];
});
$user->notify(new AccountCreated($user, $token));
return $user;
}
}