2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Actions\Users;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Password;
|
2026-10-11 06:31:11 -04:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Support\Str;
|
2026-10-11 06:31:11 -04:00
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use Illuminate\Validation\Rules\In;
|
2026-10-11 13:45:59 -04:00
|
|
|
use Illuminate\Validation\Rules\Password as PasswordRule;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Contracts\Users\CreatesUsers;
|
2026-10-11 14:19:32 -04:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
2026-10-11 06:31:11 -04:00
|
|
|
use Pterodactyl\Rules\UserEmail;
|
|
|
|
|
use Pterodactyl\Rules\Username;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Pterodactyl\Services\Extensions\ExtensionFields;
|
|
|
|
|
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
final readonly class CreateUser implements CreatesUsers
|
|
|
|
|
{
|
2026-10-11 13:45:59 -04:00
|
|
|
public function __construct(private ExtensionFields $extensions) {}
|
|
|
|
|
|
2026-10-11 06:31:11 -04:00
|
|
|
/**
|
2026-10-11 13:45:59 -04:00
|
|
|
* Rules every new password must pass, wherever it is set. The policy itself is
|
|
|
|
|
* Password::defaults(), set in AppServiceProvider.
|
2026-10-11 06:31:11 -04:00
|
|
|
*
|
2026-10-11 13:45:59 -04:00
|
|
|
* @return list<string|PasswordRule>
|
2026-10-11 06:31:11 -04:00
|
|
|
*/
|
2026-10-11 13:45:59 -04:00
|
|
|
public static function passwordRules(): array
|
|
|
|
|
{
|
|
|
|
|
return ['string', PasswordRule::default()];
|
|
|
|
|
}
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-10-11 06:31:11 -04:00
|
|
|
/**
|
2026-10-11 23:50:53 -04:00
|
|
|
* Pass the user being updated so its own email, username and external id are not taken for
|
|
|
|
|
* duplicates.
|
2026-10-11 06:31:11 -04:00
|
|
|
*
|
|
|
|
|
* @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'],
|
2026-10-11 13:45:59 -04:00
|
|
|
'password' => ['sometimes', 'nullable', ...self::passwordRules()],
|
2026-10-11 06:31:11 -04:00
|
|
|
'root_admin' => ['boolean'],
|
|
|
|
|
'language' => ['string', new In(array_keys((new User)->getAvailableLanguages()))],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create(array $input): User
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
2026-10-11 23:12:13 -04:00
|
|
|
$attributes = Validator::validate($input, self::rules());
|
2026-10-11 06:31:11 -04:00
|
|
|
$extensions = ValidatedExtensionValues::of($input['extensions'] ?? null);
|
2026-10-09 06:26:45 +01:00
|
|
|
|
2026-10-11 23:12:13 -04:00
|
|
|
// Blank fields take their defaults, so a blank password, spaces included, is no password at all.
|
|
|
|
|
$attributes = array_filter($attributes, filled(...));
|
2026-10-11 06:31:11 -04:00
|
|
|
$generateResetToken = ! isset($attributes['password']);
|
|
|
|
|
$attributes['password'] ??= Str::random(30);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
2026-10-11 06:31:11 -04:00
|
|
|
[$user, $token] = DB::transaction(function () use ($attributes, $generateResetToken, $extensions): array {
|
|
|
|
|
$user = User::query()->create($attributes);
|
2026-10-09 06:26:45 +01:00
|
|
|
$this->extensions->save($user, $extensions);
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
return [$user->refresh(), $generateResetToken ? Password::createToken($user) : null];
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-11 14:19:32 -04:00
|
|
|
Activity::event('user:user.create')
|
|
|
|
|
->subject($user)
|
|
|
|
|
->property(['email' => $user->email, 'username' => $user->username, 'admin' => $user->root_admin])
|
|
|
|
|
->log();
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$user->notify(new AccountCreated($user, $token));
|
|
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
}
|