Skip to content

Commit bf2873e

Browse files
committed
Allow to disable system addressbook sync
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 <[email protected]> Fix and add tests Signed-off-by: Thomas Citharel <[email protected]> Change settings template according to #13796 Signed-off-by: Thomas Citharel <[email protected]> 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 <[email protected]> Fix tests Signed-off-by: Thomas Citharel <[email protected]>
1 parent 4af583e commit bf2873e

File tree

14 files changed

+377
-15
lines changed

14 files changed

+377
-15
lines changed

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
<settings>
5454
<admin>OCA\DAV\Settings\CalDAVSettings</admin>
55+
<admin>OCA\DAV\Settings\CardDAVSettings</admin>
5556
</settings>
5657

5758
<activity>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
'OCA\\DAV\\RootCollection' => $baseDir . '/../lib/RootCollection.php',
183183
'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php',
184184
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
185+
'OCA\\DAV\\Settings\\CardDAVSettings' => $baseDir . '/../lib/Settings/CardDAVSettings.php',
185186
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => $baseDir . '/../lib/SystemTag/SystemTagMappingNode.php',
186187
'OCA\\DAV\\SystemTag\\SystemTagNode' => $baseDir . '/../lib/SystemTag/SystemTagNode.php',
187188
'OCA\\DAV\\SystemTag\\SystemTagPlugin' => $baseDir . '/../lib/SystemTag/SystemTagPlugin.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class ComposerStaticInitDAV
197197
'OCA\\DAV\\RootCollection' => __DIR__ . '/..' . '/../lib/RootCollection.php',
198198
'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php',
199199
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
200+
'OCA\\DAV\\Settings\\CardDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CardDAVSettings.php',
200201
'OCA\\DAV\\SystemTag\\SystemTagMappingNode' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagMappingNode.php',
201202
'OCA\\DAV\\SystemTag\\SystemTagNode' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagNode.php',
202203
'OCA\\DAV\\SystemTag\\SystemTagPlugin' => __DIR__ . '/..' . '/../lib/SystemTag/SystemTagPlugin.php',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @copyright 2019, Thomas Citharel <[email protected]>
3+
*
4+
* @author Thomas Citharel <[email protected]>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
'use strict';
23+
24+
$('#carddavSyncSystemAddressbook').change(function () {
25+
var val = $(this)[0].checked;
26+
27+
OCP.AppConfig.setValue('dav', 'syncSystemAddressbook', val ? 'yes' : 'no');
28+
});

apps/dav/lib/CardDAV/CardDavBackend.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,30 @@ function deleteAddressBook($addressBookId) {
470470
$query->delete($this->dbCardsPropertiesTable)
471471
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
472472
->execute();
473+
}
474+
475+
/**
476+
* Delete all the content from an addressbook
477+
*
478+
* @param mixed $addressBookId
479+
* @return void
480+
*/
481+
public function emptyAddressBook($addressBookId): void
482+
{
483+
$query = $this->db->getQueryBuilder();
484+
$query->delete('cards')
485+
->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid')))
486+
->setParameter('addressbookid', $addressBookId)
487+
->execute();
488+
489+
$query->delete('addressbookchanges')
490+
->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid')))
491+
->setParameter('addressbookid', $addressBookId)
492+
->execute();
473493

494+
$query->delete($this->dbCardsPropertiesTable)
495+
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
496+
->execute();
474497
}
475498

476499
/**

apps/dav/lib/CardDAV/ContactsManager.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OCA\DAV\CardDAV;
2727

2828
use OCP\Contacts\IManager;
29+
use OCP\IConfig;
2930
use OCP\IL10N;
3031
use OCP\IURLGenerator;
3132

@@ -36,15 +37,20 @@ class ContactsManager {
3637
/** @var IL10N */
3738
private $l10n;
3839

40+
/** @var IConfig */
41+
private $config;
42+
3943
/**
4044
* ContactsManager constructor.
4145
*
4246
* @param CardDavBackend $backend
4347
* @param IL10N $l10n
48+
* @param IConfig $config
4449
*/
45-
public function __construct(CardDavBackend $backend, IL10N $l10n) {
50+
public function __construct(CardDavBackend $backend, IL10N $l10n, IConfig $config) {
4651
$this->backend = $backend;
4752
$this->l10n = $l10n;
53+
$this->config = $config;
4854
}
4955

5056
/**
@@ -55,7 +61,9 @@ public function __construct(CardDavBackend $backend, IL10N $l10n) {
5561
public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlGenerator) {
5662
$addressBooks = $this->backend->getAddressBooksForUser("principals/users/$userId");
5763
$this->register($cm, $addressBooks, $urlGenerator);
58-
$this->setupSystemContactsProvider($cm, $urlGenerator);
64+
if ($this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes') === 'yes') {
65+
$this->setupSystemContactsProvider($cm, $urlGenerator);
66+
}
5967
}
6068

6169
/**

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,13 @@ public function syncInstance(\Closure $progressCallback = null) {
338338
}
339339
}
340340

341-
341+
/**
342+
* Delete the content of the system addressbook and the addressbook itself since it will be regenerated anyway
343+
* @return void
344+
*/
345+
public function purgeSystemAddressBook(): void
346+
{
347+
$systemAddressBook = $this->getLocalSystemAddressBook();
348+
$this->backend->emptyAddressBook($systemAddressBook['id']);
349+
}
342350
}

