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;
|
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
|
|
|
{
|
|
|
|
/**
|
2017-08-05 00:11:41 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $connection;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-25 00:49:09 +00:00
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $hasher;
|
2017-07-01 20:29:49 +00:00
|
|
|
|
2017-06-14 04:25:37 +00:00
|
|
|
/**
|
2018-07-01 01:47:31 +00:00
|
|
|
* @var \Illuminate\Contracts\Auth\PasswordBroker
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2018-07-01 01:47:31 +00:00
|
|
|
private $passwordBroker;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $repository;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-05 00:11:41 +00:00
|
|
|
* CreationService constructor.
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-08-05 00:11:41 +00:00
|
|
|
ConnectionInterface $connection,
|
2017-06-14 04:25:37 +00:00
|
|
|
Hasher $hasher,
|
2018-07-01 01:47:31 +00:00
|
|
|
PasswordBroker $passwordBroker,
|
2017-07-01 20:29:49 +00:00
|
|
|
UserRepositoryInterface $repository
|
2017-06-14 04:25:37 +00:00
|
|
|
) {
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->connection = $connection;
|
2017-06-14 04:25:37 +00:00
|
|
|
$this->hasher = $hasher;
|
2018-07-01 01:47:31 +00:00
|
|
|
$this->passwordBroker = $passwordBroker;
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository = $repository;
|
2017-06-14 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new user on the system.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Models\User
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2017-06-25 00:49:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
public function handle(array $data)
|
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;
|
|
|
|
}
|
|
|
|
}
|