Skip to content

Commit de53ca4

Browse files
committed
fix: force lowercase emails
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 6cc5484 commit de53ca4

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
@@ -142,7 +142,8 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
142142
$additionalEmails = $additionalEmailScopes = [];
143143
$emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
144144
foreach ($emailCollection->getProperties() as $property) {
145-
$additionalEmails[] = $property->getValue();
145+
$email = mb_strtolower(trim($property->getValue()));
146+
$additionalEmails[] = $email;
146147
if ($includeScopes) {
147148
$additionalEmailScopes[] = $property->getScope();
148149
}

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
@@ -609,7 +609,7 @@ public function testAddUserSuccessfulGeneratePassword(): void {
609609
->willReturn(false);
610610
$newUser = $this->createMock(IUser::class);
611611
$newUser->expects($this->once())
612-
->method('setEMailAddress');
612+
->method('setSystemEMailAddress');
613613
$this->userManager
614614
->expects($this->once())
615615
->method('createUser')
@@ -645,6 +645,51 @@ public function testAddUserSuccessfulGeneratePassword(): void {
645645
));
646646
}
647647

648+
public function testAddUserSuccessfulLowercaseEmail(): void {
649+
$this->userManager
650+
->expects($this->once())
651+
->method('userExists')
652+
->with('NewUser')
653+
->willReturn(false);
654+
$newUser = $this->createMock(IUser::class);
655+
$newUser->expects($this->once())
656+
->method('setSystemEMailAddress')
657+
->with('foo@bar.com');
658+
$this->userManager
659+
->expects($this->once())
660+
->method('createUser')
661+
->willReturn($newUser);
662+
$this->logger
663+
->expects($this->once())
664+
->method('info')
665+
->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
666+
$loggedInUser = $this->getMockBuilder(IUser::class)
667+
->disableOriginalConstructor()
668+
->getMock();
669+
$loggedInUser
670+
->expects($this->exactly(2))
671+
->method('getUID')
672+
->willReturn('adminUser');
673+
$this->userSession
674+
->expects($this->once())
675+
->method('getUser')
676+
->willReturn($loggedInUser);
677+
$this->groupManager
678+
->expects($this->once())
679+
->method('isAdmin')
680+
->with('adminUser')
681+
->willReturn(true);
682+
$this->eventDispatcher
683+
->expects($this->once())
684+
->method('dispatchTyped')
685+
->with(new GenerateSecurePasswordEvent());
686+
687+
$this->assertTrue(key_exists(
688+
'id',
689+
$this->api->addUser('NewUser', '', '', 'fOo@BaR.CoM')->getData()
690+
));
691+
}
692+
648693

649694
public function testAddUserFailedToGenerateUserID(): void {
650695
$this->expectException(OCSException::class);
@@ -1629,7 +1674,7 @@ public function testEditUserRegularUserSelfEditChangeEmailValid(): void {
16291674
->willReturn($targetUser);
16301675
$targetUser
16311676
->expects($this->once())
1632-
->method('setEMailAddress')
1677+
->method('setSystemEMailAddress')
16331678
->with('demo@nextcloud.com');
16341679
$targetUser
16351680
->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
@@ -154,6 +154,7 @@ public function setEMailAddress($mailAddress) {
154154
*/
155155
public function setSystemEMailAddress(string $mailAddress): void {
156156
$oldMailAddress = $this->getSystemEMailAddress();
157+
$mailAddress = mb_strtolower(trim($mailAddress));
157158

158159
if ($mailAddress === '') {
159160
$this->config->deleteUserValue($this->uid, 'settings', 'email');
@@ -176,6 +177,7 @@ public function setSystemEMailAddress(string $mailAddress): void {
176177
* @inheritDoc
177178
*/
178179
public function setPrimaryEMailAddress(string $mailAddress): void {
180+
$mailAddress = mb_strtolower(trim($mailAddress));
179181
if ($mailAddress === '') {
180182
$this->config->deleteUserValue($this->uid, 'settings', 'primary_email');
181183
return;
@@ -514,14 +516,16 @@ public function getEMailAddress() {
514516
* @inheritDoc
515517
*/
516518
public function getSystemEMailAddress(): ?string {
517-
return $this->config->getUserValue($this->uid, 'settings', 'email', null);
519+
$email = $this->config->getUserValue($this->uid, 'settings', 'email', null);
520+
return $email ? mb_strtolower(trim($email)) : null;
518521
}
519522

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

527531
/**

0 commit comments

Comments
 (0)