Skip to content

Commit 3964587

Browse files
committed
Add UsernameService
1 parent 8929d6f commit 3964587

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Models\User;
7+
use App\Services\UsernameService;
78
use Illuminate\Foundation\Auth\RegistersUsers;
89
use Illuminate\Support\Facades\Hash;
910
use Illuminate\Support\Facades\Validator;
10-
use Illuminate\Validation\Rule;
1111

1212
class RegisterController extends Controller
1313
{
@@ -56,7 +56,11 @@ protected function validator(array $data)
5656
'min:2',
5757
'max:30',
5858
'unique:users,username',
59-
Rule::notIn(['admin', 'dansup', 'support', 'loops', 'official', 'team', 'teamLoops', 'new', 'discover', 'explore']),
59+
function ($attribute, $value, $fail) {
60+
if (app(UsernameService::class)->isReserved($value)) {
61+
$fail('This username is reserved.');
62+
}
63+
},
6064
],
6165
'email' => ['required', 'string', 'email:rfc,dns,spoof,strict', 'max:255', 'unique:users'],
6266
'password' => ['required', 'string', 'min:8', 'confirmed'],

app/Http/Requests/StoreRegisterUsernameRequest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace App\Http\Requests;
44

55
use App\Models\AdminSetting;
6+
use App\Services\UsernameService;
67
use Illuminate\Auth\Access\AuthorizationException;
78
use Illuminate\Foundation\Http\FormRequest;
8-
use Illuminate\Validation\Rule;
99

1010
class StoreRegisterUsernameRequest extends FormRequest
1111
{
@@ -43,7 +43,11 @@ public function rules(): array
4343
'min:2',
4444
'max:30',
4545
'unique:users,username',
46-
Rule::notIn(['admin', 'dansup', 'support', 'loops', 'official', 'team', 'teamLoops', 'new', 'discover', 'explore']),
46+
function ($attribute, $value, $fail) {
47+
if (app(UsernameService::class)->isReserved($value)) {
48+
$fail('This username is reserved.');
49+
}
50+
},
4751
],
4852
'password' => 'required|min:8',
4953
'password_confirmation' => 'required|confirmed:password',

app/Services/UsernameService.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
class UsernameService
6+
{
7+
public function reserved()
8+
{
9+
return [
10+
'admin', 'administrator', 'root', 'system', 'sysadmin',
11+
'support', 'help', 'helpdesk', 'service',
12+
13+
'loops', 'loop', 'official', 'staff', 'team', 'teamLoops',
14+
'pixelfed',
15+
16+
'moderator', 'mod', 'mods', 'moderation',
17+
18+
'new', 'discover', 'explore', 'trending', 'popular',
19+
'featured', 'verified', 'recommended',
20+
21+
'user', 'username', 'account', 'profile',
22+
'everyone', 'all', 'users',
23+
24+
'inbox', 'outbox', 'followers', 'following',
25+
'actor', 'shared', 'public', 'instance',
26+
27+
'api', 'oauth', 'token', 'auth', 'login',
28+
'register', 'signup', 'signout', 'logout',
29+
'webhook', 'callback', 'feed', 'rss',
30+
31+
'info', 'contact', 'news', 'press', 'legal',
32+
'privacy', 'terms', 'tos', 'about', 'abuse',
33+
'security', 'dmca', 'copyright',
34+
35+
'verified', 'official', 'real', 'authentic',
36+
];
37+
}
38+
39+
public function wildcardPatterns()
40+
{
41+
return [
42+
'admin*',
43+
];
44+
}
45+
46+
public function isReserved(string $username): bool
47+
{
48+
$username = strtolower($username);
49+
50+
if (in_array($username, $this->reserved())) {
51+
return true;
52+
}
53+
54+
foreach ($this->wildcardPatterns() as $pattern) {
55+
if ($this->matchesPattern($username, $pattern)) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
61+
}
62+
63+
protected function matchesPattern(string $username, string $pattern): bool
64+
{
65+
$regex = '/^'.str_replace('\*', '.*', preg_quote($pattern, '/')).'$/i';
66+
67+
return preg_match($regex, $username) === 1;
68+
}
69+
}

0 commit comments

Comments
 (0)