Skip to content

Commit

Permalink
Allow to disable system addressbook sync
Browse files Browse the repository at this point in the history
On some instances, users don't know each other and it doesn't makes
sense to expose users as contacts (through Contacts Menu, Calendar
Attendees,…) to everyone.

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix and add tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Change settings template according to #13796

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Empty system addressbook instead of removing it

This makes Federated Cloud Sharing not fail when synching trusted
servers addressbooks (only synching an empty one, not bumping on an
non-existent one).

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Fix tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Feb 28, 2019
1 parent 4af583e commit bf2873e
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 15 deletions.
1 change: 1 addition & 0 deletions apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<settings>
<admin>OCA\DAV\Settings\CalDAVSettings</admin>
<admin>OCA\DAV\Settings\CardDAVSettings</admin>
</settings>

<activity>
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
'OCA\\DAV\\RootCollection' => $baseDir . '/../lib/RootCollection.php',
'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
'OCA\\DAV\\Settings\\CardDAVSettings' => $baseDir . '/../lib/Settings/CardDAVSettings.php',
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => $baseDir . '/../lib/SystemTag/SystemTagMappingNode.php',
'OCA\\DAV\\SystemTag\\SystemTagNode' => $baseDir . '/../lib/SystemTag/SystemTagNode.php',
'OCA\\DAV\\SystemTag\\SystemTagPlugin' => $baseDir . '/../lib/SystemTag/SystemTagPlugin.php',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\RootCollection' => __DIR__ . '/..' . '/../lib/RootCollection.php',
'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
'OCA\\DAV\\Settings\\CardDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CardDAVSettings.php',
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagMappingNode.php',
'OCA\\DAV\\SystemTag\\SystemTagNode' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagNode.php',
'OCA\\DAV\\SystemTag\\SystemTagPlugin' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagPlugin.php',
Expand Down
28 changes: 28 additions & 0 deletions apps/dav/js/settings-admin-carddav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @copyright 2019, Thomas Citharel <tcit@tcit.fr>
*
* @author Thomas Citharel <tcit@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
'use strict';

$('#carddavSyncSystemAddressbook').change(function () {
var val = $(this)[0].checked;

OCP.AppConfig.setValue('dav', 'syncSystemAddressbook', val ? 'yes' : 'no');
});
23 changes: 23 additions & 0 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,30 @@ function deleteAddressBook($addressBookId) {
$query->delete($this->dbCardsPropertiesTable)
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
->execute();
}

/**
* Delete all the content from an addressbook
*
* @param mixed $addressBookId
* @return void
*/
public function emptyAddressBook($addressBookId): void
{
$query = $this->db->getQueryBuilder();
$query->delete('cards')
->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid')))
->setParameter('addressbookid', $addressBookId)
->execute();

$query->delete('addressbookchanges')
->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid')))
->setParameter('addressbookid', $addressBookId)
->execute();

$query->delete($this->dbCardsPropertiesTable)
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
->execute();
}

/**
Expand Down
12 changes: 10 additions & 2 deletions apps/dav/lib/CardDAV/ContactsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\DAV\CardDAV;

use OCP\Contacts\IManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;

Expand All @@ -36,15 +37,20 @@ class ContactsManager {
/** @var IL10N */
private $l10n;

/** @var IConfig */
private $config;

/**
* ContactsManager constructor.
*
* @param CardDavBackend $backend
* @param IL10N $l10n
* @param IConfig $config
*/
public function __construct(CardDavBackend $backend, IL10N $l10n) {
public function __construct(CardDavBackend $backend, IL10N $l10n, IConfig $config) {
$this->backend = $backend;
$this->l10n = $l10n;
$this->config = $config;
}

/**
Expand All @@ -55,7 +61,9 @@ public function __construct(CardDavBackend $backend, IL10N $l10n) {
public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlGenerator) {
$addressBooks = $this->backend->getAddressBooksForUser("principals/users/$userId");
$this->register($cm, $addressBooks, $urlGenerator);
$this->setupSystemContactsProvider($cm, $urlGenerator);
if ($this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes') === 'yes') {
$this->setupSystemContactsProvider($cm, $urlGenerator);
}
}

/**
Expand Down
10 changes: 9 additions & 1 deletion apps/dav/lib/CardDAV/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,13 @@ public function syncInstance(\Closure $progressCallback = null) {
}
}


/**
* Delete the content of the system addressbook and the addressbook itself since it will be regenerated anyway
* @return void
*/
public function purgeSystemAddressBook(): void
{
$systemAddressBook = $this->getLocalSystemAddressBook();
$this->backend->emptyAddressBook($systemAddressBook['id']);
}
}
29 changes: 20 additions & 9 deletions apps/dav/lib/Command/SyncSystemAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace OCA\DAV\Command;

