Skip to content

Commit 804ee11

Browse files
authored
Merge pull request #31029 from nextcloud/expose-extra-emails-in-dav
Expose additional emails in {DAV:}alternate-URI-set
2 parents 5f8e1b7 + 190a71e commit 804ee11

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

apps/dav/appinfo/v1/caldav.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
3535
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
3636
use OCA\DAV\Connector\Sabre\Principal;
37+
use OCP\Accounts\IAccountManager;
3738
use Psr\Log\LoggerInterface;
3839

3940
$authBackend = new Auth(
@@ -47,6 +48,7 @@
4748
$principalBackend = new Principal(
4849
\OC::$server->getUserManager(),
4950
\OC::$server->getGroupManager(),
51+
\OC::$server->get(IAccountManager::class),
5052
\OC::$server->getShareManager(),
5153
\OC::$server->getUserSession(),
5254
\OC::$server->getAppManager(),

apps/dav/appinfo/v1/carddav.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
3737
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
3838
use OCA\DAV\Connector\Sabre\Principal;
39+
use OCP\Accounts\IAccountManager;
3940
use OCP\App\IAppManager;
4041
use Psr\Log\LoggerInterface;
4142
use Sabre\CardDAV\Plugin;
@@ -51,6 +52,7 @@
5152
$principalBackend = new Principal(
5253
\OC::$server->getUserManager(),
5354
\OC::$server->getGroupManager(),
55+
\OC::$server->get(IAccountManager::class),
5456
\OC::$server->getShareManager(),
5557
\OC::$server->getUserSession(),
5658
\OC::$server->getAppManager(),

apps/dav/lib/Command/CreateCalendar.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\DAV\CalDAV\CalDavBackend;
3030
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
3131
use OCA\DAV\Connector\Sabre\Principal;
32+
use OCP\Accounts\IAccountManager;
3233
use OCP\EventDispatcher\IEventDispatcher;
3334
use OCP\IConfig;
3435
use OCP\IDBConnection;
@@ -83,6 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8384
$principalBackend = new Principal(
8485
$this->userManager,
8586
$this->groupManager,
87+
\OC::$server->get(IAccountManager::class),
8688
\OC::$server->getShareManager(),
8789
\OC::$server->getUserSession(),
8890
\OC::$server->getAppManager(),

apps/dav/lib/Connector/Sabre/Principal.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
use OCA\Circles\Exceptions\CircleNotFoundException;
4242
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
4343
use OCA\DAV\Traits\PrincipalProxyTrait;
44+
use OCP\Accounts\IAccountManager;
45+
use OCP\Accounts\IAccountProperty;
46+
use OCP\Accounts\PropertyDoesNotExistException;
4447
use OCP\App\IAppManager;
4548
use OCP\AppFramework\QueryException;
4649
use OCP\Constants;
@@ -64,6 +67,9 @@ class Principal implements BackendInterface {
6467
/** @var IGroupManager */
6568
private $groupManager;
6669

70+
/** @var IAccountManager */
71+
private $accountManager;
72+
6773
/** @var IShareManager */
6874
private $shareManager;
6975

@@ -95,6 +101,7 @@ class Principal implements BackendInterface {
95101

96102
public function __construct(IUserManager $userManager,
97103
IGroupManager $groupManager,
104+
IAccountManager $accountManager,
98105
IShareManager $shareManager,
99106
IUserSession $userSession,
100107
IAppManager $appManager,
@@ -105,6 +112,7 @@ public function __construct(IUserManager $userManager,
105112
string $principalPrefix = 'principals/users/') {
106113
$this->userManager = $userManager;
107114
$this->groupManager = $groupManager;
115+
$this->accountManager = $accountManager;
108116
$this->shareManager = $shareManager;
109117
$this->userSession = $userSession;
110118
$this->appManager = $appManager;
@@ -506,6 +514,7 @@ public function findByUri($uri, $principalPrefix) {
506514
/**
507515
* @param IUser $user
508516
* @return array
517+
* @throws PropertyDoesNotExistException
509518
*/
510519
protected function userToPrincipal($user) {
511520
$userId = $user->getUID();
@@ -517,11 +526,18 @@ protected function userToPrincipal($user) {
517526
'{http://nextcloud.com/ns}language' => $this->languageFactory->getUserLanguage($user),
518527
];
519528

529+
$account = $this->accountManager->getAccount($user);
530+
$alternativeEmails = array_map(fn (IAccountProperty $property) => 'mailto:' . $property->getValue(), $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties());
531+
520532
$email = $user->getSystemEMailAddress();
521533
if (!empty($email)) {
522534
$principal['{http://sabredav.org/ns}email-address'] = $email;
523535
}
524536

537+
if (!empty($alternativeEmails)) {
538+
$principal['{DAV:}alternate-URI-set'] = $alternativeEmails;
539+
}
540+
525541
return $principal;
526542
}
527543

apps/dav/lib/RootCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCA\DAV\DAV\SystemPrincipalBackend;
4545
use OCA\DAV\Provisioning\Apple\AppleProvisioningNode;
4646
use OCA\DAV\Upload\CleanupService;
47+
use OCP\Accounts\IAccountManager;
4748
use OCP\App\IAppManager;
4849
use OCP\AppFramework\Utility\ITimeFactory;
4950
use OCP\EventDispatcher\IEventDispatcher;
@@ -68,6 +69,7 @@ public function __construct() {
6869
$userPrincipalBackend = new Principal(
6970
$userManager,
7071
$groupManager,
72+
\OC::$server->get(IAccountManager::class),
7173
$shareManager,
7274
\OC::$server->getUserSession(),
7375
\OC::$server->getAppManager(),

apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCA\DAV\CalDAV\CalDavBackend;
3232
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
3333
use OCA\DAV\Connector\Sabre\Principal;
34+
use OCP\Accounts\IAccountManager;
3435
use OCP\App\IAppManager;
3536
use OCP\EventDispatcher\IEventDispatcher;
3637
use OCP\IConfig;
@@ -86,6 +87,7 @@ protected function setUp(): void {
8687
->setConstructorArgs([
8788
$this->userManager,
8889
$this->groupManager,
90+
$this->createMock(IAccountManager::class),
8991
$this->createMock(ShareManager::class),
9092
$this->createMock(IUserSession::class),
9193
$this->createMock(IAppManager::class),

apps/dav/tests/unit/CardDAV/CardDavBackendTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use OCA\DAV\CardDAV\AddressBook;
3939
use OCA\DAV\CardDAV\CardDavBackend;
4040
use OCA\DAV\Connector\Sabre\Principal;
41+
use OCP\Accounts\IAccountManager;
4142
use OCP\App\IAppManager;
4243
use OCP\DB\QueryBuilder\IQueryBuilder;
4344
use OCP\EventDispatcher\IEventDispatcher;
@@ -132,6 +133,7 @@ protected function setUp(): void {
132133
->setConstructorArgs([
133134
$this->userManager,
134135
$this->groupManager,
136+
$this->createMock(IAccountManager::class),
135137
$this->createMock(ShareManager::class),
136138
$this->createMock(IUserSession::class),
137139
$this->createMock(IAppManager::class),

apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
use OCA\DAV\CalDAV\Proxy\Proxy;
3535
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
3636
use OCA\DAV\Connector\Sabre\Principal;
37+
use OCP\Accounts\IAccount;
38+
use OCP\Accounts\IAccountManager;
39+
use OCP\Accounts\IAccountProperty;
40+
use OCP\Accounts\IAccountPropertyCollection;
3741
use OCP\App\IAppManager;
3842
use OCP\IConfig;
3943
use OCP\IGroup;
@@ -59,6 +63,9 @@ class PrincipalTest extends TestCase {
5963
/** @var IGroupManager | MockObject */
6064
private $groupManager;
6165

66+
/** @var IAccountManager|MockObject */
67+
private $accountManager;
68+
6269
/** @var IManager | MockObject */
6370
private $shareManager;
6471

@@ -81,6 +88,7 @@ class PrincipalTest extends TestCase {
8188
protected function setUp(): void {
8289
$this->userManager = $this->createMock(IUserManager::class);
8390
$this->groupManager = $this->createMock(IGroupManager::class);
91+
$this->accountManager = $this->createMock(IAccountManager::class);
8492
$this->shareManager = $this->createMock(IManager::class);
8593
$this->userSession = $this->createMock(IUserSession::class);
8694
$this->appManager = $this->createMock(IAppManager::class);
@@ -92,6 +100,7 @@ protected function setUp(): void {
92100
$this->connector = new Principal(
93101
$this->userManager,
94102
$this->groupManager,
103+
$this->accountManager,
95104
$this->shareManager,
96105
$this->userSession,
97106
$this->appManager,
@@ -143,6 +152,45 @@ public function testGetPrincipalsByPrefixWithUsers(): void {
143152
->withConsecutive([$fooUser], [$barUser])
144153
->willReturnOnConsecutiveCalls('de', 'en');
145154

155+
$fooAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
156+
$fooAccountPropertyCollection->expects($this->once())
157+
->method('getProperties')
158+
->with()
159+
->willReturn([]);
160+
$fooAccount = $this->createMock(IAccount::class);
161+
$fooAccount->expects($this->once())
162+
->method('getPropertyCollection')
163+
->with(IAccountManager::COLLECTION_EMAIL)
164+
->willReturn($fooAccountPropertyCollection);
165+
166+
$emailPropertyOne = $this->createMock(IAccountProperty::class);
167+
$emailPropertyOne->expects($this->once())
168+
->method('getValue')
169+
->with()
170+
->willReturn('[email protected]');
171+
$emailPropertyTwo = $this->createMock(IAccountProperty::class);
172+
$emailPropertyTwo->expects($this->once())
173+
->method('getValue')
174+
->with()
175+
->willReturn('[email protected]');
176+
177+
$barAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
178+
$barAccountPropertyCollection->expects($this->once())
179+
->method('getProperties')
180+
->with()
181+
->willReturn([$emailPropertyOne, $emailPropertyTwo]);
182+
$barAccount = $this->createMock(IAccount::class);
183+
$barAccount->expects($this->once())
184+
->method('getPropertyCollection')
185+
->with(IAccountManager::COLLECTION_EMAIL)
186+
->willReturn($barAccountPropertyCollection);
187+
188+
$this->accountManager
189+
->expects($this->exactly(2))
190+
->method('getAccount')
191+
->withConsecutive([$fooUser], [$barUser])
192+
->willReturnOnConsecutiveCalls($fooAccount, $barAccount);
193+
146194
$expectedResponse = [
147195
0 => [
148196
'uri' => 'principals/users/foo',
@@ -156,6 +204,7 @@ public function testGetPrincipalsByPrefixWithUsers(): void {
156204
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
157205
'{http://nextcloud.com/ns}language' => 'en',
158206
'{http://sabredav.org/ns}email-address' => '[email protected]',
207+
'{DAV:}alternate-URI-set' => ['mailto:[email protected]', 'mailto:[email protected]']
159208
]
160209
];
161210
$response = $this->connector->getPrincipalsByPrefix('principals/users');

apps/files_versions/lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use OCA\Files_Versions\Listener\LoadSidebarListener;
3939
use OCA\Files_Versions\Versions\IVersionManager;
4040
use OCA\Files_Versions\Versions\VersionManager;
41+
use OCP\Accounts\IAccountManager;
4142
use OCP\App\IAppManager;
4243
use OCP\AppFramework\App;
4344
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -75,6 +76,7 @@ public function register(IRegistrationContext $context): void {
7576
return new Principal(
7677
$server->get(IUserManager::class),
7778
$server->get(IGroupManager::class),
79+
\OC::$server->get(IAccountManager::class),
7880
$server->get(IShareManager::class),
7981
$server->get(IUserSession::class),
8082
$server->get(IAppManager::class),

0 commit comments

Comments
 (0)