From 7399d78c31f39fc9a356a40edc918ee61fe972d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Sat, 18 Jun 2022 22:57:15 +0200 Subject: [PATCH] GH-224 Move username change validator to an util --- modules/settings/_includes.php | 2 + .../validateUsernameChange.errorMapper.php | 29 +++++++ .../validateUsernameChange.validator.php | 80 +++++++++++++++++++ settings.php | 52 +++++------- 4 files changed, 131 insertions(+), 32 deletions(-) create mode 100644 modules/settings/utils/errorMappers/validateUsernameChange.errorMapper.php create mode 100644 modules/settings/utils/validators/validateUsernameChange.validator.php diff --git a/modules/settings/_includes.php b/modules/settings/_includes.php index 7c6bc812..4a96c586 100644 --- a/modules/settings/_includes.php +++ b/modules/settings/_includes.php @@ -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'); @@ -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'); }); diff --git a/modules/settings/utils/errorMappers/validateUsernameChange.errorMapper.php b/modules/settings/utils/errorMappers/validateUsernameChange.errorMapper.php new file mode 100644 index 00000000..da29758a --- /dev/null +++ b/modules/settings/utils/errorMappers/validateUsernameChange.errorMapper.php @@ -0,0 +1,29 @@ + $_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]; +} + +?> diff --git a/modules/settings/utils/validators/validateUsernameChange.validator.php b/modules/settings/utils/validators/validateUsernameChange.validator.php new file mode 100644 index 00000000..2910ccfc --- /dev/null +++ b/modules/settings/utils/validators/validateUsernameChange.validator.php @@ -0,0 +1,80 @@ + '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']); +} + +?> diff --git a/settings.php b/settings.php index b26746c1..3d49ef93 100644 --- a/settings.php +++ b/settings.php @@ -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 {