Skip to content

Commit 523fb38

Browse files
committed
fix: force lowercase emails
Signed-off-by: skjnldsv <[email protected]>
1 parent b38894c commit 523fb38

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

apps/provisioning_api/lib/Controller/AUserDataOCSController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
141141
$additionalEmails = $additionalEmailScopes = [];
142142
$emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
143143
foreach ($emailCollection->getProperties() as $property) {
144-
$additionalEmails[] = $property->getValue();
144+
$email = mb_strtolower(trim($property->getValue()));
145+
$additionalEmails[] = $email;
145146
if ($includeScopes) {
146147
$additionalEmailScopes[] = $property->getScope();
147148
}

apps/provisioning_api/lib/Controller/UsersController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ public function addUser(
537537
$generatePasswordResetToken = true;
538538
}
539539

540+
$email = mb_strtolower(trim($email));
540541
if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') {
541542
throw new OCSException($this->l10n->t('Required email address was not provided'), 110);
542543
}
@@ -583,7 +584,7 @@ public function addUser(
583584

584585
// Send new user mail only if a mail is set
585586
if ($email !== '') {
586-
$newUser->setEMailAddress($email);
587+
$newUser->setSystemEMailAddress($email);
587588
if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
588589
try {
589590
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
@@ -857,6 +858,7 @@ public function editUserMultiValue(
857858
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
858859
$mailCollection->removePropertyByValue($key);
859860
if ($value !== '') {
861+
$value = mb_strtolower(trim($value));
860862
$mailCollection->addPropertyWithDefaults($value);
861863
$property = $mailCollection->getPropertyByValue($key);
862864
if ($isAdminOrSubadmin && $property) {
@@ -1142,13 +1144,15 @@ public function editUser(string $userId, string $key, string $value): DataRespon
11421144
}
11431145
break;
11441146
case IAccountManager::PROPERTY_EMAIL:
1147+
$value = mb_strtolower(trim($value));
11451148
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
1146-
$targetUser->setEMailAddress($value);
1149+
$targetUser->setSystemEMailAddress($value);
11471150
} else {
11481151
throw new OCSException('', 101);
11491152
}
11501153
break;
11511154
case IAccountManager::COLLECTION_EMAIL:
1155+
$value = mb_strtolower(trim($value));
11521156
if (filter_var($value, FILTER_VALIDATE_EMAIL) && $value !== $targetUser->getSystemEMailAddress()) {
11531157
$userAccount = $this->accountManager->getAccount($targetUser);
11541158
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);

apps/provisioning_api/tests/Controller/UsersControllerTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public function testAddUserSuccessfulGeneratePassword(): void {
632632
->willReturn(false);
633633
$newUser = $this->createMock(IUser::class);
634634
$newUser->expects($this->once())
635-
->method('setEMailAddress');
635+
->method('setSystemEMailAddress');
636636
$this->userManager
637637
->expects($this->once())
638638
->method('createUser')
@@ -668,6 +668,51 @@ public function testAddUserSuccessfulGeneratePassword(): void {
668668
));
669669
}
670670

671+
public function testAddUserSuccessfulLowercaseEmail(): void {
672+
$this->userManager
673+
->expects($this->once())
674+
->method('userExists')
675+
->with('NewUser')
676+
->willReturn(false);
677+
$newUser = $this->createMock(IUser::class);
678+
$newUser->expects($this->once())
679+
->method('setSystemEMailAddress')
680+
->with('[email protected]');
681+
$this->userManager
682+
->expects($this->once())
683+
->method('createUser')
684+
->willReturn($newUser);
685+
$this->logger
686+
->expects($this->once())
687+
->method('info')
688+
->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
689+
$loggedInUser = $this->getMockBuilder(IUser::class)
690+
->disableOriginalConstructor()
691+
->getMock();
692+
$loggedInUser
693+
->expects($this->exactly(2))
694+
->method('getUID')
695+
->willReturn('adminUser');
696+
$this->userSession
697+
->expects($this->once())
698+
->method('getUser')
699+
->willReturn($loggedInUser);
700+
$this->groupManager
701+
->expects($this->once())
702+
->method('isAdmin')
703+
->with('adminUser')
704+
->willReturn(true);
705+
$this->eventDispatcher
706+
->expects($this->once())
707+
->method('dispatchTyped')
708+
->with(new GenerateSecurePasswordEvent());
709+
710+
$this->assertTrue(key_exists(
711+
'id',
712+
$this->api->addUser('NewUser', '', '', '[email protected]')->getData()
713+
));
714+
}
715+
671716

672717
public function testAddUserFailedToGenerateUserID(): void {
673718
$this->expectException(OCSException::class);
@@ -1662,7 +1707,7 @@ public function testEditUserRegularUserSelfEditChangeEmailValid(): void {
16621707
->willReturn($targetUser);
16631708
$targetUser
16641709
->expects($this->once())
1665-
->method('setEMailAddress')
1710+
->method('setSystemEMailAddress')
16661711
->with('[email protected]');
16671712
$targetUser
16681713
->expects($this->any())

core/Command/User/Setting.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155155
$user = $this->userManager->get($uid);
156156
if ($user instanceof IUser) {
157157
if ($key === 'email') {
158-
$user->setEMailAddress($input->getArgument('value'));
158+
$email = $input->getArgument('value');
159+
$user->setSystemEMailAddress(mb_strtolower(trim($email)));
159160
} elseif ($key === 'display_name') {
160161
if (!$user->setDisplayName($input->getArgument('value'))) {
161162
if ($user->getDisplayName() === $input->getArgument('value')) {

lib/private/User/User.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function setEMailAddress($mailAddress) {
155155
*/
156156
public function setSystemEMailAddress(string $mailAddress): void {
157157
$oldMailAddress = $this->getSystemEMailAddress();
158+
$mailAddress = mb_strtolower(trim($mailAddress));
158159

159160
if ($mailAddress === '') {
160161
$this->config->deleteUserValue($this->uid, 'settings', 'email');
@@ -177,6 +178,7 @@ public function setSystemEMailAddress(string $mailAddress): void {
177178
* @inheritDoc
178179
*/
179180
public function setPrimaryEMailAddress(string $mailAddress): void {
181+
$mailAddress = mb_strtolower(trim($mailAddress));
180182
if ($mailAddress === '') {
181183
$this->config->deleteUserValue($this->uid, 'settings', 'primary_email');
182184
return;
@@ -515,14 +517,16 @@ public function getEMailAddress() {
515517
* @inheritDoc
516518
*/
517519
public function getSystemEMailAddress(): ?string {
518-
return $this->config->getUserValue($this->uid, 'settings', 'email', null);
520+
$email = $this->config->getUserValue($this->uid, 'settings', 'email', null);
521+
return $email ? mb_strtolower(trim($email)) : null;
519522
}
520523

521524
/**
522525
* @inheritDoc
523526
*/
524527
public function getPrimaryEMailAddress(): ?string {
525-
return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
528+
$email = $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
529+
return $email ? mb_strtolower(trim($email)) : null;
526530
}
527531

528532
/**

0 commit comments

Comments
 (0)