Files

69 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
2017-08-04 19:11:41 -05:00
namespace Pterodactyl\Services\Users;
2017-12-30 20:25:04 -06:00
use Ramsey\Uuid\Uuid;
2022-10-14 10:59:20 -06:00
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
{
/**
2022-10-14 10:59:20 -06:00
* UserCreationService constructor.
*/
public function __construct(
2022-10-14 10:59:20 -06:00
private ConnectionInterface $connection,
private Hasher $hasher,
private PasswordBroker $passwordBroker,
2024-10-21 19:18:20 -06:00
private UserRepositoryInterface $repository,
) {
}
/**
* Create a new user on the system.
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
2022-10-14 10:59:20 -06:00
public function handle(array $data): User
{
2021-01-23 12:33:34 -08:00
if (array_key_exists('password', $data) && !empty($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
2017-08-04 19:11:41 -05:00
$this->connection->beginTransaction();
2021-01-23 12:33:34 -08:00
if (!isset($data['password']) || empty($data['password'])) {
$generateResetToken = true;
$data['password'] = $this->hasher->make(str_random(30));
}
2024-10-21 19:18:20 -06:00
/** @var User $user */
2017-12-30 20:25:04 -06:00
$user = $this->repository->create(array_merge($data, [
'uuid' => Uuid::uuid4()->toString(),
2018-01-01 12:13:08 -06:00
]), true, true);
2017-12-30 20:25:04 -06:00
if (isset($generateResetToken)) {
$token = $this->passwordBroker->createToken($user);
}
2017-08-04 19:11:41 -05:00
$this->connection->commit();
2018-01-01 12:13:08 -06:00
$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;
}
}