Fix username validation and auto-generation, closes #927
This commit is contained in:
parent
c3dc376c4c
commit
bf537922a3
7 changed files with 159 additions and 41 deletions
36
app/Rules/Username.php
Normal file
36
app/Rules/Username.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class Username implements Rule
|
||||
{
|
||||
public const VALIDATION_REGEX = '/^[a-z0-9]([\w\.-]+)[a-z0-9]$/';
|
||||
|
||||
/**
|
||||
* Validate that a username contains only the allowed characters and starts/ends
|
||||
* with alpha-numeric characters.
|
||||
*
|
||||
* Allowed characters: a-z0-9_-.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
return preg_match(self::VALIDATION_REGEX, mb_strtolower($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a validation message for use when this rule fails.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return 'The :attribute must start and end with alpha-numeric characters and
|
||||
contain only letters, numbers, dashes, underscores, and periods.';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue