Adding a custom webdav property which holds the list of contacts groups

This commit is contained in:
Thomas Müller 2016-02-10 17:06:13 +01:00
parent 159a0eb597
commit c919b41395
5 changed files with 119 additions and 2 deletions

View File

@ -161,4 +161,11 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable {
}
parent::delete();
}
public function getContactsGroups() {
/** @var CardDavBackend $cardDavBackend */
$cardDavBackend = $this->carddavBackend;
return $cardDavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES');
}
}

View File

@ -29,6 +29,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\DAV\DAV\Sharing\Backend;
use OCA\DAV\DAV\Sharing\IShareable;
use OCP\IDBConnection;
use PDO;
use Sabre\CardDAV\Backend\BackendInterface;
use Sabre\CardDAV\Backend\SyncSupport;
use Sabre\CardDAV\Plugin;
@ -761,6 +762,25 @@ class CardDavBackend implements BackendInterface, SyncSupport {
}
/**
* @param int $bookId
* @param string $name
* @return array
*/
public function collectCardProperties($bookId, $name) {
$query = $this->db->getQueryBuilder();
$result = $query->selectDistinct('value')
->from($this->dbCardsPropertiesTable)
->where($query->expr()->eq('name', $query->createNamedParameter($name)))
->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId)))
->execute();
$all = $result->fetchAll(PDO::FETCH_COLUMN);
$result->closeCursor();
return $all;
}
/**
* get URI from a given contact
*

View File

@ -21,10 +21,19 @@
namespace OCA\DAV\CardDAV;
use OCA\DAV\CardDAV\Xml\Groups;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\HTTP\URLUtil;
class Plugin extends \Sabre\CardDAV\Plugin {
function initialize(Server $server) {
$server->on('propFind', [$this, 'propFind']);
parent::initialize($server);
}
/**
* Returns the addressbook home for a given principal
*
@ -33,15 +42,34 @@ class Plugin extends \Sabre\CardDAV\Plugin {
*/
protected function getAddressbookHomeForPrincipal($principal) {
if (strrpos($principal, 'principals/users', -strlen($principal)) !== FALSE) {
if (strrpos($principal, 'principals/users', -strlen($principal)) !== false) {
list(, $principalId) = URLUtil::splitPath($principal);
return self::ADDRESSBOOK_ROOT . '/users/' . $principalId;
}
if (strrpos($principal, 'principals/system', -strlen($principal)) !== FALSE) {
if (strrpos($principal, 'principals/system', -strlen($principal)) !== false) {
list(, $principalId) = URLUtil::splitPath($principal);
return self::ADDRESSBOOK_ROOT . '/system/' . $principalId;
}
throw new \LogicException('This is not supposed to happen');
}
/**
* Adds all CardDAV-specific properties
*
* @param PropFind $propFind
* @param INode $node
* @return void
*/
function propFind(PropFind $propFind, INode $node) {
$ns = '{http://owncloud.org/ns}';
if ($node instanceof AddressBook) {
$propFind->handle($ns . 'groups', function () use ($node) {
return new Groups($node->getContactsGroups());
});
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\CardDAV\Xml;
use Sabre\Xml\XmlSerializable;
use Sabre\Xml\Element;
use Sabre\Xml\Writer;
class Groups implements XmlSerializable {
const NS_OWNCLOUD = 'http://owncloud.org/ns';
/** @var string[] of TYPE:CHECKSUM */
private $groups;
/**
* @param string $groups
*/
public function __construct($groups) {
$this->groups = $groups;
}
function xmlSerialize(Writer $writer) {
foreach ($this->groups as $group) {
$writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group);
}
}
}

View File

@ -609,4 +609,21 @@ class CardDavBackendTest extends TestCase {
$this->assertEmpty($this->backend->getContact('uri'));
}
public function testCollectCardProperties() {
$query = $this->db->getQueryBuilder();
$query->insert($this->dbCardsPropertiesTable)
->values(
[
'addressbookid' => $query->createNamedParameter(666),
'cardid' => $query->createNamedParameter(777),
'name' => $query->createNamedParameter('FN'),
'value' => $query->createNamedParameter('John Doe'),
'preferred' => $query->createNamedParameter(0)
]
)
->execute();
$result = $this->backend->collectCardProperties(666, 'FN');
$this->assertEquals(['John Doe'], $result);
}
}