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 { public function checkAllowedDomains(string $email): bool {
$allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', ''); $allowedDomains = $this->config->getAppValue($this->appName, 'allowed_domains', '');
if ($allowedDomains !== '') { if ($allowedDomains !== '') {
$allowedDomains = explode(';', $allowedDomains); [,$mailDomain] = explode('@', strtolower($email), 2);
$allowed = false; $allowedDomains = explode(';', strtolower($allowedDomains));
foreach ($allowedDomains as $domain) { foreach ($allowedDomains as $domain) {
[,$mailDomain] = explode('@', $email, 2);
// valid domain, everything's fine // valid domain, everything's fine
if ($mailDomain === $domain) { if ($mailDomain === $domain) {
$allowed = true; return true;
break;
} }
} }
return $allowed; return false;
} }
return true; return true;
} }

View File

@ -60,7 +60,6 @@ class RegistrationServiceTest extends TestCase {
private $tokenProvider; private $tokenProvider;
/** @var ICrypto */ /** @var ICrypto */
private $crypto; private $crypto;
/** @var RegistrationService */ /** @var RegistrationService */
private $service; private $service;
@ -105,31 +104,30 @@ class RegistrationServiceTest extends TestCase {
); );
} }
public function testValidateNewEmail() { public function dataValidateEmail(): array {
$email = 'aaaa@example.com'; return [
['aaaa@example.com', ''],
$this->config->expects($this->once()) ['aaaa@example.com', 'example.com'],
->method('getAppValue') ['aaaa@example.com', 'eXample.com'],
->with('registration', 'allowed_domains', '') ['aaaa@eXample.com', 'example.com'],
->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);
} }
/** /**
* @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() { public function testValidateNewEmailNotWithinAllowedDomain() {
$email2 = 'bbbb@gmail.com'; $email2 = 'bbbb@gmail.com';