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;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Pterodactyl\Contracts\Users\CreatesUsers;
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
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
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
final readonly class CreateUser implements CreatesUsers
|
|
|
|
|
{
|
2026-10-09 06:26:45 +01:00
|
|
|
public function __construct(private ExtensionFields $extensions) {}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* 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).
|
|
|
|
|
*
|
2026-10-09 06:26:45 +01:00
|
|
|
* @param UserCreationData $data
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
|
|
|
|
public function create(array $data): User
|
|
|
|
|
{
|
2026-10-09 06:26:45 +01:00
|
|
|
$extensions = ValidatedExtensionValues::of($data['extensions'] ?? null);
|
|
|
|
|
unset($data['extensions']);
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$generateResetToken = empty($data['password']);
|
|
|
|
|
if ($generateResetToken) {
|
|
|
|
|
$data['password'] = Str::random(30);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 06:26:45 +01:00
|
|
|
[$user, $token] = DB::transaction(function () use ($data, $generateResetToken, $extensions): array {
|
2026-10-05 18:10:18 -04:00
|
|
|
$user = User::query()->create($data);
|
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];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$user->notify(new AccountCreated($user, $token));
|
|
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
}
|