Merge pull request #20948 from owncloud/fed-sync-contacts
Syncing system addressbooks across federated ownCloudsremotes/origin/app-styles-content-list
commit
59e9b93be6
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, 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;
|
||||
|
||||
use Sabre\DAV\Client;
|
||||
use Sabre\DAV\Xml\Response\MultiStatus;
|
||||
use Sabre\DAV\Xml\Service;
|
||||
use Sabre\HTTP\ClientException;
|
||||
|
||||
class SyncService {
|
||||
|
||||
/** @var CardDavBackend */
|
||||
private $backend;
|
||||
|
||||
public function __construct(CardDavBackend $backend) {
|
||||
$this->backend = $backend;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $userName
|
||||
* @param string $sharedSecret
|
||||
* @param string $syncToken
|
||||
* @param int $targetBookId
|
||||
* @param string $targetPrincipal
|
||||
* @param array $targetProperties
|
||||
* @return string
|
||||
*/
|
||||
public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
|
||||
// 1. create addressbook
|
||||
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
|
||||
$addressBookId = $book['id'];
|
||||
|
||||
// 2. query changes
|
||||
$response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken);
|
||||
|
||||
// 3. apply changes
|
||||
// TODO: use multi-get for download
|
||||
foreach ($response['response'] as $resource => $status) {
|
||||
$cardUri = basename($resource);
|
||||
if (isset($status[200])) {
|
||||
$vCard = $this->download($url, $sharedSecret, $resource);
|
||||
$existingCard = $this->backend->getCard($addressBookId, $cardUri);
|
||||
if ($existingCard === false) {
|
||||
$this->backend->createCard($addressBookId, $cardUri, $vCard['body']);
|
||||
} else {
|
||||
$this->backend->updateCard($addressBookId, $cardUri, $vCard['body']);
|
||||
}
|
||||
} else {
|
||||
$this->backend->deleteCard($addressBookId, $cardUri);
|
||||
}
|
||||
}
|
||||
|
||||
return $response['token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $principal
|
||||
* @param string $id
|
||||
* @param array $properties
|
||||
* @return array|null
|
||||
* @throws \Sabre\DAV\Exception\BadRequest
|
||||
*/
|
||||
protected function ensureSystemAddressBookExists($principal, $id, $properties) {
|
||||
$book = $this->backend->getAddressBooksByUri($id);
|
||||
if (!is_null($book)) {
|
||||
return $book;
|
||||
}
|
||||
$this->backend->createAddressBook($principal, $id, $properties);
|
||||
|
||||
return $this->backend->getAddressBooksByUri($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $userName
|
||||
* @param string $sharedSecret
|
||||
* @param string $syncToken
|
||||
* @return array
|
||||
*/
|
||||
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
|
||||
$settings = [
|
||||
'baseUri' => $url . '/',
|
||||
'userName' => $userName,
|
||||
'password' => $sharedSecret,
|
||||
];
|
||||
$client = new Client($settings);
|
||||
$client->setThrowExceptions(true);
|
||||
|
||||
$addressBookUrl = "remote.php/dav/addressbooks/system/system/system";
|
||||
$body = $this->buildSyncCollectionRequestBody($syncToken);
|
||||
|
||||
$response = $client->request('REPORT', $addressBookUrl, $body, [
|
||||
'Content-Type' => 'application/xml'
|
||||
]);
|
||||
|
||||
$result = $this->parseMultiStatus($response['body']);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $sharedSecret
|
||||
* @param string $resourcePath
|
||||
* @return array
|
||||
*/
|
||||
private function download($url, $sharedSecret, $resourcePath) {
|
||||
$settings = [
|
||||
'baseUri' => $url,
|
||||
'userName' => 'system',
|
||||
'password' => $sharedSecret,
|
||||
];
|
||||
$client = new Client($settings);
|
||||
$client->setThrowExceptions(true);
|
||||
|
||||
$response = $client->request('GET', $resourcePath);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $syncToken
|
||||
* @return string
|
||||
*/
|
||||
private function buildSyncCollectionRequestBody($syncToken) {
|
||||
|
||||
$dom = new \DOMDocument('1.0', 'UTF-8');
|
||||
$dom->formatOutput = true;
|
||||
$root = $dom->createElementNS('DAV:', 'd:sync-collection');
|
||||
$sync = $dom->createElement('d:sync-token', $syncToken);
|
||||
$prop = $dom->createElement('d:prop');
|
||||
$cont = $dom->createElement('d:getcontenttype');
|
||||
$etag = $dom->createElement('d:getetag');
|
||||
|
||||
$prop->appendChild($cont);
|
||||
$prop->appendChild($etag);
|
||||
$root->appendChild($sync);
|
||||
$root->appendChild($prop);
|
||||
$dom->appendChild($root);
|
||||
$body = $dom->saveXML();
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $body
|
||||
* @return array
|
||||
* @throws \Sabre\Xml\ParseException
|
||||
*/
|
||||
private function parseMultiStatus($body) {
|
||||
$xml = new Service();
|
||||
|
||||
/** @var MultiStatus $multiStatus */
|
||||
$multiStatus = $xml->expect('{DAV:}multistatus', $body);
|
||||
|
||||
$result = [];
|
||||
foreach ($multiStatus->getResponses() as $response) {
|
||||
$result[$response->getHref()] = $response->getResponseProperties();
|
||||
}
|
||||
|
||||
return ['response' => $result, 'token' => $multiStatus->getSyncToken()];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, 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;
|
||||
|
||||
use Test\TestCase;
|
||||
|
||||
class SyncServiceTest extends TestCase {
|
||||
public function testEmptySync() {
|
||||
$backend = $this->getBackendMock(0, 0, 0);
|
||||
|
||||
$ss = $this->getSyncServiceMock($backend, []);
|
||||
$return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
||||
$this->assertEquals('sync-token-1', $return);
|
||||
}
|
||||
|
||||
public function testSyncWithNewElement() {
|
||||
$backend = $this->getBackendMock(1, 0, 0);
|
||||
$backend->method('getCard')->willReturn(false);
|
||||
|
||||
$ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
|
||||
$return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
||||
$this->assertEquals('sync-token-1', $return);
|
||||
}
|
||||
|
||||
public function testSyncWithUpdatedElement() {
|
||||
$backend = $this->getBackendMock(0, 1, 0);
|
||||
$backend->method('getCard')->willReturn(true);
|
||||
|
||||
$ss = $this->getSyncServiceMock($backend, ['0' => [200 => '']]);
|
||||
$return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
||||
$this->assertEquals('sync-token-1', $return);
|
||||
}
|
||||
|
||||
public function testSyncWithDeletedElement() {
|
||||
$backend = $this->getBackendMock(0, 0, 1);
|
||||
|
||||
$ss = $this->getSyncServiceMock($backend, ['0' => [404 => '']]);
|
||||
$return = $ss->syncRemoteAddressBook('', 'system', '1234567890', null, '1', 'principals/system/system', []);
|
||||
$this->assertEquals('sync-token-1', $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $createCount
|
||||
* @param int $updateCount
|
||||
* @param int $deleteCount
|
||||
* @return \PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function getBackendMock($createCount, $updateCount, $deleteCount) {
|
||||
$backend = $this->getMockBuilder('OCA\DAV\CardDAV\CardDAVBackend')->disableOriginalConstructor()->getMock();
|
||||
$backend->expects($this->exactly($createCount))->method('createCard');
|
||||
$backend->expects($this->exactly($updateCount))->method('updateCard');
|
||||
$backend->expects($this->exactly($deleteCount))->method('deleteCard');
|
||||
return $backend;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $backend
|
||||
* @param $response
|
||||
* @return SyncService|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function getSyncServiceMock($backend, $response) {
|
||||
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */
|
||||
$ss = $this->getMock('OCA\DAV\CardDAV\SyncService', ['ensureSystemAddressBookExists', 'requestSyncReport', 'download'], [$backend]);
|
||||
$ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
|
||||
$ss->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]);
|
||||
$ss->method('download')->willReturn([
|
||||
'body' => '',
|
||||
'statusCode' => 200,
|
||||
'headers' => []
|
||||
]);
|
||||
return $ss;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
$dbConnection = \OC::$server->getDatabaseConnection();
|
||||
$l10n = \OC::$server->getL10N('federation');
|
||||
$dbHandler = new \OCA\Federation\DbHandler($dbConnection, $l10n);
|
||||
|
||||
/** @var Symfony\Component\Console\Application $application */
|
||||
$application->add(new \OCA\Federation\Command\SyncFederationAddressBooks($dbHandler));
|
||||
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\Federation\Command;
|
||||
|
||||
use OCA\DAV\CardDAV\SyncService;
|
||||
use OCA\Federation\DbHandler;
|
||||
use OCA\Federation\TrustedServers;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class SyncFederationAddressBooks extends Command {
|
||||
|
||||
/** @var DbHandler */
|
||||
protected $dbHandler;
|
||||
|
||||
/** @var SyncService */
|
||||
private $syncService;
|
||||
|
||||
/**
|
||||
* @param DbHandler $dbHandler
|
||||
*/
|
||||
function __construct(DbHandler $dbHandler) {
|
||||
parent::__construct();
|
||||
|
||||
$this->syncService = \OC::$server->query('CardDAVSyncService');
|
||||
$this->dbHandler = $dbHandler;
|
||||
}
|
||||
|
||||
protected function configure() {
|
||||
$this
|
||||
->setName('federation:sync-addressbooks')
|
||||
->setDescription('Synchronizes addressbooks of all federated clouds');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output) {
|
||||
|
||||
$progress = new ProgressBar($output);
|
||||
$progress->start();
|
||||
$trustedServers = $this->dbHandler->getAllServer();
|
||||
foreach ($trustedServers as $trustedServer) {
|
||||
$progress->advance();
|
||||
$url = $trustedServer['url'];
|
||||
$sharedSecret = $trustedServer['shared_secret'];
|
||||
$syncToken = $trustedServer['sync_token'];
|
||||
|
||||
if (is_null($sharedSecret)) {
|
||||
continue;
|
||||
}
|
||||
$targetBookId = sha1($url);
|
||||
$targetPrincipal = "principals/system/system";
|
||||
$targetBookProperties = [
|
||||
'{DAV:}displayname' => $url
|
||||
];
|
||||
try {
|
||||
$newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetPrincipal, $targetBookId, $targetBookProperties);
|
||||
if ($newToken !== $syncToken) {
|
||||
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
$output->writeln("Error while syncing $url : " . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
$progress->finish();
|
||||
$output->writeln('');
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, 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\Federation\DAV;
|
||||
|
||||
use OCA\Federation\DbHandler;
|
||||
use Sabre\DAV\Auth\Backend\AbstractBasic;
|
||||
|
||||
class FedAuth extends AbstractBasic {
|
||||
|
||||
/** @var DbHandler */
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* FedAuth constructor.
|
||||
*
|
||||
* @param DbHandler $db
|
||||
*/
|
||||
public function __construct(DbHandler $db) {
|
||||
$this->db = $db;
|
||||
$this->principalPrefix = 'principals/system/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a username and password
|
||||
*
|
||||
* This method should return true or false depending on if login
|
||||
* succeeded.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUserPass($username, $password) {
|
||||
return $this->db->auth($username, $password);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, 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\Federation\Tests\DAV;
|
||||
|
||||
use OCA\Federation\DAV\FedAuth;
|
||||
use OCA\Federation\DbHandler;
|
||||
use Test\TestCase;
|
||||
|
||||
class FedAuthTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @dataProvider providesUser
|
||||
*
|
||||
* @param array $expected
|
||||
* @param string $user
|
||||
* @param string $password
|
||||
*/
|
||||
public function testFedAuth($expected, $user, $password) {
|
||||
/** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $db */
|
||||
$db = $this->getMockBuilder('OCA\Federation\DbHandler')->disableOriginalConstructor()->getMock();
|
||||
$db->method('auth')->willReturn(true);
|
||||
$auth = new FedAuth($db);
|
||||
$result = $this->invokePrivate($auth, 'validateUserPass', [$user, $password]);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function providesUser() {
|
||||
return [
|
||||
[true, 'system', '123456']
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue