Skip to content
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
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 @@ -206,6 +206,7 @@
'OCA\\DAV\\Comments\\EntityCollection' => $baseDir . '/../lib/Comments/EntityCollection.php',
'OCA\\DAV\\Comments\\EntityTypeCollection' => $baseDir . '/../lib/Comments/EntityTypeCollection.php',
'OCA\\DAV\\Comments\\RootCollection' => $baseDir . '/../lib/Comments/RootCollection.php',
'OCA\\DAV\\ConfigLexicon' => $baseDir . '/../lib/ConfigLexicon.php',
'OCA\\DAV\\Connector\\LegacyDAVACL' => $baseDir . '/../lib/Connector/LegacyDAVACL.php',
'OCA\\DAV\\Connector\\LegacyPublicAuth' => $baseDir . '/../lib/Connector/LegacyPublicAuth.php',
'OCA\\DAV\\Connector\\Sabre\\AnonymousOptionsPlugin' => $baseDir . '/../lib/Connector/Sabre/AnonymousOptionsPlugin.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 @@ -221,6 +221,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Comments\\EntityCollection' => __DIR__ . '/..' . '/../lib/Comments/EntityCollection.php',
'OCA\\DAV\\Comments\\EntityTypeCollection' => __DIR__ . '/..' . '/../lib/Comments/EntityTypeCollection.php',
'OCA\\DAV\\Comments\\RootCollection' => __DIR__ . '/..' . '/../lib/Comments/RootCollection.php',
'OCA\\DAV\\ConfigLexicon' => __DIR__ . '/..' . '/../lib/ConfigLexicon.php',
'OCA\\DAV\\Connector\\LegacyDAVACL' => __DIR__ . '/..' . '/../lib/Connector/LegacyDAVACL.php',
'OCA\\DAV\\Connector\\LegacyPublicAuth' => __DIR__ . '/..' . '/../lib/Connector/LegacyPublicAuth.php',
'OCA\\DAV\\Connector\\Sabre\\AnonymousOptionsPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/AnonymousOptionsPlugin.php',
Expand Down
3 changes: 3 additions & 0 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\Notification\Notifier as NotifierCardDAV;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\ConfigLexicon;
use OCA\DAV\Events\AddressBookCreatedEvent;
use OCA\DAV\Events\AddressBookDeletedEvent;
use OCA\DAV\Events\AddressBookShareUpdatedEvent;
Expand Down Expand Up @@ -228,6 +229,8 @@ public function register(IRegistrationContext $context): void {
$context->registerDeclarativeSettings(SystemAddressBookSettings::class);
$context->registerEventListener(DeclarativeSettingsGetValueEvent::class, DavAdminSettingsListener::class);
$context->registerEventListener(DeclarativeSettingsSetValueEvent::class, DavAdminSettingsListener::class);

$context->registerConfigLexicon(ConfigLexicon::class);
}

public function boot(IBootContext $context): void {
Expand Down
9 changes: 9 additions & 0 deletions apps/dav/lib/CardDAV/ContactsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/
namespace OCA\DAV\CardDAV;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\ConfigLexicon;
use OCA\DAV\Db\PropertyMapper;
use OCP\Contacts\IManager;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;

Expand All @@ -23,6 +26,7 @@ public function __construct(
private CardDavBackend $backend,
private IL10N $l10n,
private PropertyMapper $propertyMapper,
private IAppConfig $appConfig,
) {
}

Expand All @@ -43,6 +47,11 @@ public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlG
* @param IURLGenerator $urlGenerator
*/
public function setupSystemContactsProvider(IManager $cm, ?string $userId, IURLGenerator $urlGenerator) {
$systemAddressBookExposed = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED);
if (!$systemAddressBookExposed) {
return;
}

$addressBooks = $this->backend->getAddressBooksForUser('principals/system/system');
$this->register($cm, $addressBooks, $urlGenerator, $userId);
}
Expand Down
25 changes: 12 additions & 13 deletions apps/dav/lib/CardDAV/UserAddressBooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
*/
namespace OCA\DAV\CardDAV;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\AppInfo\PluginManager;
use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
use OCA\DAV\ConfigLexicon;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\QueryException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Server;
use OCP\Util;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Sabre\CardDAV\Backend;
Expand All @@ -30,11 +34,9 @@
use function array_map;

class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
/** @var IL10N */
protected $l10n;

/** @var IConfig */
protected $config;
protected IL10N $l10n;
protected IConfig $config;
protected IAppConfig $appConfig;

public function __construct(
Backend\BackendInterface $carddavBackend,
Expand All @@ -44,6 +46,10 @@ public function __construct(
private ?IGroupManager $groupManager,
) {
parent::__construct($carddavBackend, $principalUri);

$this->l10n = Util::getL10N('dav');
$this->config = Server::get(IConfig::class);
$this->appConfig = Server::get(IAppConfig::class);
}

/**
Expand All @@ -52,19 +58,12 @@ public function __construct(
* @return IAddressBook[]
*/
public function getChildren() {
if ($this->l10n === null) {
$this->l10n = \OC::$server->getL10N('dav');
}
if ($this->config === null) {
$this->config = Server::get(IConfig::class);
}

/** @var string|array $principal */
$principal = $this->principalUri;
$addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
// add the system address book
$systemAddressBook = null;
$systemAddressBookExposed = $this->config->getAppValue('dav', 'system_addressbook_exposed', 'yes') === 'yes';
$systemAddressBookExposed = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED);
if ($systemAddressBookExposed && is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) {
$systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system');
if ($systemAddressBook !== null) {
Expand Down
45 changes: 45 additions & 0 deletions apps/dav/lib/ConfigLexicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAV;

use OCP\Config\Lexicon\Entry;
use OCP\Config\Lexicon\ILexicon;
use OCP\Config\Lexicon\Strictness;
use OCP\Config\ValueType;

/**
* Config Lexicon for files_sharing.
*
* Please Add & Manage your Config Keys in that file and keep the Lexicon up to date!
*
* {@see ILexicon}
*/
class ConfigLexicon implements ILexicon {
public const SYSTEM_ADDRESSBOOK_EXPOSED = 'system_addressbook_exposed';

public function getStrictness(): Strictness {
return Strictness::NOTICE;
}

public function getAppConfigs(): array {
return [
new Entry(
self::SYSTEM_ADDRESSBOOK_EXPOSED,
ValueType::BOOL,
defaultRaw: true,
definition: 'Whether to not expose the system address book to users',
lazy: true,
),
];
}

public function getUserConfigs(): array {
return [];
}
}
9 changes: 3 additions & 6 deletions apps/dav/lib/Listener/DavAdminSettingsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\DAV\Listener;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\ConfigLexicon;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IAppConfig;
Expand Down Expand Up @@ -46,20 +47,16 @@ public function handle(Event $event): void {
}

private function handleGetValue(DeclarativeSettingsGetValueEvent $event): void {

if ($event->getFieldId() === 'system_addressbook_enabled') {
$event->setValue((int)$this->config->getValueBool('dav', 'system_addressbook_exposed', true));
$event->setValue((int)$this->config->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED));
}

}

private function handleSetValue(DeclarativeSettingsSetValueEvent $event): void {

if ($event->getFieldId() === 'system_addressbook_enabled') {
$this->config->setValueBool('dav', 'system_addressbook_exposed', (bool)$event->getValue());
$this->config->setValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED, (bool)$event->getValue());
$event->stopPropagation();
}

}

}
5 changes: 3 additions & 2 deletions apps/dav/lib/Migration/DisableSystemAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\DAV\Migration;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\ConfigLexicon;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function getName() {
*/
public function run(IOutput $output) {
// If the system address book exposure was previously set skip the repair step
if ($this->appConfig->hasAppKey('system_addressbook_exposed') === true) {
if ($this->appConfig->hasAppKey(ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED) === true) {
$output->info('Skipping repair step system address book exposed was previously set');
return;
}
Expand All @@ -50,7 +51,7 @@ public function run(IOutput $output) {
$output->info("Skipping repair step system address book has less then the threshold $limit of contacts no need to disable");
return;
}
$this->appConfig->setAppValueBool('system_addressbook_exposed', false);
$this->appConfig->setAppValueBool(ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED, false);
$output->warning("System address book disabled because it has more then the threshold of $limit contacts this can be re-enabled later");
// Notify all admin users about the system address book being disabled
foreach ($this->groupManager->get('admin')->getUsers() as $user) {
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/SetupChecks/SystemAddressBookSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\DAV\SetupChecks;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\ConfigLexicon;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IUserManager;
Expand All @@ -33,7 +34,7 @@ public function getCategory(): string {
}

public function run(): SetupResult {
if (!$this->appConfig->getValueBool(Application::APP_ID, 'system_addressbook_exposed', true)) {
if (!$this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED)) {
return SetupResult::success($this->l10n->t('The system address book is disabled'));
}

Expand Down
9 changes: 7 additions & 2 deletions apps/dav/tests/unit/CardDAV/ContactsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\Db\PropertyMapper;
use OCP\Contacts\IManager;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -21,17 +22,21 @@ class ContactsManagerTest extends TestCase {
public function test(): void {
/** @var IManager&MockObject $cm */
$cm = $this->createMock(IManager::class);
$cm->expects($this->exactly(2))->method('registerAddressBook');
$cm->expects($this->exactly(1))->method('registerAddressBook');
/** @var IURLGenerator&MockObject $urlGenerator */
$urlGenerator = $this->createMock(IURLGenerator::class);
/** @var CardDavBackend&MockObject $backEnd */
$backEnd = $this->createMock(CardDavBackend::class);
$backEnd->method('getAddressBooksForUser')->willReturn([
['{DAV:}displayname' => 'Test address book', 'uri' => 'default'],
]);
$propertyMapper = $this->createMock(PropertyMapper::class);
/** @var IAppConfig&MockObject $appConfig */
$appConfig = $this->createMock(IAppConfig::class);

/** @var IL10N&MockObject $l */
$l = $this->createMock(IL10N::class);
$app = new ContactsManager($backEnd, $l, $propertyMapper);
$app = new ContactsManager($backEnd, $l, $propertyMapper, $appConfig);
$app->setupContactsProvider($cm, 'user01', $urlGenerator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\DAV\Tests\SetupChecks;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\ConfigLexicon;
use OCA\DAV\SetupChecks\SystemAddressBookSize;
use OCP\IAppConfig;
use OCP\IL10N;
Expand All @@ -30,7 +31,7 @@ protected function setUp(): void {

public function testSystemAddressBookDisabled() {
$this->appConfig->method('getValueBool')
->with(Application::APP_ID, 'system_addressbook_exposed', true)
->with(Application::APP_ID, ConfigLexicon::SYSTEM_ADDRESSBOOK_EXPOSED)
->willReturn(false);

$check = new SystemAddressBookSize($this->appConfig, $this->userManager, $this->l10n);
Expand Down
16 changes: 16 additions & 0 deletions build/integration/features/contacts-menu.feature
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,19 @@ Feature: contacts-menu
And searching for contacts matching with "test"
# Disabled because it regularly fails on drone:
# Then the list of searched contacts has "0" contacts

Scenario: users cannot list other users from the system address book
Given user "user0" exists
And user "user1" exists
And invoking occ with "config:app:set dav system_addressbook_exposed --value false"
And Logging in using web as "user1"
And searching for contacts matching with ""
Then the list of searched contacts has "1" contacts
And invoking occ with "config:app:delete dav system_addressbook_exposed"

Scenario: users can list other users from the system address book
Given user "user0" exists
And user "user1" exists
And Logging in using web as "user1"
And searching for contacts matching with ""
Then the list of searched contacts has "2" contacts
4 changes: 0 additions & 4 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,6 @@
</DeprecatedMethod>
</file>
<file src="apps/dav/lib/CardDAV/UserAddressBooks.php">
<DeprecatedMethod>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[getL10N]]></code>
</DeprecatedMethod>
<InvalidArgument>
<code><![CDATA[$this->principalUri]]></code>
<code><![CDATA[$this->principalUri]]></code>
Expand Down
Loading