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
8 changes: 4 additions & 4 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,11 @@ public function editUser(string $userId, string $key, string $value): DataRespon
if (filter_var($value, FILTER_VALIDATE_EMAIL) && $value !== $targetUser->getSystemEMailAddress()) {
$userAccount = $this->accountManager->getAccount($targetUser);
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
foreach ($mailCollection->getProperties() as $property) {
if ($property->getValue() === $value) {
break;
}

if ($mailCollection->getPropertyByValue($value)) {
throw new OCSException('', 102);
}

$mailCollection->addPropertyWithDefaults($value);
$this->accountManager->updateAccount($userAccount);
} else {
Expand Down
157 changes: 157 additions & 0 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -1544,7 +1546,162 @@ public function testEditUserRegularUserSelfEditChangeEmailValid() {
$this->assertEquals([], $this->api->editUser('UserToEdit', 'email', '[email protected]')->getData());
}

public function testEditUserRegularUserSelfEditAddAdditionalEmailValid(): void {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->any())
->method('getUID')
->willReturn('UID');
$targetUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
->expects($this->once())
->method('get')
->with('UserToEdit')
->willReturn($targetUser);
$targetUser
->expects($this->any())
->method('getUID')
->willReturn('UID');

$backend = $this->createMock(UserInterface::class);
$targetUser
->expects($this->any())
->method('getBackend')
->willReturn($backend);

$userAccount = $this->createMock(IAccount::class);

$this->accountManager
->expects($this->once())
->method('getAccount')
->with($targetUser)
->willReturn($userAccount);
$this->accountManager
->expects($this->once())
->method('updateAccount')
->with($userAccount);

$this->assertEquals([], $this->api->editUser('UserToEdit', 'additional_mail', '[email protected]')->getData());
}

public function testEditUserRegularUserSelfEditAddAdditionalEmailMainAddress(): void {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->any())
->method('getUID')
->willReturn('UID');
$targetUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
->expects($this->once())
->method('get')
->with('UserToEdit')
->willReturn($targetUser);
$targetUser
->expects($this->any())
->method('getUID')
->willReturn('UID');

$backend = $this->createMock(UserInterface::class);
$targetUser
->expects($this->any())
->method('getBackend')
->willReturn($backend);
$targetUser
->expects($this->any())
->method('getSystemEMailAddress')
->willReturn('[email protected]');

$userAccount = $this->createMock(IAccount::class);

$this->accountManager
->expects($this->never())
->method('getAccount')
->with($targetUser)
->willReturn($userAccount);
$this->accountManager
->expects($this->never())
->method('updateAccount')
->with($userAccount);

$this->expectException(OCSException::class);
$this->expectExceptionCode(102);
$this->api->editUser('UserToEdit', 'additional_mail', '[email protected]')->getData();
}

public function testEditUserRegularUserSelfEditAddAdditionalEmailDuplicate(): void {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->any())
->method('getUID')
->willReturn('UID');
$targetUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($loggedInUser);
$this->userManager
->expects($this->once())
->method('get')
->with('UserToEdit')
->willReturn($targetUser);
$targetUser
->expects($this->any())
->method('getUID')
->willReturn('UID');

$backend = $this->createMock(UserInterface::class);
$targetUser
->expects($this->any())
->method('getBackend')
->willReturn($backend);

$property = $this->createMock(IAccountProperty::class);
$property->method('getValue')
->willReturn('[email protected]');
$collection = $this->createMock(IAccountPropertyCollection::class);
$collection->method('getPropertyByValue')
->with('[email protected]')
->willReturn($property);

$userAccount = $this->createMock(IAccount::class);
$userAccount->method('getPropertyCollection')
->with(IAccountManager::COLLECTION_EMAIL)
->willReturn($collection);

$this->accountManager
->expects($this->once())
->method('getAccount')
->with($targetUser)
->willReturn($userAccount);
$this->accountManager
->expects($this->never())
->method('updateAccount')
->with($userAccount);

$this->expectException(OCSException::class);
$this->expectExceptionCode(102);
$this->api->editUser('UserToEdit', 'additional_mail', '[email protected]')->getData();
}

public function testEditUserRegularUserSelfEditChangeEmailInvalid() {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class);
Expand Down
44 changes: 33 additions & 11 deletions build/integration/features/provisioning-v1.feature
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ Feature: provisioning
| value | private |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | email |
| value | [email protected] |
And the OCS status code should be "100"
And the HTTP status code should be "200"
# Duplicating primary address
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | [email protected] |
And the OCS status code should be "102"
And the HTTP status code should be "200"
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | [email protected] |
And the OCS status code should be "100"
And the HTTP status code should be "200"
# Duplicating another additional address
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | [email protected] |
And the OCS status code should be "102"
And the HTTP status code should be "200"
Then user "brand-new-user" has
| id | brand-new-user |
| phoneScope | v2-private |
Expand All @@ -214,21 +236,21 @@ Feature: provisioning
And As an "brand-new-user"
When sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | no.reply@nextcloud.com |
| value | no.reply3@nextcloud.com |
And the OCS status code should be "100"
And the HTTP status code should be "200"
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | noreply@nextcloud.com |
| value | noreply4@nextcloud.com |
And the OCS status code should be "100"
And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user/additional_mailScope" with
| key | no.reply@nextcloud.com |
| key | no.reply3@nextcloud.com |
| value | v2-federated |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user/additional_mailScope" with
| key | noreply@nextcloud.com |
| key | noreply4@nextcloud.com |
| value | v2-published |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
Expand Down Expand Up @@ -260,11 +282,11 @@ Feature: provisioning
And As an "brand-new-user"
When sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | no.reply@nextcloud.com |
| value | no.reply5@nextcloud.com |
And the OCS status code should be "100"
And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user/additional_mailScope" with
| key | no.reply@nextcloud.com |
| key | no.reply5@nextcloud.com |
| value | invalid |
Then the OCS status code should be "102"
And the HTTP status code should be "200"
Expand All @@ -274,23 +296,23 @@ Feature: provisioning
And As an "brand-new-user"
When sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | no.reply@nextcloud.com |
| value | no.reply6@nextcloud.com |
And the OCS status code should be "100"
And the HTTP status code should be "200"
And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail |
| value | noreply@nextcloud.com |
| value | noreply7@nextcloud.com |
And the OCS status code should be "100"
And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user/additional_mail" with
| key | no.reply@nextcloud.com |
| key | no.reply6@nextcloud.com |
| value | |
And the OCS status code should be "100"
And the HTTP status code should be "200"
Then user "brand-new-user" has
| additional_mail | noreply@nextcloud.com |
| additional_mail | noreply7@nextcloud.com |
Then user "brand-new-user" has not
| additional_mail | no.reply@nextcloud.com |
| additional_mail | no.reply6@nextcloud.com |

Scenario: An admin cannot edit user account property scopes
Given As an "admin"
Expand Down