summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/carddav
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-22 16:11:53 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-18 22:15:17 +0100
commited24511185d2b46cc7b1c3756e9247836dabe479 (patch)
treee79fa24f579acadac796c2e9a5ab6ebb9864257d /apps/dav/lib/carddav
parentdca0a0eaf1133b0523acfc4074856485ece51093 (diff)
downloadnextcloud-server-ed24511185d2b46cc7b1c3756e9247836dabe479.tar.gz
nextcloud-server-ed24511185d2b46cc7b1c3756e9247836dabe479.zip
Readjust sharing methods
Diffstat (limited to 'apps/dav/lib/carddav')
-rw-r--r--apps/dav/lib/carddav/addressbook.php9
-rw-r--r--apps/dav/lib/carddav/carddavbackend.php72
2 files changed, 55 insertions, 26 deletions
diff --git a/apps/dav/lib/carddav/addressbook.php b/apps/dav/lib/carddav/addressbook.php
index 34b580827c1..2cfaa7b708c 100644
--- a/apps/dav/lib/carddav/addressbook.php
+++ b/apps/dav/lib/carddav/addressbook.php
@@ -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'];
}
diff --git a/apps/dav/lib/carddav/carddavbackend.php b/apps/dav/lib/carddav/carddavbackend.php
index d0cfc61b7c4..8385f4857d3 100644
--- a/apps/dav/lib/carddav/carddavbackend.php
+++ b/apps/dav/lib/carddav/carddavbackend.php
@@ -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])))
;