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

Right owner for shared AB. Fix #229

This commit is contained in:
Thomas Tanghus 2013-10-30 16:14:48 +01:00
parent 4b94a3d8e1
commit a13a319efb

View File

@ -7,17 +7,19 @@
*/ */
namespace OCA\Contacts\Share; namespace OCA\Contacts\Share;
use OCA\Contacts\Backend\Database; use OCA\Contacts\App;
class Addressbook implements \OCP\Share_Backend_Collection { class Addressbook implements \OCP\Share_Backend_Collection {
const FORMAT_ADDRESSBOOKS = 1; const FORMAT_ADDRESSBOOKS = 1;
const FORMAT_COLLECTION = 2; const FORMAT_COLLECTION = 2;
public $backend; /**
* @var \OCA\Contacts\App;
*/
public $app;
public function __construct() { public function __construct() {
// Currently only share $this->app = new App(\OCP\User::getUser());
$this->backend = new Database();
} }
/** /**
@ -32,8 +34,11 @@ class Addressbook implements \OCP\Share_Backend_Collection {
* The formatItems() function will translate the source returned back into the item * The formatItems() function will translate the source returned back into the item
*/ */
public function isValidSource($itemSource, $uidOwner) { public function isValidSource($itemSource, $uidOwner) {
$addressbook = $this->backend->getAddressBook($itemSource); $app = new App($uidOwner);
if(!$addressbook || $addressbook['owner'] !== $uidOwner) {
try {
$app->getAddressBook('local', $itemSource);
} catch(\Exception $e) {
return false; return false;
} }
return true; return true;
@ -50,18 +55,21 @@ class Addressbook implements \OCP\Share_Backend_Collection {
* If it does generate a new name e.g. name_# * If it does generate a new name e.g. name_#
*/ */
public function generateTarget($itemSource, $shareWith, $exclude = null) { public function generateTarget($itemSource, $shareWith, $exclude = null) {
$addressbook = $this->backend->getAddressBook($itemSource); // Get app for the sharee
$app = new App($shareWith);
$backend = $app->getBackend('local');
$user_addressbooks = array(); // Get address book for the owner
$addressBook = $this->app->getBackend('local')->getAddressBook($itemSource);
//foreach($this->backend->getAddressBooksForUser($shareWith) as $user_addressbook) { $userAddressBooks = array();
// FIXME: The backend used is for the current user, not the right one
foreach($this->backend->getAddressBooksForUser() as $user_addressbook) { foreach($backend->getAddressBooksForUser() as $userAddressBook) {
$user_addressbooks[] = $user_addressbook['displayname']; $userAddressBooks[] = $userAddressBook['displayname'];
} }
$name = $addressbook['displayname'] . '(' . $addressbook['userid'] . ')'; $name = $addressBook['displayname'] . '(' . $addressBook['userid'] . ')';
$suffix = ''; $suffix = '';
while (in_array($name.$suffix, $user_addressbooks)) { while (in_array($name.$suffix, $userAddressBooks)) {
$suffix++; $suffix++;
} }
@ -85,31 +93,33 @@ class Addressbook implements \OCP\Share_Backend_Collection {
public function formatItems($items, $format, $parameters = null, $include = false) { public function formatItems($items, $format, $parameters = null, $include = false) {
//\OCP\Util::writeLog('contacts', __METHOD__ //\OCP\Util::writeLog('contacts', __METHOD__
// . ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG); // . ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
$addressbooks = array(); $addressBooks = array();
$backend = $this->app->getBackend('local');
if ($format === self::FORMAT_ADDRESSBOOKS) { if ($format === self::FORMAT_ADDRESSBOOKS) {
foreach ($items as $item) { foreach ($items as $item) {
//\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $item['item_source'] . ' include: ' //\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $item['item_source'] . ' include: '
// . (int)$include, \OCP\Util::DEBUG); // . (int)$include, \OCP\Util::DEBUG);
$addressbook = $this->backend->getAddressBook($item['item_source']); $addressBook = $backend->getAddressBook($item['item_source']);
if ($addressbook) { if ($addressBook) {
$addressbook['displayname'] = $addressbook['displayname'] . ' (' . $addressbook['owner'] . ')'; $addressBook['displayname'] = $addressBook['displayname'] . ' (' . $addressBook['owner'] . ')';
$addressbook['permissions'] = $item['permissions']; $addressBook['permissions'] = $item['permissions'];
$addressbooks[] = $addressbook; $addressBooks[] = $addressBook;
} }
} }
} elseif ($format === self::FORMAT_COLLECTION) { } elseif ($format === self::FORMAT_COLLECTION) {
foreach ($items as $item) { foreach ($items as $item) {
} }
} }
return $addressbooks; return $addressBooks;
} }
public function getChildren($itemSource) { public function getChildren($itemSource) {
\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $itemSource, \OCP\Util::DEBUG); \OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $itemSource, \OCP\Util::DEBUG);
$contacts = $this->backend->getContacts($itemSource, null, null, true); $contacts = $this->app->getBackend('local')->getContacts($itemSource);
$children = array(); $children = array();
foreach($contacts as $contact) { foreach($contacts as $contact) {
$children[] = array('source' => $contact['id'], 'target' => $contact['fullname']); $children[] = array('source' => $contact['id'], 'target' => $contact['displayname']);
} }
return $children; return $children;
} }