use OCA\DAV\CardDAV\SyncService;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,12 +33,17 @@ class SyncSystemAddressBook extends Command {
/** @var SyncService */
private $syncService;

/** @var IConfig */
private $config;

/**
* @param SyncService $syncService
* @param IConfig $config
*/
function __construct(SyncService $syncService) {
function __construct(SyncService $syncService, IConfig $config) {
parent::__construct();
$this->syncService = $syncService;
$this->config = $config;
}

protected function configure() {
Expand All @@ -51,14 +57,19 @@ protected function configure() {
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln('Syncing users ...');
$progress = new ProgressBar($output);
$progress->start();
$this->syncService->syncInstance(function() use ($progress) {
$progress->advance();
});
if ($this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes') === 'yes') {
$output->writeln('Syncing users ...');
$progress = new ProgressBar($output);
$progress->start();
$this->syncService->syncInstance(function() use ($progress) {
$progress->advance();
});

$progress->finish();
$output->writeln('');
$progress->finish();
$output->writeln('');
} else {
$this->syncService->purgeSystemAddressBook();
$output->writeln('Syncing system addressbook is disabled. Addressbook has been removed');
}
}
}
68 changes: 68 additions & 0 deletions apps/dav/lib/Settings/CardDAVSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @copyright 2019, Thomas Citharel <tcit@tcit.fr>
*
* @author Thomas Citharel <tcit@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\DAV\Settings;

use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use OCP\Settings\ISettings;

class CardDAVSettings implements ISettings {

/** @var IConfig */
private $config;

/**
* CardDAVSettings constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
}

/**
* @return TemplateResponse
*/
public function getForm() {
$parameters = [
'sync_system_addressbook' => $this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes'),
];

return new TemplateResponse('dav', 'settings-admin-carddav', $parameters);
}

/**
* @return string
*/
public function getSection() {
return 'groupware';
}

/**
* @return int
*/
public function getPriority() {
return 10;
}
}
55 changes: 55 additions & 0 deletions apps/dav/templates/settings-admin-carddav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @copyright 2019, Thomas Citharel <tcit@tcit.fr>
*
* @author Thomas Citharel <tcit@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

script('dav', [
'settings-admin-carddav'
]);

/** @var \OCP\IL10N $l */
/** @var array $_ */
?>
<form id="CardDAV" class="section">
<h2><?php p($l->t('Contacts server')); ?></h2>
<p class="settings-hint">
<?php print_unescaped(str_replace(
[
'{contactsappstoreopen}',
'{contactsdocopen}',
'{linkclose}',
],
[
'<a target="_blank" href="../apps/office/contacts">',
'<a target="_blank" href="' . link_to_docs('user-sync-contacts') . '" rel="noreferrer noopener">',
'</a>',
],
$l->t('Also install the {contactsappstoreopen}Contacts app{linkclose}, or {contactsdocopen}connect your desktop & mobile for syncing ↗{linkclose}.')
)); ?>
</p>
<p>
<input type="checkbox" name="carddavSyncSystemAddressbook" id="carddavSyncSystemAddressbook" class="checkbox"
<?php ($_['sync_system_addressbook'] === 'yes') ? print_unescaped('checked="checked"') : null ?>/>
<label for="carddavSyncSystemAddressbook"><?php p($l->t('Sync the system addressbook')); ?></label>
<br>
<em><?php p($l->t('Syncs the instance users as contacts in a global system addressbook')); ?></em>
</p>
</form>
22 changes: 19 additions & 3 deletions apps/dav/tests/unit/CardDAV/ContactsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCP\Contacts\IManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use Test\TestCase;

class ContactsManagerTest extends TestCase {
public function test() {

public function dataForContactsManagerTest() {
return [
['yes'],
['no']
];
}

/**
* @dataProvider dataForContactsManagerTest
*/
public function test(string $syncSystemAddressBook) {
/** @var IManager | \PHPUnit_Framework_MockObject_MockObject $cm */
$cm = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
$cm->expects($this->exactly(2))->method('registerAddressBook');
$cm->expects($this->exactly($syncSystemAddressBook === 'yes' ? 2 : 1))->method('registerAddressBook');
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)->disableOriginalConstructor()->getMock();
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backEnd */
$backEnd = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
Expand All @@ -45,7 +57,11 @@ public function test() {
]);

$l = $this->createMock(IL10N::class);
$app = new ContactsManager($backEnd, $l);
$config = $this->createMock(IConfig::class);

$config->method('getAppValue')->with('dav', 'syncSystemAddressbook', 'yes')->willReturn($syncSystemAddressBook);

$app = new ContactsManager($backEnd, $l, $config);
$app->setupContactsProvider($cm, 'user01', $urlGenerator);
}
}
13 changes: 13 additions & 0 deletions apps/dav/tests/unit/CardDAV/SyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public function testUpdateAndDeleteUser($activated, $createCalls, $updateCalls,
$ss->deleteUser($user);
}

public function testPurgeSystemAddressBook()
{
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
$userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
$accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();

$backend->expects($this->once())->method('emptyAddressBook');

$ss = new SyncService($backend, $userManager, $logger, $accountManager);
$ss->purgeSystemAddressBook();
}

/**
* @param int $createCount
* @param int $updateCount
Expand Down
Loading

0 comments on commit bf2873e

Please sign in to comment.