Skip to content

Commit

Permalink
Expose additional emails in {DAV:}alternate-URI-set
Browse files Browse the repository at this point in the history
This allows iMip invitations to be send with an alternative email as
"Reply-To" field.

Closes #27201

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Feb 5, 2022
1 parent 9c2db7f commit e672feb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/dav/appinfo/v1/caldav.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;

$authBackend = new Auth(
\OC::$server->getSession(),
Expand All @@ -46,6 +47,7 @@
$principalBackend = new Principal(
\OC::$server->getUserManager(),
\OC::$server->getGroupManager(),
\OC::$server->get(IAccountManager::class),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getAppManager(),
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/appinfo/v1/carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
use OCA\DAV\Connector\Sabre\MaintenancePlugin;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use Sabre\CardDAV\Plugin;

Expand All @@ -49,6 +50,7 @@
$principalBackend = new Principal(
\OC::$server->getUserManager(),
\OC::$server->getGroupManager(),
\OC::$server->get(IAccountManager::class),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getAppManager(),
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/lib/Command/CreateCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IDBConnection;
Expand Down Expand Up @@ -82,6 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$principalBackend = new Principal(
$this->userManager,
$this->groupManager,
\OC::$server->get(IAccountManager::class),
\OC::$server->getShareManager(),
\OC::$server->getUserSession(),
\OC::$server->getAppManager(),
Expand Down
17 changes: 17 additions & 0 deletions apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Traits\PrincipalProxyTrait;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\Constants;
Expand All @@ -64,6 +67,9 @@ class Principal implements BackendInterface {
/** @var IGroupManager */
private $groupManager;

/** @var IAccountManager */
private $accountManager;

/** @var IShareManager */
private $shareManager;

Expand Down Expand Up @@ -95,6 +101,7 @@ class Principal implements BackendInterface {

public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IAccountManager $accountManager,
IShareManager $shareManager,
IUserSession $userSession,
IAppManager $appManager,
Expand All @@ -105,6 +112,7 @@ public function __construct(IUserManager $userManager,
string $principalPrefix = 'principals/users/') {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->accountManager = $accountManager;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->appManager = $appManager;
Expand Down Expand Up @@ -502,6 +510,7 @@ public function findByUri($uri, $principalPrefix) {
/**
* @param IUser $user
* @return array
* @throws PropertyDoesNotExistException
*/
protected function userToPrincipal($user) {
$userId = $user->getUID();
Expand All @@ -513,9 +522,17 @@ protected function userToPrincipal($user) {
'{http://nextcloud.com/ns}language' => $this->languageFactory->getUserLanguage($user),
];

$account = $this->accountManager->getAccount($user);
$alternativeEmails = array_map(fn (IAccountProperty $property) => 'mailto:' . $property->getValue(), $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties());

$email = $user->getSystemEMailAddress();
if (!empty($email)) {
$principal['{http://sabredav.org/ns}email-address'] = $email;
$alternativeEmails = array_values(array_filter($alternativeEmails, fn ($alternativeEmail) => $alternativeEmail !== 'mailto:' . $email));
}

if (!empty($alternativeEmails)) {
$principal['{DAV:}alternate-URI-set'] = $alternativeEmails;
}

return $principal;
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\DAV\DAV\SystemPrincipalBackend;
use OCA\DAV\Provisioning\Apple\AppleProvisioningNode;
use OCA\DAV\Upload\CleanupService;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -67,6 +68,7 @@ public function __construct() {
$userPrincipalBackend = new Principal(
$userManager,
$groupManager,
\OC::$server->get(IAccountManager::class),
$shareManager,
\OC::$server->getUserSession(),
\OC::$server->getAppManager(),
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
Expand Down Expand Up @@ -89,6 +90,7 @@ protected function setUp(): void {
->setConstructorArgs([
$this->userManager,
$this->groupManager,
$this->createMock(IAccountManager::class),
$this->createMock(ShareManager::class),
$this->createMock(IUserSession::class),
$this->createMock(IAppManager::class),
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/tests/unit/CardDAV/CardDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCA\DAV\CardDAV\AddressBook;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -136,6 +137,7 @@ protected function setUp(): void {
->setConstructorArgs([
$this->userManager,
$this->groupManager,
$this->createMock(IAccountManager::class),
$this->createMock(ShareManager::class),
$this->createMock(IUserSession::class),
$this->createMock(IAppManager::class),
Expand Down
49 changes: 49 additions & 0 deletions apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
use OCA\DAV\CalDAV\Proxy\Proxy;
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IGroup;
Expand All @@ -59,6 +63,9 @@ class PrincipalTest extends TestCase {
/** @var IGroupManager | MockObject */
private $groupManager;

/** @var IAccountManager|MockObject */
private $accountManager;

/** @var IManager | MockObject */
private $shareManager;

Expand All @@ -81,6 +88,7 @@ class PrincipalTest extends TestCase {
protected function setUp(): void {
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->accountManager = $this->createMock(IAccountManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->appManager = $this->createMock(IAppManager::class);
Expand All @@ -92,6 +100,7 @@ protected function setUp(): void {
$this->connector = new Principal(
$this->userManager,
$this->groupManager,
$this->accountManager,
$this->shareManager,
$this->userSession,
$this->appManager,
Expand Down Expand Up @@ -143,6 +152,45 @@ public function testGetPrincipalsByPrefixWithUsers(): void {
->withConsecutive([$fooUser], [$barUser])
->willReturnOnConsecutiveCalls('de', 'en');

$fooAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
$fooAccountPropertyCollection->expects($this->once())
->method('getProperties')
->with()
->willReturn([]);
$fooAccount = $this->createMock(IAccount::class);
$fooAccount->expects($this->once())
->method('getPropertyCollection')
->with(IAccountManager::COLLECTION_EMAIL)
->willReturn($fooAccountPropertyCollection);

$emailPropertyOne = $this->createMock(IAccountProperty::class);
$emailPropertyOne->expects($this->once())
->method('getValue')
->with()
->willReturn('bar@nextcloud.com');
$emailPropertyTwo = $this->createMock(IAccountProperty::class);
$emailPropertyTwo->expects($this->once())
->method('getValue')
->with()
->willReturn('alias@nextcloud.com');

$barAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
$barAccountPropertyCollection->expects($this->once())
->method('getProperties')
->with()
->willReturn([$emailPropertyOne, $emailPropertyTwo]);
$barAccount = $this->createMock(IAccount::class);
$barAccount->expects($this->once())
->method('getPropertyCollection')
->with(IAccountManager::COLLECTION_EMAIL)
->willReturn($barAccountPropertyCollection);

$this->accountManager
->expects($this->exactly(2))
->method('getAccount')
->withConsecutive([$fooUser], [$barUser])
->willReturnOnConsecutiveCalls($fooAccount, $barAccount);

$expectedResponse = [
0 => [
'uri' => 'principals/users/foo',
Expand All @@ -156,6 +204,7 @@ public function testGetPrincipalsByPrefixWithUsers(): void {
'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
'{http://nextcloud.com/ns}language' => 'en',
'{http://sabredav.org/ns}email-address' => 'bar@nextcloud.com',
'{DAV:}alternate-URI-set' => ['mailto:alias@nextcloud.com']
]
];
$response = $this->connector->getPrincipalsByPrefix('principals/users');
Expand Down
2 changes: 2 additions & 0 deletions apps/files_versions/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCA\Files_Versions\Listener\LoadSidebarListener;
use OCA\Files_Versions\Versions\IVersionManager;
use OCA\Files_Versions\Versions\VersionManager;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down Expand Up @@ -75,6 +76,7 @@ public function register(IRegistrationContext $context): void {
return new Principal(
$server->get(IUserManager::class),
$server->get(IGroupManager::class),
\OC::$server->get(IAccountManager::class),
$server->get(IShareManager::class),
$server->get(IUserSession::class),
$server->get(IAppManager::class),
Expand Down

0 comments on commit e672feb

Please sign in to comment.