-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-224 Move username change validator to an util
- Loading branch information
Showing
4 changed files
with
131 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
modules/settings/utils/errorMappers/validateUsernameChange.errorMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
?> |
80 changes: 80 additions & 0 deletions
80
modules/settings/utils/validators/validateUsernameChange.validator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters