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>
  • Loading branch information
tcitworld committed Jan 27, 2019
1 parent 996bad6 commit e114413
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 12 deletions.
1 change: 1 addition & 0 deletions apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -176,6 +176,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 @@ -191,6 +191,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');
});
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->deleteAddressBook($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;
}
}
40 changes: 40 additions & 0 deletions apps/dav/templates/settings-admin-carddav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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>
<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>

0 comments on commit e114413

Please sign in to comment.