Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federation fixes stable11 #4029

Merged
merged 6 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions apps/dav/lib/CardDAV/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ public function createCardFromUser(IUser $user) {
$image = $this->getAvatarImage($user);

$vCard = new VCard();
$vCard->add(new Text($vCard, 'UID', $uid));
$vCard->VERSION = '3.0';
$vCard->UID = $uid;

$publish = false;

foreach ($userData as $property => $value) {
if ($value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY ||
$value['scope'] === AccountManager::VISIBILITY_PUBLIC
) {

$shareWithTrustedServers =
$value['scope'] === AccountManager::VISIBILITY_CONTACTS_ONLY ||
$value['scope'] === AccountManager::VISIBILITY_PUBLIC;

$emptyValue = !isset($value['value']) || $value['value'] === '';
$noImage = $image === null;

if ($shareWithTrustedServers && (!$emptyValue || !$noImage)) {
$publish = true;
switch ($property) {
case AccountManager::PROPERTY_DISPLAYNAME:
Expand All @@ -71,7 +78,7 @@ public function createCardFromUser(IUser $user) {
break;
case AccountManager::PROPERTY_AVATAR:
if ($image !== null) {
$vCard->add('PHOTO', 'data:'.$image->mimeType().';base64,' . base64_encode($image->data()));
$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
}
break;
case AccountManager::PROPERTY_EMAIL:
Expand Down
66 changes: 50 additions & 16 deletions apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OC\Accounts\AccountManager;
use OCP\AppFramework\Http;
use OCP\ICertificateManager;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -52,6 +53,9 @@ class SyncService {
/** @var AccountManager */
private $accountManager;

/** @var string */
protected $certPath;

/**
* SyncService constructor.
*
Expand All @@ -65,6 +69,7 @@ public function __construct(CardDavBackend $backend, IUserManager $userManager,
$this->userManager = $userManager;
$this->logger = $logger;
$this->accountManager = $accountManager;
$this->certPath = '';
}

/**
Expand Down Expand Up @@ -132,32 +137,69 @@ public function ensureSystemAddressBookExists($principal, $id, $properties) {
return $this->backend->getAddressBooksByUri($principal, $id);
}

/**
* Check if there is a valid certPath we should use
*
* @return string
*/
protected function getCertPath() {

// we already have a valid certPath
if ($this->certPath !== '') {
return $this->certPath;
}

/** @var ICertificateManager $certManager */
$certManager = \OC::$server->getCertificateManager(null);
$certPath = $certManager->getAbsoluteBundlePath();
if (file_exists($certPath)) {
$this->certPath = $certPath;
}

return $this->certPath;
}

/**
* @param string $url
* @param string $userName
* @param string $sharedSecret
* @param string $syncToken
* @return array
* @return Client
*/
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
protected function getClient($url, $userName, $sharedSecret) {
$settings = [
'baseUri' => $url . '/',
'userName' => $userName,
'password' => $sharedSecret,
];
$client = new Client($settings);
$certPath = $this->getCertPath();
$client->setThrowExceptions(true);

if ($certPath !== '' && strpos($url, 'http://') !== 0) {
$client->addCurlSetting(CURLOPT_CAINFO, $this->certPath);
}

return $client;
}

/**
* @param string $url
* @param string $userName
* @param string $sharedSecret
* @param string $syncToken
* @return array
*/
protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken) {
$client = $this->getClient($url, $userName, $sharedSecret);

$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;
return $this->parseMultiStatus($response['body']);
}

/**
Expand All @@ -167,16 +209,8 @@ protected function requestSyncReport($url, $userName, $sharedSecret, $syncToken)
* @return array
*/
protected 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;
$client = $this->getClient($url, 'system', $sharedSecret);
return $client->request('GET', $resourcePath);
}

/**
Expand Down
16 changes: 13 additions & 3 deletions apps/dav/tests/unit/CardDAV/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function providesNewUsers() {
[
'cloud' => 'foo@cloud.net',
'email' => 'foo@bar.net',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5',
'photo' => 'MTIzNDU2Nzg5',
],
null,
'foo@bar.net',
Expand All @@ -157,7 +157,7 @@ public function providesNewUsers() {
'cloud' => 'foo@cloud.net',
'email' => 'foo@bar.net',
'fn' => 'Dr. Foo Bar',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5',
'photo' => 'MTIzNDU2Nzg5',
],
"Dr. Foo Bar",
"foo@bar.net",
Expand All @@ -167,12 +167,22 @@ public function providesNewUsers() {
[
'cloud' => 'foo@cloud.net',
'fn' => 'Dr. Foo Bar',
'photo' => 'data:image/jpeg;base64,MTIzNDU2Nzg5',
'photo' => 'MTIzNDU2Nzg5',
],
"Dr. Foo Bar",
null,
"foo@cloud.net"
],
[
[
'cloud' => 'foo@cloud.net',
'fn' => 'Dr. Foo Bar',
'photo' => 'MTIzNDU2Nzg5',
],
'Dr. Foo Bar',
'',
'foo@cloud.net'
],
];
}

Expand Down
77 changes: 39 additions & 38 deletions apps/dav/tests/unit/CardDAV/SyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,50 +103,50 @@ public function testUpdateAndDeleteUser() {
$user->method('getBackendClassName')->willReturn('unittest');
$user->method('getUID')->willReturn('test-user');
$user->method('getCloudId')->willReturn('cloudId');
$user->method('getDisplayName')->willReturn('test-user');
$accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock();
$accountManager->expects($this->any())->method('getUser')
->willReturn([
AccountManager::PROPERTY_DISPLAYNAME =>
[
'value' => $user->getDisplayName(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
],
AccountManager::PROPERTY_ADDRESS =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_WEBSITE =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_EMAIL =>
[
'value' => $user->getEMailAddress(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
],
AccountManager::PROPERTY_AVATAR =>
[
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
],
AccountManager::PROPERTY_PHONE =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_TWITTER =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
]);
AccountManager::PROPERTY_DISPLAYNAME =>
[
'value' => $user->getDisplayName(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
],
AccountManager::PROPERTY_ADDRESS =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_WEBSITE =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_EMAIL =>
[
'value' => $user->getEMailAddress(),
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
],
AccountManager::PROPERTY_AVATAR =>
[
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
],
AccountManager::PROPERTY_PHONE =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
AccountManager::PROPERTY_TWITTER =>
[
'value' => '',
'scope' => AccountManager::VISIBILITY_PRIVATE,
],
]
);

$ss = new SyncService($backend, $userManager, $logger, $accountManager);
$ss->updateUser($user);

$user->method('getDisplayName')->willReturn('A test user for unit testing');

$ss->updateUser($user);

$ss->deleteUser($user);
Expand Down Expand Up @@ -179,7 +179,7 @@ private function getSyncServiceMock($backend, $response) {
$accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock();
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */
$ss = $this->getMockBuilder(SyncService::class)
->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download'])
->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download', 'getCertPath'])
->setConstructorArgs([$backend, $userManager, $logger, $accountManager])
->getMock();
$ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']);
Expand All @@ -189,6 +189,7 @@ private function getSyncServiceMock($backend, $response) {
'statusCode' => 200,
'headers' => []
]);
$ss->method('getCertPath')->willReturn('');
return $ss;
}

Expand Down
4 changes: 4 additions & 0 deletions apps/federation/lib/Middleware/AddServerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCA\Federation\Middleware;

use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Middleware;
Expand Down Expand Up @@ -57,6 +58,9 @@ public function __construct($appName, IL10N $l, ILogger $logger) {
* @return JSONResponse
*/
public function afterException($controller, $methodName, \Exception $exception) {
if (($controller instanceof SettingsController) === false) {
throw $exception;
}
$this->logger->error($exception->getMessage(), ['app' => $this->appName]);
if ($exception instanceof HintException) {
$message = $exception->getHint();
Expand Down
6 changes: 3 additions & 3 deletions apps/federation/tests/Middleware/AddServerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


use OC\HintException;
use OCA\Federation\Controller\SettingsController;
use OCA\Federation\Middleware\AddServerMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\IL10N;
use OCP\ILogger;
Expand All @@ -44,15 +44,15 @@ class AddServerMiddlewareTest extends TestCase {
/** @var AddServerMiddleware */
private $middleware;

/** @var \PHPUnit_Framework_MockObject_MockObject | Controller */
/** @var \PHPUnit_Framework_MockObject_MockObject | SettingsController */
private $controller;

public function setUp() {
parent::setUp();

$this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->controller = $this->getMockBuilder(Controller::class)
$this->controller = $this->getMockBuilder(SettingsController::class)
->disableOriginalConstructor()->getMock();

$this->middleware = new AddServerMiddleware(
Expand Down