Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 591bbda

Browse files
authored
feat: polite username rule (#53)
1 parent 0e81f1b commit 591bbda

File tree

11 files changed

+1307
-588
lines changed

11 files changed

+1307
-588
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"arkecosystem/ui": "^2.0",
2020
"spatie/laravel-personal-data-export": "^2.0",
2121
"spatie/laravel-medialibrary": "^9.4",
22-
"konceiver/laravel-data-bags": "^1.1"
22+
"konceiver/laravel-data-bags": "^1.1",
23+
"snipe/banbuilder": "^2.3"
2324
},
2425
"autoload": {
2526
"psr-4": {

composer.lock

Lines changed: 1199 additions & 583 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/profanities.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
7+
'cunt',
8+
'shit',
9+
'fag',
10+
'penis',
11+
12+
];

resources/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
'messages' => [
1010
'one_time_password' => 'We were not able to enable two-factor authentication with this one-time password.',
11+
'polite_username' => 'The given username contains words with profanities.',
1112
],
1213
];

src/Actions/CreateNewUser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ARKEcosystem\Fortify\Actions;
66

77
use ARKEcosystem\Fortify\Models;
8+
use ARKEcosystem\Fortify\Rules\PoliteUsername;
89
use Illuminate\Support\Facades\Config;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Hash;
@@ -51,15 +52,15 @@ public function create(array $input)
5152
private function buildValidator(array $input): ValidationValidator
5253
{
5354
$rules = [
54-
'name' => ['required', 'string', 'max:255'],
55+
'name' => ['required', 'string', 'max:255', resolve(PoliteUsername::class)],
5556
Fortify::username() => $this->usernameRules(),
5657
'password' => $this->passwordRules(),
5758
'terms' => ['required', 'accepted'],
5859
'invitation' => ['sometimes', 'required', 'string'],
5960
];
6061

6162
if ($usernameAlt = Config::get('fortify.username_alt')) {
62-
$rules[$usernameAlt] = ['required', 'string', 'max:255', 'unique:users'];
63+
$rules[$usernameAlt] = ['required', 'string', 'max:255', 'unique:users', resolve(PoliteUsername::class)];
6364
}
6465

6566
return Validator::make($input, $rules);
@@ -82,7 +83,7 @@ private function getUserData(array $input): array
8283

8384
private function usernameRules(): array
8485
{
85-
$rules = ['required', 'string', 'max:255', 'unique:users'];
86+
$rules = ['required', 'string', 'max:255', 'unique:users', resolve(PoliteUsername::class)];
8687

8788
if (Fortify::username() === 'email') {
8889
$rules[] = 'email';

src/Actions/UpdateUserProfileInformation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ARKEcosystem\Fortify\Actions;
66

7+
use ARKEcosystem\Fortify\Rules\PoliteUsername;
78
use Illuminate\Contracts\Auth\MustVerifyEmail;
89
use Illuminate\Support\Facades\Validator;
910
use Illuminate\Validation\Rule;
@@ -22,7 +23,7 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
2223
public function update($user, array $input)
2324
{
2425
Validator::make($input, [
25-
'name' => ['required', 'string', 'max:255'],
26+
'name' => ['required', 'string', 'max:255', resolve(PoliteUsername::class)],
2627
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
2728
])->validateWithBag('updateProfileInformation');
2829

src/FortifyServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public function registerPublishers(): void
8787
__DIR__.'/../config/fortify.php' => config_path('fortify.php'),
8888
], 'config');
8989

90+
$this->mergeConfigFrom(
91+
__DIR__.'/../config/profanities.php',
92+
'profanities'
93+
);
94+
9095
$this->publishes([
9196
__DIR__.'/../resources/views/auth' => resource_path('views/auth'),
9297
__DIR__.'/../resources/views/components' => resource_path('views/components'),

src/Rules/PoliteUsername.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ARKEcosystem\Fortify\Rules;
6+
7+
use Illuminate\Contracts\Validation\Rule;
8+
use Snipe\BanBuilder\CensorWords;
9+
10+
final class PoliteUsername implements Rule
11+
{
12+
public function __construct(public CensorWords $censor)
13+
{
14+
$this->censor->badwords = config('profanities');
15+
}
16+
17+
public function passes($attribute, $value)
18+
{
19+
return count($this->censor->censorString($value)['matched']) === 0;
20+
}
21+
22+
public function message()
23+
{
24+
return trans('fortify::validation.messages.polite_username');
25+
}
26+
}

tests/Actions/CreateNewUserTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,16 @@
217217

218218
$this->assertNull($user->email_verified_at);
219219
});
220+
221+
it('should require to not have any profanity in the name', function () {
222+
Config::set('fortify.models.user', \ARKEcosystem\Fortify\Models\User::class);
223+
224+
expectValidationError(fn () => (new CreateNewUser())->create([
225+
'name' => 'John Penis',
226+
'username' => 'alfonsobries',
227+
'email' => 'john@doe.com',
228+
'password' => 'sec$r2t12345',
229+
'password_confirmation' => 'sec$r2t12345',
230+
'terms' => true,
231+
]), 'name', trans('fortify::validation.messages.polite_username'));
232+
});

tests/Actions/UpdateUserProfileInformationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@
8585
'email' => str_repeat('#', 256),
8686
]), 'email', 'The email must be a valid email address.');
8787
});
88+
89+
it('should not update the profile information if the name of the user contain profanity', function () {
90+
$user = createUserModel();
91+
92+
expectValidationError(fn () => resolve(UpdateUserProfileInformation::class)->update($user, [
93+
'name' => 'Penis Doe',
94+
'email' => 'jane@doe.com',
95+
]), 'name', trans('fortify::validation.messages.polite_username'));
96+
});

0 commit comments

Comments
 (0)