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
20 changes: 16 additions & 4 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,21 @@ function getCalendarsForUser($principalUri) {
->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
->execute();

$readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
while($row = $result->fetch()) {
$readOnly = (int) $row['access'] === Backend::ACCESS_READ;
if (isset($calendars[$row['id']])) {
if ($readOnly) {
// New share can not have more permissions then the old one.
continue;
}
if (isset($calendars[$row['id']][$readOnlyPropertyName]) &&
$calendars[$row['id']][$readOnlyPropertyName] === 0) {
// Old share is already read-write, no more permissions can be gained
continue;
}
}

list(, $name) = URLUtil::splitPath($row['principaluri']);
$uri = $row['uri'] . '_shared_by_' . $name;
$row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
Expand All @@ -294,16 +308,14 @@ function getCalendarsForUser($principalUri) {
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
'{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint),
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
$readOnlyPropertyName => $readOnly,
];

foreach($this->propertyMap as $xmlName=>$dbName) {
$calendar[$xmlName] = $row[$dbName];
}

if (!isset($calendars[$calendar['id']])) {
$calendars[$calendar['id']] = $calendar;
}
$calendars[$calendar['id']] = $calendar;
}
$result->closeCursor();

Expand Down
39 changes: 26 additions & 13 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,36 @@ function getAddressBooksForUser($principalUri) {
->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY)
->execute();

$readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
while($row = $result->fetch()) {
$readOnly = (int) $row['access'] === Backend::ACCESS_READ;
if (isset($addressBooks[$row['id']])) {
if ($readOnly) {
// New share can not have more permissions then the old one.
continue;
}
if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) &&
$addressBooks[$row['id']][$readOnlyPropertyName] === 0) {
// Old share is already read-write, no more permissions can be gained
continue;
}
}

list(, $name) = URLUtil::splitPath($row['principaluri']);
$uri = $row['uri'] . '_shared_by_' . $name;
$displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')';
if (!isset($addressBooks[$row['id']])) {
$addressBooks[$row['id']] = [
'id' => $row['id'],
'uri' => $uri,
'principaluri' => $principalUriOriginal,
'{DAV:}displayname' => $displayName,
'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
'{http://calendarserver.org/ns/}getctag' => $row['synctoken'],
'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'],
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ,
];
}

$addressBooks[$row['id']] = [
'id' => $row['id'],
'uri' => $uri,
'principaluri' => $principalUriOriginal,
'{DAV:}displayname' => $displayName,
'{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
'{http://calendarserver.org/ns/}getctag' => $row['synctoken'],
'{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'],
$readOnlyPropertyName => $readOnly,
];
}
$result->closeCursor();

Expand Down
3 changes: 2 additions & 1 deletion apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract class AbstractCalDavBackendTest extends TestCase {
const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1';
const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group';
const UNIT_TEST_GROUP2 = 'principals/groups/caldav-unit-test-group2';

public function setUp() {
parent::setUp();
Expand All @@ -71,7 +72,7 @@ public function setUp() {
]);
$this->principal->expects($this->any())->method('getGroupMembership')
->withAnyParameters()
->willReturn([self::UNIT_TEST_GROUP]);
->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);

$db = \OC::$server->getDatabaseConnection();
$this->random = \OC::$server->getSecureRandom();
Expand Down
93 changes: 56 additions & 37 deletions apps/dav/tests/unit/CalDAV/CalDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ public function testCalendarOperations() {
$this->backend->updateCalendar($calendarId, $patch);
$patch->commit();
$this->assertEquals(1, $this->backend->getCalendarsForUserCount(self::UNIT_TEST_USER));
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertEquals(1, count($books));
$this->assertEquals('Unit test', $books[0]['{DAV:}displayname']);
$this->assertEquals('Calendar used for unit testing', $books[0]['{urn:ietf:params:xml:ns:caldav}calendar-description']);
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertCount(1, $calendars);
$this->assertEquals('Unit test', $calendars[0]['{DAV:}displayname']);
$this->assertEquals('Calendar used for unit testing', $calendars[0]['{urn:ietf:params:xml:ns:caldav}calendar-description']);

// delete the address book
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar');
$this->backend->deleteCalendar($books[0]['id']);
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertEquals(0, count($books));
$this->backend->deleteCalendar($calendars[0]['id']);
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertCount(0, $calendars);
}

