/
RegisterUserHandler.php
180 lines (145 loc) · 4.84 KB
/
RegisterUserHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User\Command;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AvatarUploader;
use Flarum\User\Event\RegisteringFromProvider;
use Flarum\User\Event\Saving;
use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\RegistrationToken;
use Flarum\User\User;
use Flarum\User\UserValidator;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Intervention\Image\ImageManager;
use InvalidArgumentException;
class RegisterUserHandler
{
use DispatchEventsTrait;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var UserValidator
*/
protected $userValidator;
/**
* @var AvatarUploader
*/
protected $avatarUploader;
/**
* @var Factory
*/
private $validator;
/**
* @var ImageManager
*/
protected $imageManager;
public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings, UserValidator $userValidator, AvatarUploader $avatarUploader, Factory $validator, ImageManager $imageManager)
{
$this->events = $events;
$this->settings = $settings;
$this->userValidator = $userValidator;
$this->avatarUploader = $avatarUploader;
$this->validator = $validator;
$this->imageManager = $imageManager;
}
/**
* @param RegisterUser $command
* @return User
* @throws PermissionDeniedException if signup is closed and the actor is
* not an administrator.
* @throws ValidationException
*/
public function handle(RegisterUser $command)
{
$actor = $command->actor;
$data = $command->data;
if (! $this->settings->get('allow_sign_up')) {
$actor->assertAdmin();
}
$password = Arr::get($data, 'attributes.password');
// If a valid authentication token was provided as an attribute,
// then we won't require the user to choose a password.
if (isset($data['attributes']['token'])) {
/** @var RegistrationToken $token */
$token = RegistrationToken::validOrFail($data['attributes']['token']);
$password = $password ?: Str::random(20);
}
$user = User::register(
Arr::get($data, 'attributes.username'),
Arr::get($data, 'attributes.email'),
$password
);
if (isset($token)) {
$this->applyToken($user, $token);
}
if ($actor->isAdmin() && Arr::get($data, 'attributes.isEmailConfirmed')) {
$user->activate();
}
$this->events->dispatch(
new Saving($user, $actor, $data)
);
$this->userValidator->assertValid(array_merge($user->getAttributes(), compact('password')));
$user->save();
if (isset($token)) {
$this->fulfillToken($user, $token);
}
$this->dispatchEventsFor($user, $actor);
return $user;
}
private function applyToken(User $user, RegistrationToken $token)
{
foreach ($token->user_attributes as $k => $v) {
if ($k === 'avatar_url') {
$this->uploadAvatarFromUrl($user, $v);
continue;
}
$user->$k = $v;
if ($k === 'email') {
$user->activate();
}
}
$this->events->dispatch(
new RegisteringFromProvider($user, $token->provider, $token->payload)
);
}
/**
* @throws InvalidArgumentException
*/
private function uploadAvatarFromUrl(User $user, string $url)
{
$urlValidator = $this->validator->make(compact('url'), [
'url' => 'required|active_url',
]);
if ($urlValidator->fails()) {
throw new InvalidArgumentException('Provided avatar URL must be a valid URI.', 503);
}
$scheme = parse_url($url, PHP_URL_SCHEME);
if (! in_array($scheme, ['http', 'https'])) {
throw new InvalidArgumentException("Provided avatar URL must have scheme http or https. Scheme provided was $scheme.", 503);
}
$image = $this->imageManager->make($url);
$this->avatarUploader->upload($user, $image);
}
private function fulfillToken(User $user, RegistrationToken $token)
{
$token->delete();
if ($token->provider && $token->identifier) {
$user->loginProviders()->create([
'provider' => $token->provider,
'identifier' => $token->identifier
]);
}
}
}