]> source.dussan.org Git - nextcloud-server.git/commitdiff
Readjust sharing methods
authorThomas Müller <thomas.mueller@tmit.eu>
Tue, 22 Dec 2015 15:11:53 +0000 (16:11 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Mon, 18 Jan 2016 21:15:17 +0000 (22:15 +0100)
apps/dav/lib/carddav/addressbook.php
apps/dav/lib/carddav/carddavbackend.php

index 34b580827c17c7e946d12daa2543885a488cfacb..2cfaa7b708c0b8cdcb0e8f4d1155af1eba77c5ef 100644 (file)
@@ -50,7 +50,7 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres
        function updateShares(array $add, array $remove) {
                /** @var CardDavBackend $carddavBackend */
                $carddavBackend = $this->carddavBackend;
-               $carddavBackend->updateShares($this->getName(), $add, $remove);
+               $carddavBackend->updateShares($this, $add, $remove);
        }
 
        /**
@@ -119,14 +119,17 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareableAddres
        }
 
        function getChild($name) {
-               $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
+               $obj = $this->carddavBackend->getCard($this->getBookId(), $name);
                if (!$obj) {
                        throw new NotFound('Card not found');
                }
                return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
        }
 
-       private function getBookId() {
+       /**
+        * @return int
+        */
+       public function getBookId() {
                return $this->addressBookInfo['id'];
        }
 
index d0cfc61b7c4de4b9f45eb98de95dbd895d4ce5d9..8385f4857d32f20bc25281a15ba761fbfc8d0393 100644 (file)
@@ -140,11 +140,43 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                return $addressBooks;
        }
 
-       public function getAddressBooksByUri($addressBookUri) {
+       /**
+        * @param int $addressBookId
+        */
+       public function getAddressBookById($addressBookId) {
+               $query = $this->db->getQueryBuilder();
+               $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken'])
+                       ->from('addressbooks')
+                       ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId)))
+                       ->execute();
+
+               $row = $result->fetch();
+               $result->closeCursor();
+               if ($row === false) {
+                       return null;
+               }
+
+               return [
+                       'id'  => $row['id'],
+                       'uri' => $row['uri'],
+                       'principaluri' => $row['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',
+               ];
+       }
+
+       /**
+        * @param $addressBookUri
+        * @return array|null
+        */
+       public function getAddressBooksByUri($principal, $addressBookUri) {
                $query = $this->db->getQueryBuilder();
                $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken'])
                        ->from('addressbooks')
                        ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri)))
+                       ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
                        ->setMaxResults(1)
                        ->execute();
 
@@ -223,6 +255,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
         * @param string $principalUri
         * @param string $url Just the 'basename' of the url.
         * @param array $properties
+        * @return int
         * @throws BadRequest
         */
        function createAddressBook($principalUri, $url, array $properties) {
@@ -266,6 +299,8 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                        ])
                        ->setParameters($values)
                        ->execute();
+
+               return $query->getLastInsertId();
        }
 
        /**
@@ -669,16 +704,16 @@ class CardDavBackend implements BackendInterface, SyncSupport {
        }
 
        /**
-        * @param string $path
+        * @param AddressBook $book
         * @param string[] $add
         * @param string[] $remove
         */
-       public function updateShares($path, $add, $remove) {
+       public function updateShares($book, $add, $remove) {
                foreach($add as $element) {
-                       $this->shareWith($path, $element);
+                       $this->shareWith($book, $element);
                }
                foreach($remove as $element) {
-                       $this->unshare($path, $element);
+                       $this->unshare($book->getBookId(), $element);
                }
        }
 
@@ -764,10 +799,10 @@ class CardDavBackend implements BackendInterface, SyncSupport {
 
 
        /**
-        * @param string $addressBookUri
+        * @param AddressBook $addressBook
         * @param string $element
         */
-       private function shareWith($addressBookUri, $element) {
+       private function shareWith($addressBook, $element) {
                $user = $element['href'];
                $parts = explode(':', $user, 2);
                if ($parts[0] !== 'principal') {
@@ -778,19 +813,15 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                        return;
                }
 
-               $addressBook = $this->getAddressBooksByUri($addressBookUri);
-               if (is_null($addressBook)) {
-                       return;
-               }
-
                // remove the share if it already exists
-               $this->unshare($addressBookUri, $element['href']);
+               $this->unshare($addressBook->getBookId(), $element['href']);
                $access = self::ACCESS_READ;
                if (isset($element['readOnly'])) {
                        $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE;
                }
 
-               $newUri = sha1($addressBookUri . $addressBook['principaluri']);
+               $newUri = sha1($addressBook->getName() . $addressBook->getOwner());
+//             $newUri = $addressBook->getName() . '-' . $addressBook->getOwner();
                $query = $this->db->getQueryBuilder();
                $query->insert('dav_shares')
                        ->values([
@@ -798,16 +829,16 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                                'uri' => $query->createNamedParameter($newUri),
                                'type' => $query->createNamedParameter('addressbook'),
                                'access' => $query->createNamedParameter($access),
-                               'resourceid' => $query->createNamedParameter($addressBook['id'])
+                               'resourceid' => $query->createNamedParameter($addressBook->getBookId())
                        ]);
                $query->execute();
        }
 
        /**
-        * @param string $addressBookUri
+        * @param int $addressBookId
         * @param string $element
         */
-       private function unshare($addressBookUri, $element) {
+       private function unshare($addressBookId, $element) {
                $parts = explode(':', $element, 2);
                if ($parts[0] !== 'principal') {
                        return;
@@ -817,14 +848,9 @@ class CardDavBackend implements BackendInterface, SyncSupport {
                        return;
                }
 
-               $addressBook = $this->getAddressBooksByUri($addressBookUri);
-               if (is_null($addressBook)) {
-                       return;
-               }
-
                $query = $this->db->getQueryBuilder();
                $query->delete('dav_shares')
-                       ->where($query->expr()->eq('resourceid', $query->createNamedParameter($addressBook['id'])))
+                       ->where($query->expr()->eq('resourceid', $query->createNamedParameter($addressBookId)))
                        ->andWhere($query->expr()->eq('type', $query->createNamedParameter('addressbook')))
                        ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1])))
                ;