-
-
Notifications
You must be signed in to change notification settings - Fork 379
/
CreateNewUser.php
129 lines (111 loc) · 4.52 KB
/
CreateNewUser.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
<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Actions\Fortify;
use App\Models\Group;
use App\Models\Invite;
use App\Models\User;
use App\Repositories\ChatRepository;
use App\Rules\EmailBlacklist;
use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\CreatesNewUsers;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
public function __construct(private readonly ChatRepository $chatRepository)
{
}
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
* @throws ValidationException
* @throws Exception
*/
public function create(array $input): RedirectResponse | User
{
Validator::make($input, [
'username' => 'required|alpha_dash|string|between:3,25|unique:users',
'password' => [
'required',
'confirmed',
$this->passwordRules(),
],
'email' => [
'required',
'string',
'email',
'max:70',
'unique:users',
Rule::when(config('email-blacklist.enabled') === true, fn () => new EmailBlacklist()),
],
'captcha' => [
Rule::excludeIf(config('captcha.enabled') === false),
Rule::when(config('captcha.enabled') === true, 'hiddencaptcha'),
],
'code' => [
Rule::when(config('other.invite-only') === true, [
'required',
Rule::exists('invites', 'code')->whereNull('accepted_by'),
]),
]
])->validate();
$validatingGroup = cache()->rememberForever('validating_group', fn () => Group::query()->where('slug', '=', 'validating')->pluck('id'));
$user = User::create([
'username' => $input['username'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'passkey' => md5(random_bytes(60)),
'rsskey' => md5(random_bytes(60)),
'uploaded' => config('other.default_upload'),
'downloaded' => config('other.default_download'),
'style' => config('other.default_style', 0),
'locale' => config('app.locale'),
'group_id' => $validatingGroup[0],
]);
$user->passkeys()->create(['content' => $user->passkey]);
$user->rsskeys()->create(['content' => $user->rsskey]);
$user->emailUpdates()->create();
if (config('other.invite-only') === true) {
Invite::where('code', '=', $input['code'])->update([
'accepted_by' => $user->id,
'accepted_at' => now(),
]);
}
// Select A Random Welcome Message
$profileUrl = href_profile($user);
$welcomeArray = [
\sprintf('[url=%s]%s[/url], Welcome to ', $profileUrl, $user->username).config('other.title').'! Hope you enjoy the community.',
\sprintf("[url=%s]%s[/url], We've been expecting you.", $profileUrl, $user->username),
\sprintf("[url=%s]%s[/url] has arrived. Party's over.", $profileUrl, $user->username),
\sprintf("It's a bird! It's a plane! Nevermind, it's just [url=%s]%s[/url].", $profileUrl, $user->username),
\sprintf('Ready player [url=%s]%s[/url].', $profileUrl, $user->username),
\sprintf('A wild [url=%s]%s[/url] appeared.', $profileUrl, $user->username),
'Welcome to '.config('other.title').\sprintf(' [url=%s]%s[/url]. We were expecting you.', $profileUrl, $user->username),
];
$this->chatRepository->systemMessage(
$welcomeArray[array_rand($welcomeArray)]
);
// Send Welcome PM
$user->sendSystemNotification(
subject: config('welcomepm.subject'),
message: config('welcomepm.message'),
);
return $user;
}
}