public function providesSharingData() {
Expand All @@ -83,6 +83,26 @@ public function providesSharingData() {
'readOnly' => true
]
]],
[true, true, true, false, [
[
'href' => 'principal:' . self::UNIT_TEST_GROUP,
'readOnly' => true,
],
[
'href' => 'principal:' . self::UNIT_TEST_GROUP2,
'readOnly' => false,
],
]],
[true, true, true, true, [
[
'href' => 'principal:' . self::UNIT_TEST_GROUP,
'readOnly' => false,
],
[
'href' => 'principal:' . self::UNIT_TEST_GROUP2,
'readOnly' => true,
],
]],
[true, false, false, false, [
[
'href' => 'principal:' . self::UNIT_TEST_USER1,
Expand All @@ -98,9 +118,8 @@ public function providesSharingData() {
*/
public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead, $groupCanWrite, $add) {

/** @var IL10N | \PHPUnit_Framework_MockObject_MockObject $l10n */
$l10n = $this->getMockBuilder('\OCP\IL10N')
->disableOriginalConstructor()->getMock();
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
$l10n = $this->createMock(IL10N::class);
$l10n
->expects($this->any())
->method('t')
Expand All @@ -109,16 +128,16 @@ public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead,
}));

$calendarId = $this->createTestCalendar();
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertEquals(1, count($books));
$calendar = new Calendar($this->backend, $books[0], $l10n);
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertCount(1, $calendars);
$calendar = new Calendar($this->backend, $calendars[0], $l10n);
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::updateShares');
$this->backend->updateShares($calendar, $add, []);
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1);
$this->assertEquals(1, count($books));
$calendar = new Calendar($this->backend, $books[0], $l10n);
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1);
$this->assertCount(1, $calendars);
$calendar = new Calendar($this->backend, $calendars[0], $l10n);
$acl = $calendar->getACL();
$this->assertAcl(self::UNIT_TEST_USER, '{DAV:}read', $acl);
$this->assertAcl(self::UNIT_TEST_USER, '{DAV:}write', $acl);
Expand All @@ -129,7 +148,7 @@ public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead,
$this->assertEquals(self::UNIT_TEST_USER, $calendar->getOwner());

// test acls on the child
$uri = $this->getUniqueID('calobj');
$uri = static::getUniqueID('calobj');
$calData = <<<'EOD'
BEGIN:VCALENDAR
VERSION:2.0
Expand Down Expand Up @@ -166,17 +185,17 @@ public function testCalendarSharing($userCanRead, $userCanWrite, $groupCanRead,
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar');
$this->backend->deleteCalendar($books[0]['id']);
$books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertEquals(0, count($books));
$this->backend->deleteCalendar($calendars[0]['id']);
$calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
$this->assertCount(0, $calendars);
}

