Skip to content

Commit edd80b5

Browse files
authored
Merge pull request #53908 from nextcloud/backport/53887/stable30
2 parents f3855a1 + 9cbf2ed commit edd80b5

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

apps/provisioning_api/lib/Controller/AUserData.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
136136
$additionalEmails = $additionalEmailScopes = [];
137137
$emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
138138
foreach ($emailCollection->getProperties() as $property) {
139-
$additionalEmails[] = $property->getValue();
139+
$email = mb_strtolower(trim($property->getValue()));
140+
$additionalEmails[] = $email;
140141
if ($includeScopes) {
141142
$additionalEmailScopes[] = $property->getScope();
142143
}

apps/provisioning_api/lib/Controller/UsersController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ public function addUser(
531531
$generatePasswordResetToken = true;
532532
}
533533

534+
$email = mb_strtolower(trim($email));
534535
if ($email === '' && $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes') {
535536
throw new OCSException($this->l10n->t('Required email address was not provided'), 110);
536537
}
@@ -577,7 +578,7 @@ public function addUser(
577578

578579
// Send new user mail only if a mail is set
579580
if ($email !== '') {
580-
$newUser->setEMailAddress($email);
581+
$newUser->setSystemEMailAddress($email);
581582
if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
582583
try {
583584
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
@@ -837,6 +838,7 @@ public function editUserMultiValue(
837838
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
838839
$mailCollection->removePropertyByValue($key);
839840
if ($value !== '') {
841+
$value = mb_strtolower(trim($value));
840842
$mailCollection->addPropertyWithDefaults($value);
841843
$property = $mailCollection->getPropertyByValue($key);
842844
if ($isAdminOrSubadmin && $property) {
@@ -1119,13 +1121,15 @@ public function editUser(string $userId, string $key, string $value): DataRespon
11191121
}
11201122
break;
11211123
case IAccountManager::PROPERTY_EMAIL:
1124+
$value = mb_strtolower(trim($value));
11221125
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
1123-
$targetUser->setEMailAddress($value);
1126+
$targetUser->setSystemEMailAddress($value);
11241127
} else {
11251128
throw new OCSException('', 101);
11261129
}
11271130
break;
11281131
case IAccountManager::COLLECTION_EMAIL:
1132+
$value = mb_strtolower(trim($value));
11291133
if (filter_var($value, FILTER_VALIDATE_EMAIL) && $value !== $targetUser->getSystemEMailAddress()) {
11301134
$userAccount = $this->accountManager->getAccount($targetUser);
11311135
$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
@@ -621,7 +621,7 @@ public function testAddUserSuccessfulGeneratePassword() {
621621
->willReturn(false);
622622
$newUser = $this->createMock(IUser::class);
623623
$newUser->expects($this->once())
624-
->method('setEMailAddress');
624+
->method('setSystemEMailAddress');
625625
$this->userManager
626626
->expects($this->once())
627627
->method('createUser')
@@ -657,6 +657,51 @@ public function testAddUserSuccessfulGeneratePassword() {
657657
));
658658
}
659659

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

661706
public function testAddUserFailedToGenerateUserID() {
662707
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
@@ -1627,7 +1672,7 @@ public function testEditUserRegularUserSelfEditChangeEmailValid() {
16271672
->willReturn($targetUser);
16281673
$targetUser
16291674
->expects($this->once())
1630-
->method('setEMailAddress')
1675+
->method('setSystemEMailAddress')
16311676
->with('demo@nextcloud.com');
16321677
$targetUser
16331678
->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;
@@ -496,14 +498,16 @@ public function getEMailAddress() {
496498
* @inheritDoc
497499
*/
498500
public function getSystemEMailAddress(): ?string {
499-
return $this->config->getUserValue($this->uid, 'settings', 'email', null);
501+
$email = $this->config->getUserValue($this->uid, 'settings', 'email', null);
502+
return $email ? mb_strtolower(trim($email)) : null;
500503
}
501504

502505
/**
503506
* @inheritDoc
504507
*/
505508
public function getPrimaryEMailAddress(): ?string {
506-
return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
509+
$email = $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
510+
return $email ? mb_strtolower(trim($email)) : null;
507511
}
508512

509513
/**

0 commit comments

Comments
 (0)