Skip to content

Commit 4457634

Browse files
committed
Fix user creation to use UUIDs correctly
Also updates the notification send method to be cleaner and more maintainable
1 parent 410a0cc commit 4457634

File tree

5 files changed

+98
-172
lines changed

5 files changed

+98
-172
lines changed

app/Http/Requests/Admin/UserFormRequest.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,21 @@
77
class UserFormRequest extends AdminFormRequest
88
{
99
/**
10-
* {@inheritdoc}
10+
* Rules to apply to requests for updating or creating a user
11+
* in the Admin CP.
1112
*/
1213
public function rules()
1314
{
15+
$rules = collect(User::getCreateRules());
1416
if ($this->method() === 'PATCH') {
15-
$rules = User::getUpdateRulesForId($this->route()->parameter('user')->id);
16-
17-
return array_merge($rules, [
18-
'ignore_connection_error' => 'sometimes|nullable|boolean',
17+
$rules = collect(User::getUpdateRulesForId($this->route()->parameter('user')->id))->merge([
18+
'ignore_connection_error' => ['sometimes', 'nullable', 'boolean'],
1919
]);
2020
}
2121

22-
return User::getCreateRules();
23-
}
24-
25-
/**
26-
* @param array|null $only
27-
* @return array
28-
*/
29-
public function normalize(array $only = null)
30-
{
31-
if ($this->method === 'PATCH') {
32-
return array_merge(
33-
$this->all(['password']),
34-
$this->only(['email', 'username', 'name_first', 'name_last', 'root_admin', 'language', 'ignore_connection_error'])
35-
);
36-
}
37-
38-
return parent::normalize();
22+
return $rules->only([
23+
'email', 'username', 'name_first', 'name_last', 'password',
24+
'language', 'ignore_connection_error', 'root_admin',
25+
])->toArray();
3926
}
4027
}

app/Models/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class User extends Model implements
115115
* @var array
116116
*/
117117
protected static $applicationRules = [
118+
'uuid' => 'required',
118119
'email' => 'required',
119120
'username' => 'required',
120121
'name_first' => 'required',
@@ -130,6 +131,7 @@ class User extends Model implements
130131
* @var array
131132
*/
132133
protected static $dataIntegrityRules = [
134+
'uuid' => 'string|size:36|unique:users,uuid',
133135
'email' => 'email|unique:users,email',
134136
'username' => 'alpha_dash|between:1,255|unique:users,username',
135137
'name_first' => 'string|between:1,255',

app/Notifications/AccountCreated.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Notifications;
114

5+
use Pterodactyl\Models\User;
126
use Illuminate\Bus\Queueable;
137
use Illuminate\Notifications\Notification;
148
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -19,7 +13,15 @@ class AccountCreated extends Notification implements ShouldQueue
1913
use Queueable;
2014

2115
/**
22-
* The password reset token to send.
16+
* The authentication token to be used for the user to set their
17+
* password for the first time.
18+
*
19+
* @var string|null
20+
*/
21+
public $token;
22+
23+
/**
24+
* The user model for the created user.
2325
*
2426
* @var object
2527
*/
@@ -28,11 +30,13 @@ class AccountCreated extends Notification implements ShouldQueue
2830
/**
2931
* Create a new notification instance.
3032
*
31-
* @param aray $user
33+
* @param \Pterodactyl\Models\User $user
34+
* @param string|null $token
3235
*/
33-
public function __construct(array $user)
36+
public function __construct(User $user, string $token = null)
3437
{
35-
$this->user = (object) $user;
38+
$this->token = $token;
39+
$this->user = $user;
3640
}
3741

3842
/**
@@ -56,12 +60,12 @@ public function toMail($notifiable)
5660
{
5761
$message = (new MailMessage)
5862
->greeting('Hello ' . $this->user->name . '!')
59-
->line('You are recieving this email because an account has been created for you on Pterodactyl Panel.')
63+
->line('You are recieving this email because an account has been created for you on ' . config('app.name') . '.')
6064
->line('Username: ' . $this->user->username)
61-
->line('Email: ' . $notifiable->email);
65+
->line('Email: ' . $this->user->email);
6266

63-
if (! is_null($this->user->token)) {
64-
return $message->action('Setup Your Account', url('/auth/password/reset/' . $this->user->token . '?email=' . $notifiable->email));
67+
if (! is_null($this->token)) {
68+
return $message->action('Setup Your Account', url('/auth/password/reset/' . $this->token . '?email=' . $this->user->email));
6569
}
6670

6771
return $message;

app/Services/Users/UserCreationService.php

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,52 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Services\Users;
114

125
use Ramsey\Uuid\Uuid;
13-
use Illuminate\Foundation\Application;
146
use Illuminate\Contracts\Hashing\Hasher;
157
use Illuminate\Database\ConnectionInterface;
16-
use Illuminate\Notifications\ChannelManager;
178
use Pterodactyl\Notifications\AccountCreated;
189
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
1910
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2011

2112
class UserCreationService
2213
{
23-
/**
24-
* @var \Illuminate\Foundation\Application
25-
*/
26-
protected $app;
27-
2814
/**
2915
* @var \Illuminate\Database\ConnectionInterface
3016
*/
31-
protected $connection;
17+
private $connection;
3218

3319
/**
3420
* @var \Illuminate\Contracts\Hashing\Hasher
3521
*/
36-
protected $hasher;
37-
38-
/**
39-
* @var \Illuminate\Notifications\ChannelManager
40-
*/
41-
protected $notification;
22+
private $hasher;
4223

4324
/**
4425
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
4526
*/
46-
protected $passwordService;
27+
private $passwordService;
4728

4829
/**
4930
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
5031
*/
51-
protected $repository;
32+
private $repository;
5233

5334
/**
5435
* CreationService constructor.
5536
*
56-
* @param \Illuminate\Foundation\Application $application
57-
* @param \Illuminate\Notifications\ChannelManager $notification
5837
* @param \Illuminate\Database\ConnectionInterface $connection
5938
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
6039
* @param \Pterodactyl\Services\Helpers\TemporaryPasswordService $passwordService
6140
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
6241
*/
6342
public function __construct(
64-
Application $application,
65-
ChannelManager $notification,
6643
ConnectionInterface $connection,
6744
Hasher $hasher,
6845
TemporaryPasswordService $passwordService,
6946
UserRepositoryInterface $repository
7047
) {
71-
$this->app = $application;
7248
$this->connection = $connection;
7349
$this->hasher = $hasher;
74-
$this->notification = $notification;
7550
$this->passwordService = $passwordService;
7651
$this->repository = $repository;
7752
}
@@ -97,20 +72,13 @@ public function handle(array $data)
9772
$token = $this->passwordService->handle($data['email']);
9873
}
9974

75+
/** @var \Pterodactyl\Models\User $user */
10076
$user = $this->repository->create(array_merge($data, [
10177
'uuid' => Uuid::uuid4()->toString(),
102-
]));
78+
]), true, true);
10379

10480
$this->connection->commit();
105-
106-
// @todo fire event, handle notification there
107-
$this->notification->send($user, $this->app->makeWith(AccountCreated::class, [
108-
'user' => [
109-
'name' => $user->name_first,
110-
'username' => $user->username,
111-
'token' => $token ?? null,
112-
],
113-
]));
81+
$user->notify(new AccountCreated($user, $token ?? null));
11482

11583
return $user;
11684
}

0 commit comments

Comments
 (0)