Make the email domain check case insensitive

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-10 10:47:46 +02:00
parent 0192fa9deb
commit fffb627844
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 25 additions and 28 deletions

View File

@ -217,17 +217,16 @@ class RegistrationService {
public function checkAllowedDomains(string $email): bool {
$allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
if ($allowedDomains !== '') {
$allowedDomains = explode(';', $allowedDomains);
$allowed = false;
[,$mailDomain] = explode('@', strtolower($email), 2);
$allowedDomains = explode(';', strtolower($allowedDomains));
foreach ($allowedDomains as $domain) {
[,$mailDomain] = explode('@', $email, 2);
// valid domain, everything's fine
if ($mailDomain === $domain) {
$allowed = true;
break;
return true;
}
}
return $allowed;
return false;
}
return true;
}

View File

@ -60,7 +60,6 @@ class RegistrationServiceTest extends TestCase {
private $tokenProvider;
/** @var ICrypto */
private $crypto;
/** @var RegistrationService */
private $service;
@ -105,31 +104,30 @@ class RegistrationServiceTest extends TestCase {
);
}
public function testValidateNewEmail() {
$email = 'aaaa@example.com';
$this->config->expects($this->once())
->method('getAppValue')
->with('registration', 'allowed_domains', '')
->willReturn('');
$this->service->validateEmail($email);
}
public function testValidateNewEmailWithinAllowedDomain() {
$email = 'aaaa@example.com';
$this->config->expects($this->atLeastOnce())
->method('getAppValue')
->with('registration', 'allowed_domains', '')
->willReturn('example.com');
$this->service->validateEmail($email);
public function dataValidateEmail(): array {
return [
['aaaa@example.com', ''],
['aaaa@example.com', 'example.com'],
['aaaa@example.com', 'eXample.com'],
['aaaa@eXample.com', 'example.com'],
];
}
/**
* @depends testValidateNewEmailWithinAllowedDomain
* @dataProvider dataValidateEmail
* @param string $email
* @param string $allowedDomains
* @throws RegistrationException
*/
public function testValidateEmail(string $email, string $allowedDomains) {
$this->config->expects($this->once())
->method('getAppValue')
->with('registration', 'allowed_domains', '')
->willReturn($allowedDomains);
$this->service->validateEmail($email);
}
public function testValidateNewEmailNotWithinAllowedDomain() {
$email2 = 'bbbb@gmail.com';