Skip to content

Commit

Permalink
GH-224 Move banned domain checker to a common function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 9, 2022
1 parent 24bf057 commit a962a4c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
2 changes: 2 additions & 0 deletions common/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

include($includePath . './exceptions.php');

include($includePath . './modules/uni/utils/isEmailDomainBanned.util.php');

?>
5 changes: 5 additions & 0 deletions common/modules/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
5 changes: 5 additions & 0 deletions common/modules/uni/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
5 changes: 5 additions & 0 deletions common/modules/uni/utils/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
18 changes: 18 additions & 0 deletions common/modules/uni/utils/isEmailDomainBanned.util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace UniEngine\Engine\Common\Modules\Uni\Utils;

function isEmailDomainBanned($emailAddress) {
global $_GameConfig;

$bannedDomains = $_GameConfig['BannedMailDomains'];
$bannedDomains = str_replace('.', '\.', $bannedDomains);

if (empty($bannedDomains)) {
return false;
}

return preg_match('#('.$bannedDomains.')+#si', $emailAddress) === 1;
}

?>
8 changes: 3 additions & 5 deletions modules/registration/validators/validateInputs.validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace UniEngine\Engine\Modules\Registration\Validators;

use UniEngine\Engine\Common\Modules\Uni;

function _validateUsername($normalizedInput) {
$validator = function ($input, $resultHelpers) {
$value = $input['username'];
Expand Down Expand Up @@ -54,12 +56,8 @@ function _validatePassword($normalizedInput) {

function _validateEmail($normalizedInput) {
$validator = function ($input, $resultHelpers) {
global $_GameConfig;

$value = $input['email'];

$bannedDomains = str_replace('.', '\.', $_GameConfig['BannedMailDomains']);

if (empty($value['escaped'])) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_EMPTY',
Expand All @@ -75,7 +73,7 @@ function _validateEmail($normalizedInput) {
'code' => 'EMAIL_INVALID',
]);
}
if (!empty($bannedDomains) && preg_match('#('.$bannedDomains.')+#si', $value['escaped'])) {
if (Uni\Utils\isEmailDomainBanned($value['escaped'])) {
return $resultHelpers['createFailure']([
'code' => 'EMAIL_ON_BANNED_DOMAIN',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@

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;
}
use UniEngine\Engine\Common\Modules\Uni;

/**
* @param array $params
Expand Down Expand Up @@ -55,7 +43,7 @@ function validateEmailChange($params) {
'code' => 'NEW_EMAIL_CONFIRMATION_INVALID',
]);
}
if (_isOnDomainBanlist($newEmailAddress)) {
if (Uni\Utils\isEmailDomainBanned($newEmailAddress)) {
return $resultHelpers['createFailure']([
'code' => 'BANNED_DOMAIN_USED',
]);
Expand Down

0 comments on commit a962a4c

Please sign in to comment.