-
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 email change validation into utils
- Loading branch information
Showing
4 changed files
with
187 additions
and
100 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/validateEmailChange.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\validateEmailChange | ||
*/ | ||
function mapValidateEmailChangeErrorToReadableMessage($error) { | ||
global $_Lang; | ||
|
||
$errorCode = $error['code']; | ||
|
||
$knownErrorsByCode = [ | ||
'EMAIL_CHANGE_IN_PROGRESS' => $_Lang['Mail_alreadyInChange'], | ||
'INVALID_EMAIL' => $_Lang['Mail_badEmail'], | ||
'NEW_EMAIL_SAME_AS_OLD' => $_Lang['Mail_same_as_old'], | ||
'NEW_EMAIL_CONFIRMATION_INVALID' => $_Lang['Mail_Confirm_isbad'], | ||
'BANNED_DOMAIN_USED' => $_Lang['Mail_banned_domain'], | ||
'NEW_EMAIL_ALREADY_IN_USE' => $_Lang['Mail_some1_hasemail'], | ||
]; | ||
|
||
if (!isset($knownErrorsByCode[$errorCode])) { | ||
return $_Lang['fleet_generic_errors_unknown']; | ||
} | ||
|
||
return $knownErrorsByCode[$errorCode]; | ||
} | ||
|
||
?> |
83 changes: 83 additions & 0 deletions
83
modules/settings/utils/validators/validateEmailChange.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,83 @@ | ||
<?php | ||
|
||
namespace UniEngine\Engine\Modules\Settings\Utils\Validators; | ||
|
||
// TODO: Deduplicate, registration does the same thing | ||
function _isOnDomainBanlist($emailAddress) { | ||
global $_GameConfig; | ||
|
||
$bannedDomains = $_GameConfig['BannedMailDomains']; | ||
$bannedDomains = str_replace('.', '\.', $bannedDomains); | ||
|
||
if (empty($bannedDomains)) { | ||
return false; | ||
} | ||
|
||
return preg_match('#('.$bannedDomains.')+#si', $emailAddress) === 1; | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* @param array $params['input'] | ||
* @param string $params['input']['newEmailAddress'] | ||
* @param string $params['input']['newEmailAddressConfirm'] | ||
* @param arrayRef $params['currentUser'] | ||
* @param boolean $params['isAlreadyChangingEmail'] | ||
*/ | ||
function validateEmailChange($params) { | ||
$currentUser = &$params['currentUser']; | ||
$isAlreadyChangingEmail = $params['isAlreadyChangingEmail']; | ||
|
||
$executor = function ($input, $resultHelpers) use (&$currentUser, $isAlreadyChangingEmail) { | ||
$newEmailAddress = $input['newEmailAddress']; | ||
$newEmailAddressConfirm = $input['newEmailAddressConfirm']; | ||
|
||
$currentUserEmail = $currentUser['email']; | ||
|
||
if ($isAlreadyChangingEmail) { | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'EMAIL_CHANGE_IN_PROGRESS', | ||
]); | ||
} | ||
|
||
if (!is_email($newEmailAddress)) { | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'INVALID_EMAIL', | ||
]); | ||
} | ||
if ($newEmailAddress === $currentUserEmail) { | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'NEW_EMAIL_SAME_AS_OLD', | ||
]); | ||
} | ||
if ($newEmailAddress !== $newEmailAddressConfirm) { | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'NEW_EMAIL_CONFIRMATION_INVALID', | ||
]); | ||
} | ||
if (_isOnDomainBanlist($newEmailAddress)) { | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'BANNED_DOMAIN_USED', | ||
]); | ||
} | ||
|
||
$fetchExistingEmailFromDB = doquery( | ||
"SELECT `id` FROM {{table}} WHERE `email` = '{$newEmailAddress}' LIMIT 1;", | ||
'users', | ||
true | ||
); | ||
|
||
if ($fetchExistingEmailFromDB) { | ||
// TODO: Verify whether we should fetch email change processes as well | ||
return $resultHelpers['createFailure']([ | ||
'code' => 'NEW_EMAIL_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