get addressbook url and carddav user from remote server

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-02-24 16:09:52 +01:00 committed by Roeland Jago Douma
parent 930c507d89
commit d5dec527c9
No known key found for this signature in database
GPG Key ID: F941078878347C0C
3 changed files with 35 additions and 21 deletions

View File

@ -75,6 +75,7 @@ class SyncService {
/**
* @param string $url
* @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret
* @param string $syncToken
* @param int $targetBookId
@ -83,14 +84,14 @@ class SyncService {
* @return string
* @throws \Exception
*/
public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
// 1. create addressbook
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
$addressBookId = $book['id'];
// 2. query changes
try {
$response = $this->requestSyncReport($url, $userName, $sharedSecret, $syncToken);
$response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken);
} catch (ClientHttpException $ex) {
if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) {
// remote server revoked access to the address book, remove it
@ -105,7 +106,7 @@ class SyncService {
foreach ($response['response'] as $resource => $status) {
$cardUri = basename($resource);
if (isset($status[200])) {
$vCard = $this->download($url, $sharedSecret, $resource);
$vCard = $this->download($url, $userName, $sharedSecret, $resource);
$existingCard = $this->backend->getCard($addressBookId, $cardUri);
if ($existingCard === false) {
$this->backend->createCard($addressBookId, $cardUri, $vCard['body']);
@ -162,6 +163,7 @@ class SyncService {
/**
* @param string $url
* @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret
* @return Client
*/
@ -185,31 +187,32 @@ class SyncService {
/**
* @param string $url
* @param string $userName
* @param string $addressBookUrl
* @param string $sharedSecret
* @param string $syncToken
* @return array
*/
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
$client = $this->getClient($url, $userName, $sharedSecret);
protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) {
$client = $this->getClient($url, $userName, $sharedSecret);
$addressBookUrl = "remote.php/dav/addressbooks/system/system/system";
$body = $this->buildSyncCollectionRequestBody($syncToken);
$body = $this->buildSyncCollectionRequestBody($syncToken);
$response = $client->request('REPORT', $addressBookUrl, $body, [
'Content-Type' => 'application/xml'
]);
$response = $client->request('REPORT', $addressBookUrl, $body, [
'Content-Type' => 'application/xml'
]);
return $this->parseMultiStatus($response['body']);
}
return $this->parseMultiStatus($response['body']);
}
/**
* @param string $url
* @param string $userName
* @param string $sharedSecret
* @param string $resourcePath
* @return array
*/
protected function download($url, $sharedSecret, $resourcePath) {
$client = $this->getClient($url, 'system', $sharedSecret);
protected function download($url, $userName, $sharedSecret, $resourcePath) {
$client = $this->getClient($url, $userName, $sharedSecret);
return $client->request('GET', $resourcePath);
}

View File

@ -135,7 +135,8 @@ class Application extends \OCP\AppFramework\App {
public function getSyncService() {
$syncService = \OC::$server->query('CardDAVSyncService');
$dbHandler = $this->getContainer()->query('DbHandler');
return new SyncFederationAddressBooks($dbHandler, $syncService);
$discoveryService = \OC::$server->getOCSDiscoveryService();
return new SyncFederationAddressBooks($dbHandler, $syncService, $discoveryService);
}
}

View File

@ -23,12 +23,10 @@
*/
namespace OCA\Federation;
use OC\OCS\DiscoveryService;
use OCA\DAV\CardDAV\SyncService;
use OCP\AppFramework\Http;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use OCP\OCS\IDiscoveryService;
class SyncFederationAddressBooks {
@ -38,13 +36,21 @@ class SyncFederationAddressBooks {
/** @var SyncService */
private $syncService;
/** @var DiscoveryService */
private $ocsDiscoveryService;
/**
* @param DbHandler $dbHandler
* @param SyncService $syncService
* @param IDiscoveryService $ocsDiscoveryService
*/
function __construct(DbHandler $dbHandler, SyncService $syncService) {
public function __construct(DbHandler $dbHandler,
SyncService $syncService,
IDiscoveryService $ocsDiscoveryService
) {
$this->syncService = $syncService;
$this->dbHandler = $dbHandler;
$this->ocsDiscoveryService = $ocsDiscoveryService;
}
/**
@ -59,6 +65,10 @@ class SyncFederationAddressBooks {
$sharedSecret = $trustedServer['shared_secret'];
$syncToken = $trustedServer['sync_token'];
$endPoints = $this->ocsDiscoveryService->discover($url, 'FEDERATED_SHARING');
$cardDavUser = isset($endPoints['carddav-user']) ? $endPoints['carddav-user'] : 'system';
$addressBookUrl = isset($endPoints['system-address-book']) ? trim($endPoints['system-address-book'], '/') : 'remote.php/dav/addressbooks/system/system/system';
if (is_null($sharedSecret)) {
continue;
}
@ -68,7 +78,7 @@ class SyncFederationAddressBooks {
'{DAV:}displayname' => $url
];
try {
$newToken = $this->syncService->syncRemoteAddressBook($url, 'system', $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
$newToken = $this->syncService->syncRemoteAddressBook($url, $cardDavUser, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetBookProperties);
if ($newToken !== $syncToken) {
$this->dbHandler->setServerStatus($url, TrustedServers::STATUS_OK, $newToken);
}