Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
$additionalEmails = $additionalEmailScopes = [];
$emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
foreach ($emailCollection->getProperties() as $property) {
$additionalEmails[] = $property->getValue();
$email = mb_strtolower(trim($property->getValue()));
$additionalEmails[] = $email;
if ($includeScopes) {
$additionalEmailScopes[] = $property->getScope();
}
Expand Down
8 changes: 6 additions & 2 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ public function addUser(
$generatePasswordResetToken = true;
}

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

// Send new user mail only if a mail is set
if ($email !== '') {
$newUser->setEMailAddress($email);
$newUser->setSystemEMailAddress($email);
if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
try {
$emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken);
Expand Down Expand Up @@ -857,6 +858,7 @@ public function editUserMultiValue(
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
$mailCollection->removePropertyByValue($key);
if ($value !== '') {
$value = mb_strtolower(trim($value));
$mailCollection->addPropertyWithDefaults($value);
$property = $mailCollection->getPropertyByValue($key);
if ($isAdminOrSubadmin && $property) {
Expand Down Expand Up @@ -1142,13 +1144,15 @@ public function editUser(string $userId, string $key, string $value): DataRespon
}
break;
case IAccountManager::PROPERTY_EMAIL:
$value = mb_strtolower(trim($value));
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
$targetUser->setEMailAddress($value);
$targetUser->setSystemEMailAddress($value);
} else {
throw new OCSException('', 101);
}
break;
case IAccountManager::COLLECTION_EMAIL:
$value = mb_strtolower(trim($value));
if (filter_var($value, FILTER_VALIDATE_EMAIL) && $value !== $targetUser->getSystemEMailAddress()) {
$userAccount = $this->accountManager->getAccount($targetUser);
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
Expand Down
49 changes: 47 additions & 2 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public function testAddUserSuccessfulGeneratePassword(): void {
->willReturn(false);
$newUser = $this->createMock(IUser::class);
$newUser->expects($this->once())
->method('setEMailAddress');
->method('setSystemEMailAddress');
$this->userManager
->expects($this->once())
->method('createUser')
Expand Down Expand Up @@ -668,6 +668,51 @@ public function testAddUserSuccessfulGeneratePassword(): void {
));
}

public function testAddUserSuccessfulLowercaseEmail(): void {
$this->userManager
->expects($this->once())
->method('userExists')
->with('NewUser')
->willReturn(false);
$newUser = $this->createMock(IUser::class);
$newUser->expects($this->once())
->method('setSystemEMailAddress')
->with('foo@bar.com');
$this->userManager
->expects($this->once())
->method('createUser')
->willReturn($newUser);
$this->logger
->expects($this->once())
->method('info')
->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->exactly(2))
->method('getUID')
->willReturn('adminUser');
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('adminUser')
->willReturn(true);
$this->eventDispatcher
->expects($this->once())
->method('dispatchTyped')
->with(new GenerateSecurePasswordEvent());

$this->assertTrue(key_exists(
'id',
$this->api->addUser('NewUser', '', '', 'fOo@BaR.CoM')->getData()
));
}


public function testAddUserFailedToGenerateUserID(): void {
$this->expectException(OCSException::class);
Expand Down Expand Up @@ -1662,7 +1707,7 @@ public function testEditUserRegularUserSelfEditChangeEmailValid(): void {
->willReturn($targetUser);
$targetUser
->expects($this->once())
->method('setEMailAddress')
->method('setSystemEMailAddress')
->with('demo@nextcloud.com');
$targetUser
->expects($this->any())
Expand Down
3 changes: 2 additions & 1 deletion core/Command/User/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
if ($key === 'email') {
$user->setEMailAddress($input->getArgument('value'));
$email = $input->getArgument('value');
$user->setSystemEMailAddress(mb_strtolower(trim($email)));
} elseif ($key === 'display_name') {
if (!$user->setDisplayName($input->getArgument('value'))) {
if ($user->getDisplayName() === $input->getArgument('value')) {
Expand Down
8 changes: 6 additions & 2 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function setEMailAddress($mailAddress) {
*/
public function setSystemEMailAddress(string $mailAddress): void {
$oldMailAddress = $this->getSystemEMailAddress();
$mailAddress = mb_strtolower(trim($mailAddress));

if ($mailAddress === '') {
$this->config->deleteUserValue($this->uid, 'settings', 'email');
Expand All @@ -177,6 +178,7 @@ public function setSystemEMailAddress(string $mailAddress): void {
* @inheritDoc
*/
public function setPrimaryEMailAddress(string $mailAddress): void {
$mailAddress = mb_strtolower(trim($mailAddress));
if ($mailAddress === '') {
$this->config->deleteUserValue($this->uid, 'settings', 'primary_email');
return;
Expand Down Expand Up @@ -515,14 +517,16 @@ public function getEMailAddress() {
* @inheritDoc
*/
public function getSystemEMailAddress(): ?string {
return $this->config->getUserValue($this->uid, 'settings', 'email', null);
$email = $this->config->getUserValue($this->uid, 'settings', 'email', null);
return $email ? mb_strtolower(trim($email)) : null;
}

/**
* @inheritDoc
*/
public function getPrimaryEMailAddress(): ?string {
return $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
$email = $this->config->getUserValue($this->uid, 'settings', 'primary_email', null);
return $email ? mb_strtolower(trim($email)) : null;
}

/**
Expand Down
Loading