Reduce username limit to 16 characters

This commit is contained in:
ErickSkrauch
2025-05-30 04:51:38 +02:00
parent 09c51c943f
commit efd386b5e0
2 changed files with 9 additions and 5 deletions

View File

@@ -37,7 +37,7 @@ class UsernameValidatorTest extends TestCase {
$this->validator->validateAttribute($model, 'field');
$this->assertSame(['error.username_too_short'], $model->getErrors('field'));
$model = $this->createModel('erickskrauch_erickskrauch');
$model = $this->createModel('mcmodder_mcmodder');
$this->validator->validateAttribute($model, 'field');
$this->assertSame(['error.username_too_long'], $model->getErrors('field'));
@@ -50,8 +50,8 @@ class UsernameValidatorTest extends TestCase {
// TODO: rewrite this test with @provider usage
public function testValidateAttributePattern(): void {
$shouldBeValid = [
'русский_ник', 'русский_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!$%^&*()[]',
'[ESP]Эрик', 'Свят_помидор;', роблена_ў_беларусі:)',
'русский_ник', 'by_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!$%^&*()[]',
'[ESP]Эрик', 'Свят_помидор;',
];
foreach ($shouldBeValid as $nickname) {
$model = $this->createModel($nickname);

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\validators;
use Closure;
@@ -26,9 +28,11 @@ class UsernameValidator extends Validator {
$required = new validators\RequiredValidator();
$required->message = E::USERNAME_REQUIRED;
// Using username longer than 16 characters causes the "Failed to encode client:hello" error
// during server authentication on Minecraft 1.20 and above
$length = new validators\StringValidator();
$length->min = 3;
$length->max = 21;
$length->max = 16;
$length->tooShort = E::USERNAME_TOO_SHORT;
$length->tooLong = E::USERNAME_TOO_LONG;
@@ -54,7 +58,7 @@ class UsernameValidator extends Validator {
return null;
}
protected function executeValidation(Validator $validator, Model $model, string $attribute) {
protected function executeValidation(Validator $validator, Model $model, string $attribute): bool {
$validator->validateAttribute($model, $attribute);
return !$model->hasErrors($attribute);