2017-06-14 04:25:37 +00:00
|
|
|
<?php
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
namespace Pterodactyl\Services\Users;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
2017-12-31 02:25:04 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2018-07-01 01:47:31 +00:00
|
|
|
use Illuminate\Contracts\Auth\PasswordBroker;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserCreationService
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* UserCreationService constructor.
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private Hasher $hasher,
|
|
|
|
private PasswordBroker $passwordBroker,
|
|
|
|
private UserRepositoryInterface $repository
|
2017-06-14 04:25:37 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new user on the system.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2017-06-25 00:49:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(array $data): User
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (array_key_exists('password', $data) && !empty($data['password'])) {
|
2017-06-14 04:25:37 +00:00
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->connection->beginTransaction();
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!isset($data['password']) || empty($data['password'])) {
|
2018-07-01 01:47:31 +00:00
|
|
|
$generateResetToken = true;
|
2017-07-01 20:29:49 +00:00
|
|
|
$data['password'] = $this->hasher->make(str_random(30));
|
|
|
|
}
|
|
|
|
|
2018-01-01 18:13:08 +00:00
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2017-12-31 02:25:04 +00:00
|
|
|
$user = $this->repository->create(array_merge($data, [
|
|
|
|
'uuid' => Uuid::uuid4()->toString(),
|
2018-01-01 18:13:08 +00:00
|
|
|
]), true, true);
|
2017-12-31 02:25:04 +00:00
|
|
|
|
2018-07-01 01:47:31 +00:00
|
|
|
if (isset($generateResetToken)) {
|
|
|
|
$token = $this->passwordBroker->createToken($user);
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->connection->commit();
|
2018-01-01 18:13:08 +00:00
|
|
|
$user->notify(new AccountCreated($user, $token ?? null));
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|