From 004ede2a6aee93788bcc2e9c341f1ee5354cc962 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Mon, 14 Oct 2019 19:22:36 +0200 Subject: [PATCH] refs #169 don't get contacts from disabled address books Signed-off-by: Julien Veyssier --- lib/Controller/ContactsController.php | 65 +++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/Controller/ContactsController.php b/lib/Controller/ContactsController.php index 3402b22e..3df3067d 100644 --- a/lib/Controller/ContactsController.php +++ b/lib/Controller/ContactsController.php @@ -58,7 +58,20 @@ class ContactsController extends Controller { $addressBooks = $this->contactsManager->getUserAddressBooks(); $result = []; $userid = trim($this->userId); + // get addressbook ids + $bookids = []; foreach ($contacts as $c) { + if (!in_array($c['addressbook-key'], $bookids)) { + array_push($bookids, $c['addressbook-key']); + } + } + // determine if addressbooks are enabled + $bookIsEnabled = $this->getBookIsEnabled($bookids); + + foreach ($contacts as $c) { + if (!$bookIsEnabled[$c['addressbook-key']]) { + continue; + } $addressBookUri = $addressBooks[$c['addressbook-key']]->getUri(); $uid = trim($c['UID']); // we don't give users, just contacts @@ -117,6 +130,44 @@ class ContactsController extends Controller { return new DataResponse($result); } + private function getBookIsEnabled($bookids) { + $bookIsEnabled = []; + $qb = $this->qb; + foreach ($bookids as $bookid) { + // first get the address book uri in oc_addressbooks + $qb->select('uri', 'principaluri') + ->from('addressbooks') + ->where($qb->expr()->eq('id', $qb->createNamedParameter($bookid, IQueryBuilder::PARAM_INT))); + $req = $qb->execute(); + $uri = null; + $principaluri = null; + while ($row = $req->fetch()) { + $uri = $row['uri']; + $principaluri = $row['principaluri']; + } + $req->closeCursor(); + $qb = $qb->resetQueryParts(); + // then check its {http://owncloud.org/ns}enabled property in oc_properties + if ($uri !== null) { + $propertypath = str_replace('principals/users/', 'addressbooks/users/', $principaluri) . '/' . $uri; + $qb->select('propertyvalue') + ->from('properties') + ->where($qb->expr()->eq('propertypath', $qb->createNamedParameter($propertypath, IQueryBuilder::PARAM_STR))) + ->andWhere($qb->expr()->eq('propertyname', $qb->createNamedParameter('{http://owncloud.org/ns}enabled', IQueryBuilder::PARAM_STR))); + $req = $qb->execute(); + $propertyvalue = null; + while ($row = $req->fetch()) { + $propertyvalue = intval($row['propertyvalue']); + } + $req->closeCursor(); + $qb = $qb->resetQueryParts(); + + $bookIsEnabled[$bookid] = ($propertyvalue === null or $propertyvalue === 1); + } + } + return $bookIsEnabled; + } + private function N2FN(string $n) { if ($n) { $spl = explode($n, ';'); @@ -141,7 +192,21 @@ class ContactsController extends Controller { $booksReadOnly = $this->getAddressBooksReadOnly(); $result = []; $userid = trim($this->userId); + + // get addressbook ids + $bookids = []; foreach ($contacts as $c) { + if (!in_array($c['addressbook-key'], $bookids)) { + array_push($bookids, $c['addressbook-key']); + } + } + // determine if addressbooks are enabled + $bookIsEnabled = $this->getBookIsEnabled($bookids); + + foreach ($contacts as $c) { + if (!$bookIsEnabled[$c['addressbook-key']]) { + continue; + } $uid = trim($c['UID']); // we don't give users, just contacts if (strcmp($c['URI'], 'Database:'.$c['UID'].'.vcf') !== 0 and