diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-12 13:23:50 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-18 22:14:51 +0100 |
commit | 45baf03a2acbecc46310611f9bd6670b1e93eb00 (patch) | |
tree | 14bcfb183aa872daddb948be40bb08e37aa07a79 /apps/dav/lib/carddav | |
parent | 1448215055ddfc0c0300ac727952d4f0e904334b (diff) | |
download | nextcloud-server-45baf03a2acbecc46310611f9bd6670b1e93eb00.tar.gz nextcloud-server-45baf03a2acbecc46310611f9bd6670b1e93eb00.zip |
Test ownCloud CardDAV sharing using CalDAVTester
Diffstat (limited to 'apps/dav/lib/carddav')
-rw-r--r-- | apps/dav/lib/carddav/addressbook.php | 31 | ||||
-rw-r--r-- | apps/dav/lib/carddav/card.php | 8 | ||||
-rw-r--r-- | apps/dav/lib/carddav/carddavbackend.php | 57 | ||||
-rw-r--r-- | apps/dav/lib/carddav/sharing/plugin.php | 6 | ||||
-rw-r--r-- | apps/dav/lib/carddav/sharing/xml/sharerequest.php | 19 |
5 files changed, 93 insertions, 28 deletions
diff --git a/apps/dav/lib/carddav/addressbook.php b/apps/dav/lib/carddav/addressbook.php index 3e3e751828e..34b580827c1 100644 --- a/apps/dav/lib/carddav/addressbook.php +++ b/apps/dav/lib/carddav/addressbook.php @@ -68,7 +68,7 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres function getShares() { /** @var CardDavBackend $carddavBackend */ $carddavBackend = $this->carddavBackend; - $carddavBackend->getShares($this->getName()); + return $carddavBackend->getShares($this->getBookId()); } function getACL() { @@ -81,7 +81,26 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres ]; } - return $acl; + // add the current user + if (isset($this->addressBookInfo['{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'])) { + $owner = $this->addressBookInfo['{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal']; + $acl[] = [ + 'privilege' => '{DAV:}read', + 'principal' => $owner, + 'protected' => true, + ]; + if ($this->addressBookInfo['{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only']) { + $acl[] = [ + 'privilege' => '{DAV:}write', + 'principal' => $owner, + 'protected' => true, + ]; + } + } + + /** @var CardDavBackend $carddavBackend */ + $carddavBackend = $this->carddavBackend; + return $carddavBackend->applyShareAcl($this->getBookId(), $acl); } function getChildACL() { @@ -94,7 +113,9 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres ]; } - return $acl; + /** @var CardDavBackend $carddavBackend */ + $carddavBackend = $this->carddavBackend; + return $carddavBackend->applyShareAcl($this->getBookId(), $acl); } function getChild($name) { @@ -105,4 +126,8 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres return new Card($this->carddavBackend, $this->addressBookInfo, $obj); } + private function getBookId() { + return $this->addressBookInfo['id']; + } + } diff --git a/apps/dav/lib/carddav/card.php b/apps/dav/lib/carddav/card.php index 5ec9a7e93a9..d848f2e28ec 100644 --- a/apps/dav/lib/carddav/card.php +++ b/apps/dav/lib/carddav/card.php @@ -33,7 +33,13 @@ class Card extends \Sabre\CardDAV\Card { ]; } - return $acl; + /** @var CardDavBackend $carddavBackend */ + $carddavBackend = $this->carddavBackend; + return $carddavBackend->applyShareAcl($this->getBookId(), $acl); + } + + private function getBookId() { + return $this->addressBookInfo['id']; } } diff --git a/apps/dav/lib/carddav/carddavbackend.php b/apps/dav/lib/carddav/carddavbackend.php index e8a5a776646..3a98fd6e810 100644 --- a/apps/dav/lib/carddav/carddavbackend.php +++ b/apps/dav/lib/carddav/carddavbackend.php @@ -53,6 +53,10 @@ class CardDavBackend implements BackendInterface, SyncSupport { 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); + const ACCESS_OWNER = 1; + const ACCESS_READ_WRITE = 2; + const ACCESS_READ = 3; + /** * CardDavBackend constructor. * @@ -109,14 +113,11 @@ class CardDavBackend implements BackendInterface, SyncSupport { $principals[]= $principalUri; $query = $this->db->getQueryBuilder(); - $query2 = $this->db->getQueryBuilder(); - $query2->select(['resourceid']) - ->from('dav_shares') - ->where($query2->expr()->in('principaluri', $query2->createParameter('principaluri'))) - ->andWhere($query2->expr()->eq('type', $query2->createParameter('type'))); - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) - ->from('addressbooks') - ->where($query->expr()->in('id', $query->createFunction($query2->getSQL()))) + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.uri', 's.access']) + ->from('dav_shares', 's') + ->join('s', 'addressbooks', 'a', 's.resourceid = a.id') + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) ->setParameter('type', 'addressbook') ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) ->execute(); @@ -125,11 +126,13 @@ class CardDavBackend implements BackendInterface, SyncSupport { $addressBooks[] = [ 'id' => $row['id'], 'uri' => $row['uri'], - 'principaluri' => $row['principaluri'], + 'principaluri' => $principalUri, '{DAV:}displayname' => $row['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\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], + '{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $row['access'] === self::ACCESS_READ, ]; } $result->closeCursor(); @@ -782,12 +785,14 @@ class CardDavBackend implements BackendInterface, SyncSupport { // remove the share if it already exists $this->unshare($addressBookUri, $element['href']); + $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE; + $newUri = sha1($addressBookUri . $addressBook['principaluri']); $query = $this->db->getQueryBuilder(); $query->insert('dav_shares') ->values([ 'principaluri' => $query->createNamedParameter($parts[1]), - 'uri' => $query->createNamedParameter($addressBookUri), + 'uri' => $query->createNamedParameter($newUri), 'type' => $query->createNamedParameter('addressbook'), 'access' => $query->createNamedParameter(0), 'resourceid' => $query->createNamedParameter($addressBook['id']) @@ -835,11 +840,11 @@ class CardDavBackend implements BackendInterface, SyncSupport { * * @return array */ - public function getShares($addressBookUri) { + public function getShares($addressBookId) { $query = $this->db->getQueryBuilder(); $result = $query->select(['principaluri', 'access']) ->from('dav_shares') - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($addressBookId))) ->andWhere($query->expr()->eq('type', $query->createNamedParameter('addressbook'))) ->execute(); @@ -850,7 +855,8 @@ class CardDavBackend implements BackendInterface, SyncSupport { 'href' => "principal:${p['uri']}", 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', 'status' => 1, - 'readOnly' => ($row['access'] === 1) + 'readOnly' => ($row['access'] === self::ACCESS_READ), + '{'.\OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD.'}principal' => $p['uri'] ]; } @@ -945,4 +951,29 @@ class CardDavBackend implements BackendInterface, SyncSupport { return (int)$cardIds['id']; } + + /** + * @param $addressBookId + * @param $acl + * @return array + */ + public function applyShareAcl($addressBookId, $acl) { + + $shares = $this->getShares($addressBookId); + foreach ($shares as $share) { + $acl[] = [ + 'privilege' => '{DAV:}read', + 'principal' => $share['{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], + 'protected' => true, + ]; + if (!$share['readOnly']) { + $acl[] = [ + 'privilege' => '{DAV:}write', + 'principal' => $share['{' . \OCA\DAV\CardDAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], + 'protected' => true, + ]; + } + } + return $acl; + } } diff --git a/apps/dav/lib/carddav/sharing/plugin.php b/apps/dav/lib/carddav/sharing/plugin.php index 7ad3f43dca8..d25b84d01f3 100644 --- a/apps/dav/lib/carddav/sharing/plugin.php +++ b/apps/dav/lib/carddav/sharing/plugin.php @@ -34,6 +34,8 @@ use Sabre\HTTP\ResponseInterface; class Plugin extends ServerPlugin { + const NS_OWNCLOUD = 'http://owncloud.org/ns'; + /** @var Auth */ private $auth; @@ -100,7 +102,7 @@ class Plugin extends ServerPlugin { function initialize(Server $server) { $this->server = $server; $server->resourceTypeMapping['OCA\\DAV\CardDAV\\ISharedAddressbook'] = '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}shared'; - $this->server->xml->elementMap['{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}share'] = 'OCA\\DAV\\CardDAV\\Sharing\\Xml\\ShareRequest'; + $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = 'OCA\\DAV\\CardDAV\\Sharing\\Xml\\ShareRequest'; $this->server->on('method:POST', [$this, 'httpPost']); } @@ -148,7 +150,7 @@ class Plugin extends ServerPlugin { // Dealing with the 'share' document, which modified invitees on a // calendar. - case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}share' : + case '{' . self::NS_OWNCLOUD . '}share' : // We can only deal with IShareableCalendar objects if (!$node instanceof IShareableAddressBook) { diff --git a/apps/dav/lib/carddav/sharing/xml/sharerequest.php b/apps/dav/lib/carddav/sharing/xml/sharerequest.php index 6be6bd795a1..bd55dd4073e 100644 --- a/apps/dav/lib/carddav/sharing/xml/sharerequest.php +++ b/apps/dav/lib/carddav/sharing/xml/sharerequest.php @@ -20,6 +20,7 @@ */ namespace OCA\DAV\CardDAV\Sharing\Xml; +use OCA\DAV\CardDAV\Sharing\Plugin; use Sabre\Xml\Reader; use Sabre\Xml\XmlDeserializable; @@ -44,32 +45,32 @@ class ShareRequest implements XmlDeserializable { static function xmlDeserialize(Reader $reader) { - $elems = $reader->parseInnerTree([ - '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV. '}set' => 'Sabre\\Xml\\Element\\KeyValue', - '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}remove' => 'Sabre\\Xml\\Element\\KeyValue', + $elements = $reader->parseInnerTree([ + '{' . Plugin::NS_OWNCLOUD. '}set' => 'Sabre\\Xml\\Element\\KeyValue', + '{' . Plugin::NS_OWNCLOUD . '}remove' => 'Sabre\\Xml\\Element\\KeyValue', ]); $set = []; $remove = []; - foreach ($elems as $elem) { + foreach ($elements as $elem) { switch ($elem['name']) { - case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}set' : + case '{' . Plugin::NS_OWNCLOUD . '}set' : $sharee = $elem['value']; - $sumElem = '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}summary'; - $commonName = '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}common-name'; + $sumElem = '{' . Plugin::NS_OWNCLOUD . '}summary'; + $commonName = '{' . Plugin::NS_OWNCLOUD . '}common-name'; $set[] = [ 'href' => $sharee['{DAV:}href'], 'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null, 'summary' => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null, - 'readOnly' => !array_key_exists('{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}read-write', $sharee), + 'readOnly' => !array_key_exists('{' . Plugin::NS_OWNCLOUD . '}read-write', $sharee), ]; break; - case '{' . \Sabre\CardDAV\Plugin::NS_CARDDAV . '}remove' : + case '{' . Plugin::NS_OWNCLOUD . '}remove' : $remove[] = $elem['value']['{DAV:}href']; break; |