misc_pterodactyl-panel/app/Services/Users/UserCreationService.php

59 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Services\Users;
2017-12-31 02:25:04 +00:00
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\User;
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
{
2021-01-23 20:33:34 +00:00
if (array_key_exists('password', $data) && !empty($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
$this->connection->beginTransaction();
2021-01-23 20:33:34 +00:00
if (!isset($data['password']) || empty($data['password'])) {
$generateResetToken = true;
$data['password'] = $this->hasher->make(str_random(30));
}
/** @var \Pterodactyl\Models\User $user */
2017-12-31 02:25:04 +00:00
$user = $this->repository->create(array_merge($data, [
'uuid' => Uuid::uuid4()->toString(),
]), true, true);
2017-12-31 02:25:04 +00:00
if (isset($generateResetToken)) {
$token = $this->passwordBroker->createToken($user);
}
$this->connection->commit();
$user->notify(new AccountCreated($user, $token ?? null));
return $user;
}
}