apps/dav/lib/Command/SyncSystemAddressBook.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace OCA\DAV\Command;
2323

2424
use OCA\DAV\CardDAV\SyncService;
25+
use OCP\IConfig;
2526
use Symfony\Component\Console\Command\Command;
2627
use Symfony\Component\Console\Helper\ProgressBar;
2728
use Symfony\Component\Console\Input\InputInterface;
@@ -32,12 +33,17 @@ class SyncSystemAddressBook extends Command {
3233
/** @var SyncService */
3334
private $syncService;
3435

36+
/** @var IConfig */
37+
private $config;
38+
3539
/**
3640
* @param SyncService $syncService
41+
* @param IConfig $config
3742
*/
38-
function __construct(SyncService $syncService) {
43+
function __construct(SyncService $syncService, IConfig $config) {
3944
parent::__construct();
4045
$this->syncService = $syncService;
46+
$this->config = $config;
4147
}
4248

4349
protected function configure() {
@@ -51,14 +57,19 @@ protected function configure() {
5157
* @param OutputInterface $output
5258
*/
5359
protected function execute(InputInterface $input, OutputInterface $output) {
54-
$output->writeln('Syncing users ...');
55-
$progress = new ProgressBar($output);
56-
$progress->start();
57-
$this->syncService->syncInstance(function() use ($progress) {
58-
$progress->advance();
59-
});
60+
if ($this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes') === 'yes') {
61+
$output->writeln('Syncing users ...');
62+
$progress = new ProgressBar($output);
63+
$progress->start();
64+
$this->syncService->syncInstance(function() use ($progress) {
65+
$progress->advance();
66+
});
6067

61-
$progress->finish();
62-
$output->writeln('');
68+
$progress->finish();
69+
$output->writeln('');
70+
} else {
71+
$this->syncService->purgeSystemAddressBook();
72+
$output->writeln('Syncing system addressbook is disabled. Addressbook has been removed');
73+
}
6374
}
6475
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @copyright 2019, Thomas Citharel <[email protected]>
4+
*
5+
* @author Thomas Citharel <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\DAV\Settings;
25+
26+
use OCP\AppFramework\Http\TemplateResponse;
27+
use OCP\IConfig;
28+
use OCP\Settings\ISettings;
29+
30+
class CardDAVSettings implements ISettings {
31+
32+
/** @var IConfig */
33+
private $config;
34+
35+
/**
36+
* CardDAVSettings constructor.
37+
*
38+
* @param IConfig $config
39+
*/
40+
public function __construct(IConfig $config) {
41+
$this->config = $config;
42+
}
43+
44+
/**
45+
* @return TemplateResponse
46+
*/
47+
public function getForm() {
48+
$parameters = [
49+
'sync_system_addressbook' => $this->config->getAppValue('dav', 'syncSystemAddressbook', 'yes'),
50+
];
51+
52+
return new TemplateResponse('dav', 'settings-admin-carddav', $parameters);
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getSection() {
59+
return 'groupware';
60+
}
61+
62+
/**
63+
* @return int
64+
*/
65+
public function getPriority() {
66+
return 10;
67+
}
68+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @copyright 2019, Thomas Citharel <[email protected]>
4+
*
5+
* @author Thomas Citharel <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
script('dav', [
25+
'settings-admin-carddav'
26+
]);
27+
28+
/** @var \OCP\IL10N $l */
29+
/** @var array $_ */
30+
?>
31+
<form id="CardDAV" class="section">
32+
<h2><?php p($l->t('Contacts server')); ?></h2>
33+
<p class="settings-hint">
34+
<?php print_unescaped(str_replace(
35+
[
36+
'{contactsappstoreopen}',
37+
'{contactsdocopen}',
38+
'{linkclose}',
39+
],
40+
[
41+
'<a target="_blank" href="../apps/office/contacts">',
42+
'<a target="_blank" href="' . link_to_docs('user-sync-contacts') . '" rel="noreferrer noopener">',
43+
'</a>',
44+
],
45+
$l->t('Also install the {contactsappstoreopen}Contacts app{linkclose}, or {contactsdocopen}connect your desktop & mobile for syncing ↗{linkclose}.')
46+
)); ?>
47+
</p>
48+
<p>
49+
<input type="checkbox" name="carddavSyncSystemAddressbook" id="carddavSyncSystemAddressbook" class="checkbox"
50+
<?php ($_['sync_system_addressbook'] === 'yes') ? print_unescaped('checked="checked"') : null ?>/>
51+
<label for="carddavSyncSystemAddressbook"><?php p($l->t('Sync the system addressbook')); ?></label>
52+
<br>
53+
<em><?php p($l->t('Syncs the instance users as contacts in a global system addressbook')); ?></em>
54+
</p>
55+
</form>

0 commit comments

Comments
 (0)