Skip to content

Commit

Permalink
GH-150 Move username validation to a new mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Mar 19, 2022
1 parent 26a25cf commit 9f2ee3d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 32 deletions.
1 change: 1 addition & 0 deletions modules/registration/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
include($includePath . './utils/general.utils.php');
include($includePath . './utils/queries.utils.php');

include($includePath . './validators/validateInputs.validators.php');
include($includePath . './validators/validateTakenParams.validators.php');
include($includePath . './validators/validateReCaptcha.validators.php');

Expand Down
67 changes: 67 additions & 0 deletions modules/registration/validators/validateInputs.validators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace UniEngine\Engine\Modules\Registration\Validators;

function _createFuncWithResultHelpers($func) {
return function ($arguments) use ($func) {
$createSuccess = function ($payload) {
return [
'isSuccess' => true,
'payload' => $payload,
];
};
$createFailure = function ($payload) {
return [
'isSuccess' => false,
'error' => $payload,
];
};

return $func($arguments, [
'createSuccess' => $createSuccess,
'createFailure' => $createFailure,
]);
};
}

function _validateUsername($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['username'];

$minLength = 4;
$maxLenght = 64;

if (strlen($value) < $minLength) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_TOO_SHORT',
'minLength' => $minLength,
]);
}
if (strlen($value) > $maxLenght) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_TOO_LONG',
'maxLength' => $maxLenght,
]);
}
if (!preg_match(REGEXP_USERNAME_ABSOLUTE, $value)) {
return $resultHelpers['createFailure']([
'code' => 'USERNAME_INVALID',
]);
}

return $resultHelpers['createSuccess']([]);
};

return _createFuncWithResultHelpers($validator)($normalizedInput);
}

// Arguments
// - $normalizedInput (Object)
//
function validateInputs($normalizedInput) {
return [
'username' => _validateUsername($normalizedInput),
];
}

?>
58 changes: 26 additions & 32 deletions reg_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,35 @@
$JSONResponse = null;
$JSONResponse['Errors'] = array();

$userInput = Registration\Input\normalizeUserInput($_GET);
$normalizedInput = Registration\Input\normalizeUserInput($_GET);

$Username = $userInput['username'];
$Password = $userInput['password'];
$CheckEmail = $userInput['email']['original'];
$Email = $userInput['email']['escaped'];
$Rules = $userInput['hasAcceptedRules'];
$GalaxyNo = $userInput['galaxyNo'];
$LangCode = $userInput['langCode'];
$Username = $normalizedInput['username'];
$Password = $normalizedInput['password'];
$CheckEmail = $normalizedInput['email']['original'];
$Email = $normalizedInput['email']['escaped'];
$Rules = $normalizedInput['hasAcceptedRules'];
$GalaxyNo = $normalizedInput['galaxyNo'];
$LangCode = $normalizedInput['langCode'];

$userSessionIP = Users\Session\getCurrentIP();

// Check if Username is correct
$UsernameGood = false;
if(strlen($Username) < 4)
{
// Username is too short
$JSONResponse['Errors'][] = 1;
$JSONResponse['BadFields'][] = 'username';
}
else if(strlen($Username) > 64)
{
// Username is too long
$JSONResponse['Errors'][] = 2;
$JSONResponse['BadFields'][] = 'username';
}
else if(!preg_match(REGEXP_USERNAME_ABSOLUTE, $Username))
{
// Username has illegal signs
$JSONResponse['Errors'][] = 3;
$JSONResponse['BadFields'][] = 'username';
}
else
{
$UsernameGood = true;
$validationResult = Registration\Validators\validateInputs($normalizedInput);

if (!$validationResult['username']['isSuccess']) {
switch ($validationResult['username']['error']['code']) {
case 'USERNAME_TOO_SHORT':
$JSONResponse['Errors'][] = 1;
$JSONResponse['BadFields'][] = 'username';
break;
case 'USERNAME_TOO_LONG':
$JSONResponse['Errors'][] = 2;
$JSONResponse['BadFields'][] = 'username';
break;
case 'USERNAME_INVALID':
$JSONResponse['Errors'][] = 3;
$JSONResponse['BadFields'][] = 'username';
break;
}
}

// Check if Password is correct
Expand Down Expand Up @@ -146,7 +140,7 @@

if (
$EmailGood === true &&
$UsernameGood === true
$validationResult['username']['isSuccess'] === true
) {
$takenParamsValidationResult = Registration\Validators\validateTakenParams([
'username' => $Username,
Expand Down

0 comments on commit 9f2ee3d

Please sign in to comment.