Path: blob/1.0-develop/app/Services/Users/UserCreationService.php
10283 views
<?php12namespace Pterodactyl\Services\Users;34use Ramsey\Uuid\Uuid;5use Pterodactyl\Models\User;6use Illuminate\Contracts\Hashing\Hasher;7use Illuminate\Database\ConnectionInterface;8use Illuminate\Contracts\Auth\PasswordBroker;9use Pterodactyl\Notifications\AccountCreated;10use Pterodactyl\Contracts\Repository\UserRepositoryInterface;1112class UserCreationService13{14/**15* UserCreationService constructor.16*/17public function __construct(18private ConnectionInterface $connection,19private Hasher $hasher,20private PasswordBroker $passwordBroker,21private UserRepositoryInterface $repository,22) {23}2425/**26* Create a new user on the system.27*28* @throws \Exception29* @throws \Pterodactyl\Exceptions\Model\DataValidationException30*/31public function handle(array $data): User32{33if (array_key_exists('password', $data) && !empty($data['password'])) {34$data['password'] = $this->hasher->make($data['password']);35}3637$this->connection->beginTransaction();38if (!isset($data['password']) || empty($data['password'])) {39$generateResetToken = true;40$data['password'] = $this->hasher->make(str_random(30));41}4243/** @var User $user */44$user = $this->repository->create(array_merge($data, [45'uuid' => Uuid::uuid4()->toString(),46]), true, true);4748if (isset($generateResetToken)) {49$token = $this->passwordBroker->createToken($user);50}5152$this->connection->commit();53$user->notify(new AccountCreated($user, $token ?? null));5455return $user;56}57}585960