diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-04-08 15:19:38 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-04-08 15:19:38 +0200 |
commit | 6f3eeeeb36d86b352d396f730fbc6a8d1e8ca160 (patch) | |
tree | 62f29d34ebbd891a56ffc9c0a64eba3d5f36394b /apps/dav/lib | |
parent | 88a366b3143b70953224f96c500cf725270065b4 (diff) | |
parent | c8d6a9594ab7c0525cc9485b394547ae960d8f34 (diff) | |
download | nextcloud-server-6f3eeeeb36d86b352d396f730fbc6a8d1e8ca160.tar.gz nextcloud-server-6f3eeeeb36d86b352d396f730fbc6a8d1e8ca160.zip |
Merge pull request #23510 from owncloud/birthdays-on-shared-addressbooks
Propagate birthdays of shared addressbooks to the sharee's birthday c…
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/caldav/birthdayservice.php | 71 | ||||
-rw-r--r-- | apps/dav/lib/dav/groupprincipalbackend.php | 43 | ||||
-rw-r--r-- | apps/dav/lib/dav/sharing/backend.php | 3 |
3 files changed, 90 insertions, 27 deletions
diff --git a/apps/dav/lib/caldav/birthdayservice.php b/apps/dav/lib/caldav/birthdayservice.php index e03a630fed8..1d0b06f03a8 100644 --- a/apps/dav/lib/caldav/birthdayservice.php +++ b/apps/dav/lib/caldav/birthdayservice.php @@ -23,6 +23,7 @@ namespace OCA\DAV\CalDAV; use Exception; use OCA\DAV\CardDAV\CardDavBackend; +use OCA\DAV\DAV\GroupPrincipalBackend; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Reader; @@ -30,15 +31,20 @@ class BirthdayService { const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; + /** @var GroupPrincipalBackend */ + private $principalBackend; + /** * BirthdayService constructor. * * @param CalDavBackend $calDavBackEnd * @param CardDavBackend $cardDavBackEnd + * @param GroupPrincipalBackend $principalBackend */ - public function __construct($calDavBackEnd, $cardDavBackEnd) { + public function __construct($calDavBackEnd, $cardDavBackEnd, $principalBackend) { $this->calDavBackEnd = $calDavBackEnd; $this->cardDavBackEnd = $cardDavBackEnd; + $this->principalBackend = $principalBackend; } /** @@ -48,22 +54,26 @@ class BirthdayService { */ public function onCardChanged($addressBookId, $cardUri, $cardData) { + $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); + $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); - $principalUri = $book['principaluri']; - $calendar = $this->ensureCalendarExists($principalUri); - $objectUri = $book['uri'] . '-' . $cardUri. '.ics'; - $calendarData = $this->buildBirthdayFromContact($cardData); - $existing = $this->calDavBackEnd->getCalendarObject($calendar['id'], $objectUri); - if (is_null($calendarData)) { - if (!is_null($existing)) { - $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); - } - } else { - if (is_null($existing)) { - $this->calDavBackEnd->createCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); + $targetPrincipals[] = $book['principaluri']; + foreach ($targetPrincipals as $principalUri) { + $calendar = $this->ensureCalendarExists($principalUri); + $objectUri = $book['uri'] . '-' . $cardUri. '.ics'; + $calendarData = $this->buildBirthdayFromContact($cardData); + $existing = $this->calDavBackEnd->getCalendarObject($calendar['id'], $objectUri); + if (is_null($calendarData)) { + if (!is_null($existing)) { + $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); + } } else { - if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { - $this->calDavBackEnd->updateCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); + if (is_null($existing)) { + $this->calDavBackEnd->createCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); + } else { + if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { + $this->calDavBackEnd->updateCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); + } } } } @@ -74,11 +84,14 @@ class BirthdayService { * @param string $cardUri */ public function onCardDeleted($addressBookId, $cardUri) { + $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); - $principalUri = $book['principaluri']; - $calendar = $this->ensureCalendarExists($principalUri); - $objectUri = $book['uri'] . '-' . $cardUri. '.ics'; - $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); + $targetPrincipals[] = $book['principaluri']; + foreach ($targetPrincipals as $principalUri) { + $calendar = $this->ensureCalendarExists($principalUri); + $objectUri = $book['uri'] . '-' . $cardUri . '.ics'; + $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); + } } /** @@ -190,4 +203,24 @@ class BirthdayService { return false; } + /** + * @param $addressBookId + * @return mixed + */ + protected function getAllAffectedPrincipals($addressBookId) { + $targetPrincipals = []; + $shares = $this->cardDavBackEnd->getShares($addressBookId); + foreach ($shares as $share) { + if ($share['{http://owncloud.org/ns}group-share']) { + $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); + foreach ($users as $user) { + $targetPrincipals[] = $user['uri']; + } + } else { + $targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; + } + } + return array_values(array_unique($targetPrincipals, SORT_STRING)); + } + } diff --git a/apps/dav/lib/dav/groupprincipalbackend.php b/apps/dav/lib/dav/groupprincipalbackend.php index 34c00b7927e..e0568639784 100644 --- a/apps/dav/lib/dav/groupprincipalbackend.php +++ b/apps/dav/lib/dav/groupprincipalbackend.php @@ -22,6 +22,7 @@ namespace OCA\DAV\DAV; use OCP\IGroup; use OCP\IGroupManager; +use OCP\IUser; use Sabre\DAV\Exception; use \Sabre\DAV\PropPatch; use Sabre\DAVACL\PrincipalBackend\BackendInterface; @@ -82,10 +83,10 @@ class GroupPrincipalBackend implements BackendInterface { return null; } $name = $elements[2]; - $user = $this->groupManager->get($name); + $group = $this->groupManager->get($name); - if (!is_null($user)) { - return $this->groupToPrincipal($user); + if (!is_null($group)) { + return $this->groupToPrincipal($group); } return null; @@ -99,8 +100,23 @@ class GroupPrincipalBackend implements BackendInterface { * @throws Exception */ public function getGroupMemberSet($principal) { - // TODO: implement if we want that - return []; + $elements = explode('/', $principal); + if ($elements[0] !== 'principals') { + return []; + } + if ($elements[1] !== 'groups') { + return []; + } + $name = $elements[2]; + $group = $this->groupManager->get($name); + + if (is_null($group)) { + return []; + } + + return array_map(function($user) { + return $this->userToPrincipal($user); + }, $group->getUsers()); } /** @@ -162,8 +178,21 @@ class GroupPrincipalBackend implements BackendInterface { protected function groupToPrincipal($group) { $groupId = $group->getGID(); $principal = [ - 'uri' => "principals/groups/$groupId", - '{DAV:}displayname' => $groupId, + 'uri' => "principals/groups/$groupId", + '{DAV:}displayname' => $groupId, + ]; + + return $principal; + } + + /** + * @param IUser $user + * @return array + */ + protected function userToPrincipal($user) { + $principal = [ + 'uri' => 'principals/users/' . $user->getUID(), + '{DAV:}displayname' => $user->getDisplayName(), ]; return $principal; diff --git a/apps/dav/lib/dav/sharing/backend.php b/apps/dav/lib/dav/sharing/backend.php index ffc4193e34b..225b773713d 100644 --- a/apps/dav/lib/dav/sharing/backend.php +++ b/apps/dav/lib/dav/sharing/backend.php @@ -161,7 +161,8 @@ class Backend { 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', 'status' => 1, 'readOnly' => ($row['access'] == self::ACCESS_READ), - '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal' => $row['principaluri'] + '{http://owncloud.org/ns}principal' => $row['principaluri'], + '{http://owncloud.org/ns}group-share' => is_null($p) ]; } |