Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Users/UserCreationService.php
10283 views
1
<?php
2
3
namespace Pterodactyl\Services\Users;
4
5
use Ramsey\Uuid\Uuid;
6
use Pterodactyl\Models\User;
7
use Illuminate\Contracts\Hashing\Hasher;
8
use Illuminate\Database\ConnectionInterface;
9
use Illuminate\Contracts\Auth\PasswordBroker;
10
use Pterodactyl\Notifications\AccountCreated;
11
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
12
13
class UserCreationService
14
{
15
/**
16
* UserCreationService constructor.
17
*/
18
public function __construct(
19
private ConnectionInterface $connection,
20
private Hasher $hasher,
21
private PasswordBroker $passwordBroker,
22
private UserRepositoryInterface $repository,
23
) {
24
}
25
26
/**
27
* Create a new user on the system.
28
*
29
* @throws \Exception
30
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
31
*/
32
public function handle(array $data): User
33
{
34
if (array_key_exists('password', $data) && !empty($data['password'])) {
35
$data['password'] = $this->hasher->make($data['password']);
36
}
37
38
$this->connection->beginTransaction();
39
if (!isset($data['password']) || empty($data['password'])) {
40
$generateResetToken = true;
41
$data['password'] = $this->hasher->make(str_random(30));
42
}
43
44
/** @var User $user */
45
$user = $this->repository->create(array_merge($data, [
46
'uuid' => Uuid::uuid4()->toString(),
47
]), true, true);
48
49
if (isset($generateResetToken)) {
50
$token = $this->passwordBroker->createToken($user);
51
}
52
53
$this->connection->commit();
54
$user->notify(new AccountCreated($user, $token ?? null));
55
56
return $user;
57
}
58
}
59
60