Readjust sharing methods

This commit is contained in:
Thomas Müller 2015-12-22 16:11:53 +01:00
parent dca0a0eaf1
commit ed24511185
2 changed files with 55 additions and 26 deletions

View 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'];
}

View 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])))
;