Skip to content

Commit

Permalink
GH-224 Move username change validator to an util
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 18, 2022
1 parent a9768e6 commit 7399d78
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 32 deletions.
2 changes: 2 additions & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
include($includePath . './utils/errorMappers/tryIgnoreUser.errorMapper.php');
include($includePath . './utils/errorMappers/validatePasswordChange.errorMapper.php');
include($includePath . './utils/errorMappers/validateEmailChange.errorMapper.php');
include($includePath . './utils/errorMappers/validateUsernameChange.errorMapper.php');

include($includePath . './utils/helpers/tryDeleteUserIgnoreEntries.helper.php');
include($includePath . './utils/helpers/tryEnableVacation.helper.php');
Expand All @@ -33,6 +34,7 @@
include($includePath . './utils/validators/validatePasswordChange.validator.php');
include($includePath . './utils/validators/validateEmailChange.validator.php');
include($includePath . './utils/validators/validateResourcesOrdering.validator.php');
include($includePath . './utils/validators/validateUsernameChange.validator.php');

});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\ErrorMappers;

/**
* @param object $error As returned by Settings\Utils\Validators\validateUsernameChange
*/
function mapValidateUsernameChangeErrorToReadableMessage($error) {
global $_Lang;

$errorCode = $error['code'];

$knownErrorsByCode = [
'NOT_ENOUGH_DARK_ENERGY' => $_Lang['NewNick_donthave_DE'],
'NEW_USERNAME_SAME_AS_OLD' => $_Lang['NewNick_is_like_old'],
'NEW_USERNAME_TOO_SHORT' => $_Lang['NewNick_is_tooshort'],
'NEW_USERNAME_LINK_FORBIDDEN' => $_Lang['NewNick_nolinks'],
'NEW_USERNAME_INVALID_CHARACTERS' => $_Lang['NewNick_badSigns'],
'NEW_USERNAME_ALREADY_IN_USE' => $_Lang['NewNick_already_taken'],
];

if (!isset($knownErrorsByCode[$errorCode])) {
return $_Lang['fleet_generic_errors_unknown'];
}

return $knownErrorsByCode[$errorCode];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Utils\Validators;

/**
* @param array $params
* @param array $params['input']
* @param string $params['input']['newUsername']
* @param arrayRef $params['currentUser']
*/
function validateUsernameChange($params) {
$currentUser = &$params['currentUser'];

$executor = function ($input, $resultHelpers) use (&$currentUser) {
$newUsername = $input['newUsername'];
$currentUsername = $currentUser['username'];

$CHANGE_COST = 10;
$USERNAME_MIN_LENGTH = 4;

if ($currentUser['darkEnergy'] < $CHANGE_COST) {
return $resultHelpers['createFailure']([
'code' => 'NOT_ENOUGH_DARK_ENERGY',
'params' => [
'cost' => $CHANGE_COST,
],
]);
}
if ($newUsername === $currentUsername) {
return $resultHelpers['createFailure']([
'code' => 'NEW_USERNAME_SAME_AS_OLD',
]);
}
if (strlen($newUsername) < $USERNAME_MIN_LENGTH) {
return $resultHelpers['createFailure']([
'code' => 'NEW_USERNAME_TOO_SHORT',
'params' => [
'minLength' => $USERNAME_MIN_LENGTH,
],
]);
}
if (
strstr($newUsername, 'http') ||
strstr($newUsername, 'www.')
) {
return $resultHelpers['createFailure']([
'code' => 'NEW_USERNAME_LINK_FORBIDDEN',
]);
}
if (!preg_match(REGEXP_USERNAME_ABSOLUTE, $newUsername)) {
return $resultHelpers['createFailure']([
'code' => 'NEW_USERNAME_INVALID_CHARACTERS',
]);
}

$fetchExistingUsernameFromDB = doquery(
"SELECT " .
"`id` " .
"FROM {{table}} " .
"WHERE " .
"`username` = '{$newUsername}' " .
"LIMIT 1 ".
";",
'users',
true
);

if ($fetchExistingUsernameFromDB) {
return $resultHelpers['createFailure']([
'code' => 'NEW_USERNAME_ALREADY_IN_USE',
]);
}

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

return createFuncWithResultHelpers($executor)($params['input']);
}

?>
52 changes: 20 additions & 32 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,40 +1178,28 @@
else if($Mode == 'nickchange')
{
// User is trying to change his nickname
if(!empty($_POST['newnick']))
{
// Nickname Change in progress
if($_User['darkEnergy'] < 10)
{
message($_Lang['NewNick_donthave_DE'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}
if (!empty($_POST['newnick'])) {
$NewNick = trim($_POST['newnick']);
if($NewNick == $_User['username'])
{
message($_Lang['NewNick_is_like_old'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}
if(strlen($NewNick) < 4)
{
message($_Lang['NewNick_is_tooshort'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}
if(strstr($NewNick, 'http') OR strstr($NewNick, 'www.'))
{
message($_Lang['NewNick_nolinks'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}
if(!preg_match(REGEXP_USERNAME_ABSOLUTE, $NewNick))
{
message($_Lang['NewNick_badSigns'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}
$SelectNewNick = doquery("SELECT `id` FROM {{table}} WHERE `username` = '{$NewNick}' LIMIT 1;", 'users', true);
if($SelectNewNick['id'] > 0)
{
message($_Lang['NewNick_already_taken'], $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
}

doquery("UPDATE {{table}} SET `darkEnergy` = `darkEnergy` - 10, `username` = '{$NewNick}', `old_username` = '{$_User['username']}', `old_username_expire` = UNIX_TIMESTAMP() + (7*24*60*60) WHERE `id` = {$_User['id']} LIMIT 1;", 'users');
doquery("INSERT INTO {{table}} VALUES(NULL, {$_User['id']}, UNIX_TIMESTAMP(), '{$NewNick}', '{$_User['username']}');", 'nick_changelog');
setcookie(getSessionCookieKey(), '', $Now - 3600, '/', '');
message($_Lang['NewNick_saved'], $_Lang['NickChange_Title'], 'login.php');
$usernameChangeValidationResult = Settings\Utils\Validators\validateUsernameChange([
'input' => [
'newUsername' => $NewNick,
],
'currentUser' => &$_User,
]);

if (!$usernameChangeValidationResult['isSuccess']) {
$errorMessage = Settings\Utils\ErrorMappers\mapValidateUsernameChangeErrorToReadableMessage(
$usernameChangeValidationResult['error']
);

message($errorMessage, $_Lang['NickChange_Title'], 'settings.php?mode=nickchange');
} else {
doquery("UPDATE {{table}} SET `darkEnergy` = `darkEnergy` - 10, `username` = '{$NewNick}', `old_username` = '{$_User['username']}', `old_username_expire` = UNIX_TIMESTAMP() + (7*24*60*60) WHERE `id` = {$_User['id']} LIMIT 1;", 'users');
doquery("INSERT INTO {{table}} VALUES(NULL, {$_User['id']}, UNIX_TIMESTAMP(), '{$NewNick}', '{$_User['username']}');", 'nick_changelog');
setcookie(getSessionCookieKey(), '', $Now - 3600, '/', '');
message($_Lang['NewNick_saved'], $_Lang['NickChange_Title'], 'login.php');
}
}
else
{
Expand Down

0 comments on commit 7399d78

Please sign in to comment.