Files
panel/app/Services/Users/UserCreationService.php
SkyMulley 04dec3dd86 bugfix: user creation now properly appears in action log (#5644)
### Description

User creation was not properly logged within the action log, neither
from sub-user invite or manual user creation. This PR adds the relevant
log and includes testing.

### Manual testing scenarios

1. Invite a user within any created server
2. Create a user in the admin
3. Verify both users appear as an action in the action log

### Questions or comments

AI was used for secondary testing, QA and code review. No code was
written by AI.

### Resolved issues:
1. [x] resolves pterodactyl/panel#5631
2026-06-01 11:55:37 -04:00

69 lines
1.9 KiB
PHP

<?php
namespace Pterodactyl\Services\Users;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\User;
use Pterodactyl\Facades\Activity;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Auth\PasswordBroker;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserCreationService
{
/**
* UserCreationService constructor.
*/
public function __construct(
private ConnectionInterface $connection,
private Hasher $hasher,
private PasswordBroker $passwordBroker,
private UserRepositoryInterface $repository,
) {
}
/**
* Create a new user on the system.
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function handle(array $data): User
{
if (array_key_exists('password', $data) && !empty($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
$this->connection->beginTransaction();
if (!isset($data['password']) || empty($data['password'])) {
$generateResetToken = true;
$data['password'] = $this->hasher->make(str_random(30));
}
/** @var User $user */
$user = $this->repository->create(array_merge($data, [
'uuid' => Uuid::uuid4()->toString(),
]), true, true);
if (isset($generateResetToken)) {
$token = $this->passwordBroker->createToken($user);
}
$this->connection->commit();
$user->notify(new AccountCreated($user, $token ?? null));
Activity::event('user:user.create')
->subject($user)
->property([
'email' => $user->email,
'username' => $user->username,
'admin' => $user->root_admin,
])
->log();
return $user;
}
}