1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00

Contacts: Use a backend name/id combo for CardDAV

This commit is contained in:
Thomas Tanghus 2013-04-08 01:27:35 +02:00
parent ac0d12470b
commit a308a3ed21
4 changed files with 50 additions and 44 deletions

View File

@ -386,6 +386,7 @@ class Database extends AbstractBackend {
* @return array * @return array
*/ */
public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) { public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) {
//\OCP\Util::writeLog('contacts', __METHOD__.' addressbookid: ' . $addressbookid, \OCP\Util::DEBUG);
$cards = array(); $cards = array();
try { try {
$qfields = $omitdata ? '`id`, `fullname` AS `displayname`' : '*'; $qfields = $omitdata ? '`id`, `fullname` AS `displayname`' : '*';
@ -413,7 +414,7 @@ class Database extends AbstractBackend {
} }
/** /**
* Returns a specfic contact. * Returns a specific contact.
* *
* The $id for Database and Shared backends can be an array containing * The $id for Database and Shared backends can be an array containing
* either 'id' or 'uri' to be able to play seamlessly with the * either 'id' or 'uri' to be able to play seamlessly with the
@ -429,10 +430,8 @@ class Database extends AbstractBackend {
* @return array|false * @return array|false
*/ */
public function getContact($addressbookid, $id, $noCollection = false) { public function getContact($addressbookid, $id, $noCollection = false) {
//\OCP\Util::writeLog('contacts', __METHOD__.' identifier: ' //\OCP\Util::writeLog('contacts', __METHOD__.' identifier: ' . $addressbookid . ' ' . $id['uri'], \OCP\Util::DEBUG);
// . print_r($id, true), \OCP\Util::DEBUG);
$ids = array($id);
$where_query = '`id` = ?'; $where_query = '`id` = ?';
if(is_array($id)) { if(is_array($id)) {
$where_query = ''; $where_query = '';
@ -445,8 +444,10 @@ class Database extends AbstractBackend {
throw new \Exception( throw new \Exception(
__METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.' __METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.'
); );
return false;
} }
} }
$ids = array($id);
if(!$noCollection) { if(!$noCollection) {
$where_query .= ' AND `addressbookid` = ?'; $where_query .= ' AND `addressbookid` = ?';

View File

@ -89,8 +89,9 @@ class AddressBook extends \Sabre_CardDAV_AddressBook {
), ),
); );
if($uid != \OCP\User::getUser()) { if($uid !== \OCP\User::getUser()) {
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $this->addressBookInfo['id']); list($backendName, $id) = explode('::', $this->addressBookInfo['id']);
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if($sharedAddressbook) { if($sharedAddressbook) {
if(($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE) if(($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE)
&& ($sharedAddressbook['permissions'] & \OCP\PERMISSION_UPDATE) && ($sharedAddressbook['permissions'] & \OCP\PERMISSION_UPDATE)

View File

@ -39,19 +39,17 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
*/ */
public function getAddressBooksForUser($principaluri) { public function getAddressBooksForUser($principaluri) {
$userid = $this->userIDByPrincipal($principaluri); $userid = $this->userIDByPrincipal($principaluri);
$addressBooks = array(); $userAddressBooks = array();
foreach($this->backends as $backend) { foreach($this->backends as $backend) {
$addressBooks = array_merge($addressBooks, $backend->getAddressBooksForUser($userid)); $addressBooks = $backend->getAddressBooksForUser($userid);
}
$addressbooks = array();
foreach($addressBooks as $addressBook) { foreach($addressBooks as $addressBook) {
if($addressBook['owner'] != \OCP\USER::getUser()) { if($addressBook['owner'] != \OCP\USER::getUser()) {
$addressBook['uri'] = $addressBook['uri'] . '_shared_by_' . $addressBook['owner']; $addressBook['uri'] = $addressBook['uri'] . '_shared_by_' . $addressBook['owner'];
$addressBook['displayname'] = $addressBook['displayname']; $addressBook['displayname'] = $addressBook['displayname'];
} }
$addressbooks[] = array( $userAddressbooks[] = array(
'id' => $addressBook['id'], 'id' => $backend->name . '::' . $addressBook['id'],
'uri' => $addressBook['uri'], 'uri' => $addressBook['uri'],
'principaluri' => 'principals/'.$addressBook['owner'], 'principaluri' => 'principals/'.$addressBook['owner'],
'{DAV:}displayname' => $addressBook['displayname'], '{DAV:}displayname' => $addressBook['displayname'],
@ -60,8 +58,9 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
'{http://calendarserver.org/ns/}getctag' => $addressBook['lastmodified'], '{http://calendarserver.org/ns/}getctag' => $addressBook['lastmodified'],
); );
} }
}
return $addressbooks; return $userAddressbooks;
} }
@ -97,7 +96,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
} }
} }
return $this->backend->updateAddressBook($addressbookid, $changes); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
return $backend->updateAddressBook($id, $changes);
} }
@ -133,7 +133,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
$properties['uri'] = $uri; $properties['uri'] = $uri;
$this->backend->createAddressBook($properties, $userid); list(,$backend) = $this->getBackendForAddressBook($addressbookid);
$backend->createAddressBook($properties, $userid);
} }
/** /**
@ -143,8 +144,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
* @return void * @return void
*/ */
public function deleteAddressBook($addressbookid) { public function deleteAddressBook($addressbookid) {
$backend = $this->getBackendForAddressBook($addressbookid); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->deleteAddressBook($addressbookid); $backend->deleteAddressBook($id);
} }
/** /**
@ -155,8 +156,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
*/ */
public function getCards($addressbookid) { public function getCards($addressbookid) {
$contacts = array(); $contacts = array();
$backend = $this->getBackendForAddressBook($addressbookid); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$contacts = $backend->getContacts($addressbookid); $contacts = $backend->getContacts($id);
$cards = array(); $cards = array();
foreach($contacts as $contact) { foreach($contacts as $contact) {
@ -181,8 +182,9 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
* @return array * @return array
*/ */
public function getCard($addressbookid, $carduri) { public function getCard($addressbookid, $carduri) {
$backend = $this->getBackendForAddressBook($addressbookid); \OCP\Util::writeLog('contacts', __METHOD__.' identifier: ' . $carduri . ' ' . print_r($addressbookid, true), \OCP\Util::DEBUG);
$contact = $backend->getContact($addressbookid, array('uri' => $carduri)); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$contact = $backend->getContact($id, array('uri' => $carduri));
return ($contact ? $contact : false); return ($contact ? $contact : false);
} }
@ -200,8 +202,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
* @return string|null * @return string|null
*/ */
public function createCard($addressbookid, $carduri, $carddata) { public function createCard($addressbookid, $carduri, $carddata) {
$backend = $this->getBackendForAddressBook($addressbookid); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->createContact($addressbookid, $carddata, $carduri); $backend->createContact($id, $carddata, $carduri);
} }
/** /**
@ -213,8 +215,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
* @return null * @return null
*/ */
public function updateCard($addressbookid, $carduri, $carddata) { public function updateCard($addressbookid, $carduri, $carddata) {
$backend = $this->getBackendForAddressBook($addressbookid); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
$backend->updateContact($addressbookid, array('uri' => $carduri,), $carddata); $backend->updateContact($id, array('uri' => $carduri,), $carddata);
} }
/** /**
@ -225,8 +227,8 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
* @return bool * @return bool
*/ */
public function deleteCard($addressbookid, $carduri) { public function deleteCard($addressbookid, $carduri) {
$backend = $this->getBackendForAddressBook($addressbookid); list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
return $backend->deleteContact($addressbookid); return $backend->deleteContact($id);
} }
/** /**
@ -241,15 +243,16 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
/** /**
* Get the backend for an address book * Get the backend for an address book
* *
* FIXME: Find a better way to get the right backend if possible. * @param mixed $addressbookid
* @param string $addressbookid * @return array(string, \OCA\Contacts\Backend\AbstractBackend)
* @return \OCA\Contacts\Backend\AbstractBackend
*/ */
public function getBackendForAddressBook($addressbookid) { public function getBackendForAddressBook($addressbookid) {
list($backendName, $id) = explode('::', $addressbookid);
foreach($this->backends as $backend) { foreach($this->backends as $backend) {
if($backend->hasAddressBook($addressbookid)) { if($backend->name === $backendName && $backend->hasAddressBook($id)) {
return $backend; return array($id, $backend);
} }
} }
throw new \Sabre_DAV_Exception_NotFound('Backend not found: ' . $addressbookid);
} }
} }

View File

@ -70,7 +70,8 @@ class Card extends \Sabre_CardDAV_Card {
$uid = $this->carddavBackend->userIDByPrincipal($this->getOwner()); $uid = $this->carddavBackend->userIDByPrincipal($this->getOwner());
if($uid != \OCP\USER::getUser()) { if($uid != \OCP\USER::getUser()) {
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $this->addressBookInfo['id']); list($backendName, $id) = explode('::', $this->addressBookInfo['id']);
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if ($sharedAddressbook && ($sharedAddressbook['permissions'] & \OCP\PERMISSION_READ)) { if ($sharedAddressbook && ($sharedAddressbook['permissions'] & \OCP\PERMISSION_READ)) {
$readprincipal = 'principals/' . \OCP\USER::getUser(); $readprincipal = 'principals/' . \OCP\USER::getUser();
} }