From fffb6278444e97b087955e903128a21701bc3b31 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 10 Jul 2020 10:47:46 +0200 Subject: [PATCH] Make the email domain check case insensitive Signed-off-by: Joas Schilling --- lib/Service/RegistrationService.php | 11 +++-- .../Service/RegistrationServiceTest.php | 42 +++++++++---------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/lib/Service/RegistrationService.php b/lib/Service/RegistrationService.php index 26bf83b..a02b2bd 100644 --- a/lib/Service/RegistrationService.php +++ b/lib/Service/RegistrationService.php @@ -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; } diff --git a/tests/Integration/Service/RegistrationServiceTest.php b/tests/Integration/Service/RegistrationServiceTest.php index 4130ffd..00413bf 100644 --- a/tests/Integration/Service/RegistrationServiceTest.php +++ b/tests/Integration/Service/RegistrationServiceTest.php @@ -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';