Files
pterodactyl-panel/app/Actions/Users/UpdateUser.php
T

49 lines
1.3 KiB
PHP
Raw Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Users;
use Illuminate\Support\Arr;
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;
use Pterodactyl\Services\Extensions\ExtensionFields;
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
2026-10-05 18:10:18 -04:00
use Throwable;
final readonly class UpdateUser implements UpdatesUsers
2026-10-05 18:10:18 -04: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.
*
* @param UserUpdateData $data
2026-10-05 18:10:18 -04:00
*
* @throws Throwable
*/
public function update(User $user, array $data): User
{
$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']);
}
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
}
}