public function testCalendarObjectsOperations() {

$calendarId = $this->createTestCalendar();

// create a card
$uri = $this->getUniqueID('calobj');
$uri = static::getUniqueID('calobj');
$calData = <<<'EOD'
BEGIN:VCALENDAR
VERSION:2.0
Expand All @@ -201,7 +220,7 @@ public function testCalendarObjectsOperations() {

// get all the cards
$calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertEquals(1, count($calendarObjects));
$this->assertCount(1, $calendarObjects);
$this->assertEquals($calendarId, $calendarObjects[0]['calendarid']);
$this->assertArrayHasKey('classification', $calendarObjects[0]);

Expand Down Expand Up @@ -245,7 +264,7 @@ public function testCalendarObjectsOperations() {
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
$this->backend->deleteCalendarObject($calendarId, $uri);
$calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertEquals(0, count($calendarObjects));
$this->assertCount(0, $calendarObjects);
}

public function testMultiCalendarObjects() {
Expand All @@ -269,29 +288,29 @@ public function testMultiCalendarObjects() {
END:VEVENT
END:VCALENDAR
EOD;
$uri0 = $this->getUniqueID('card');
$uri0 = static::getUniqueID('card');
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
$this->backend->createCalendarObject($calendarId, $uri0, $calData);
$uri1 = $this->getUniqueID('card');
$uri1 = static::getUniqueID('card');
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
$this->backend->createCalendarObject($calendarId, $uri1, $calData);
$uri2 = $this->getUniqueID('card');
$uri2 = static::getUniqueID('card');
$this->dispatcher->expects($this->at(0))
->method('dispatch')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject');
$this->backend->createCalendarObject($calendarId, $uri2, $calData);

// get all the cards
$calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertEquals(3, count($calendarObjects));
$this->assertCount(3, $calendarObjects);

// get the cards
$calendarObjects = $this->backend->getMultipleCalendarObjects($calendarId, [$uri1, $uri2]);
$this->assertEquals(2, count($calendarObjects));
$this->assertCount(2, $calendarObjects);
foreach($calendarObjects as $card) {
$this->assertArrayHasKey('id', $card);
$this->assertArrayHasKey('uri', $card);
Expand All @@ -316,7 +335,7 @@ public function testMultiCalendarObjects() {
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject');
$this->backend->deleteCalendarObject($calendarId, $uri2);
$calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertEquals(0, count($calendarObjects));
$this->assertCount(0, $calendarObjects);
}

/**
Expand Down Expand Up @@ -385,15 +404,15 @@ public function testPublications() {

$calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];

$l10n = $this->getMockBuilder('\OCP\IL10N')
->disableOriginalConstructor()->getMock();
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
$l10n = $this->createMock(IL10N::class);

$calendar = new Calendar($this->backend, $calendarInfo, $l10n);
$calendar->setPublishStatus(true);
$this->assertNotEquals(false, $calendar->getPublishStatus());

$publicCalendars = $this->backend->getPublicCalendars();
$this->assertEquals(1, count($publicCalendars));
$this->assertCount(1, $publicCalendars);
$this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);

$publicCalendarURI = $publicCalendars[0]['uri'];
Expand All @@ -415,7 +434,7 @@ public function testSubscriptions() {
]);

$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
$this->assertEquals(1, count($subscriptions));
$this->assertCount(1, $subscriptions);
$this->assertEquals('#1C4587', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);
$this->assertEquals(true, $subscriptions[0]['{http://calendarserver.org/ns/}subscribed-strip-todos']);
$this->assertEquals($id, $subscriptions[0]['id']);
Expand All @@ -428,29 +447,29 @@ public function testSubscriptions() {
$patch->commit();

$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
$this->assertEquals(1, count($subscriptions));
$this->assertCount(1, $subscriptions);
$this->assertEquals($id, $subscriptions[0]['id']);
$this->assertEquals('Unit test', $subscriptions[0]['{DAV:}displayname']);
$this->assertEquals('#ac0606', $subscriptions[0]['{http://apple.com/ns/ical/}calendar-color']);

$this->backend->deleteSubscription($id);
$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
$this->assertEquals(0, count($subscriptions));
$this->assertCount(0, $subscriptions);
}

public function testScheduling() {
$this->backend->createSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule', '');

$sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
$this->assertEquals(1, count($sos));
$this->assertCount(1, $sos);

$so = $this->backend->getSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');
$this->assertNotNull($so);

$this->backend->deleteSchedulingObject(self::UNIT_TEST_USER, 'Sample Schedule');

$sos = $this->backend->getSchedulingObjects(self::UNIT_TEST_USER);
$this->assertEquals(0, count($sos));
$this->assertCount(0, $sos);
}

/**
Expand Down