Skip to content

Commit bb80295

Browse files
committed
enh(contacts): show/hide addressbooks for all
Signed-off-by: Johannes Merkel <mail@johannesgge.de>
1 parent a84fa17 commit bb80295

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

apps/dav/lib/CardDAV/AddressBook.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
* @property CardDavBackend $carddavBackend
4747
*/
4848
class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable, IMoveTarget {
49-
5049
/**
5150
* AddressBook constructor.
5251
*
@@ -116,7 +115,12 @@ public function getACL() {
116115
'privilege' => '{DAV:}write',
117116
'principal' => $this->getOwner(),
118117
'protected' => true,
119-
]
118+
],
119+
[
120+
'privilege' => '{DAV:}write-properties',
121+
'principal' => '{DAV:}authenticated',
122+
'protected' => true,
123+
],
120124
];
121125

122126
if ($this->getOwner() === 'principals/system/system') {
@@ -147,7 +151,7 @@ public function getACL() {
147151
}
148152

149153
$acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl);
150-
$allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system'];
154+
$allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system', '{DAV:}authenticated'];
151155
return array_filter($acl, function ($rule) use ($allowedPrincipals) {
152156
return \in_array($rule['principal'], $allowedPrincipals, true);
153157
});
@@ -166,8 +170,7 @@ public function getChild($name) {
166170
return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
167171
}
168172

169-
public function getChildren()
170-
{
173+
public function getChildren() {
171174
$objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
172175
$children = [];
173176
foreach ($objs as $obj) {
@@ -178,8 +181,7 @@ public function getChildren()
178181
return $children;
179182
}
180183

181-
public function getMultipleChildren(array $paths)
182-
{
184+
public function getMultipleChildren(array $paths) {
183185
$objs = $this->carddavBackend->getMultipleCards($this->addressBookInfo['id'], $paths);
184186
$children = [];
185187
foreach ($objs as $obj) {
@@ -221,10 +223,12 @@ public function delete() {
221223
}
222224

223225
public function propPatch(PropPatch $propPatch) {
224-
if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
225-
throw new Forbidden();
226+
// shared address books will be handled by
227+
// \OCA\DAV\DAV\CustomPropertiesBackend::propPatch
228+
// to save values in db table instead of dav object
229+
if (!$this->isShared()) {
230+
parent::propPatch($propPatch);
226231
}
227-
parent::propPatch($propPatch);
228232
}
229233

230234
public function getContactsGroups() {

apps/dav/lib/DAV/CustomPropertiesBackend.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ public function propFind($path, PropFind $propFind) {
176176
}
177177
}
178178

179+
// substr of addressbooks/ => path is inside the CardDAV component
180+
// three '/' => this a addressbook (no addressbook-home nor contact object)
181+
if (str_starts_with($path, 'addressbooks/') && substr_count($path, '/') === 3) {
182+
$allRequestedProps = $propFind->getRequestedProperties();
183+
$customPropertiesForShares = [
184+
'{DAV:}displayname',
185+
];
186+
187+
foreach ($customPropertiesForShares as $customPropertyForShares) {
188+
if (in_array($customPropertyForShares, $allRequestedProps, true)) {
189+
$requestedProps[] = $customPropertyForShares;
190+
}
191+
}
192+
}
193+
179194
if (empty($requestedProps)) {
180195
return;
181196
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\DAV\CardDAV\AddressBook;
3030
use OCA\DAV\CardDAV\Card;
3131
use OCA\DAV\CardDAV\CardDavBackend;
32+
use OCA\DAV\DAV\CustomPropertiesBackend;
3233
use OCP\IL10N;
3334
use PHPUnit\Framework\MockObject\MockObject;
3435
use Psr\Log\LoggerInterface;
@@ -101,11 +102,10 @@ public function testDeleteFromGroup(): void {
101102
}
102103

103104

104-
public function testPropPatch(): void {
105-
$this->expectException(Forbidden::class);
106-
105+
public function testPropPatchShared(): void {
107106
/** @var MockObject | CardDavBackend $backend */
108107
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
108+
$backend->expects($this->never())->method('updateAddressBook');
109109
$addressBookInfo = [
110110
'{http://owncloud.org/ns}owner-principal' => 'user1',
111111
'{DAV:}displayname' => 'Test address book',
@@ -116,7 +116,24 @@ public function testPropPatch(): void {
116116
$l10n = $this->createMock(IL10N::class);
117117
$logger = $this->createMock(LoggerInterface::class);
118118
$addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
119-
$addressBook->propPatch(new PropPatch([]));
119+
$addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
120+
}
121+
122+
public function testPropPatchNotShared(): void {
123+
/** @var MockObject | CardDavBackend $backend */
124+
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
125+
$backend->expects($this->atLeast(1))->method('updateAddressBook');
126+
$addressBookInfo = [
127+
'{http://owncloud.org/ns}owner-principal' => 'user1',
128+
'{DAV:}displayname' => 'Test address book',
129+
'principaluri' => 'user1',
130+
'id' => 666,
131+
'uri' => 'default',
132+
];
133+
$l10n = $this->createMock(IL10N::class);
134+
$logger = $this->createMock(LoggerInterface::class);
135+
$addressBook = new AddressBook($backend, $addressBookInfo, $l10n, $logger);
136+
$addressBook->propPatch(new PropPatch(['{DAV:}displayname' => 'Test address book']));
120137
}
121138

122139
/**
@@ -152,6 +169,10 @@ public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet): void {
152169
'privilege' => '{DAV:}write',
153170
'principal' => $hasOwnerSet ? 'user1' : 'user2',
154171
'protected' => true
172+
], [
173+
'privilege' => '{DAV:}write-properties',
174+
'principal' => '{DAV:}authenticated',
175+
'protected' => true
155176
]];
156177
if ($hasOwnerSet) {
157178
$expectedAcl[] = [

0 commit comments

Comments
 (0)