2026-10-05 18:10:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Actions\Users;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
2026-10-09 06:26:45 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Contracts\Users\UpdatesUsers;
|
|
|
|
|
use Pterodactyl\Events\User\PasswordChanged;
|
|
|
|
|
use Pterodactyl\Models\User;
|
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;
|
|
|
|
|
|
2026-10-09 06:26:45 +01:00
|
|
|
final readonly class UpdateUser implements UpdatesUsers
|
2026-10-05 18:10:18 -04:00
|
|
|
{
|
2026-10-09 06:26:45 +01:00
|
|
|
public function __construct(private ExtensionFields $extensions) {}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Update the user model instance and return the updated model. The hashed
|
|
|
|
|
* password cast on the model takes care of hashing a new password.
|
|
|
|
|
*
|
2026-10-09 06:26:45 +01:00
|
|
|
* @param UserUpdateData $data
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
|
|
|
|
public function update(User $user, 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
|
|
|
if (empty(Arr::get($data, 'password'))) {
|
|
|
|
|
unset($data['password']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 06:26:45 +01:00
|
|
|
DB::transaction(function () use ($user, $data, $extensions): void {
|
|
|
|
|
$user->forceFill($data)->saveOrFail();
|
|
|
|
|
$this->extensions->save($user, $extensions);
|
|
|
|
|
});
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
if (isset($data['password'])) {
|
|
|
|
|
event(new PasswordChanged($user));
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-08 17:40:00 -04:00
|
|
|
return $user;
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|
|
|
|
|
}
|