Skip to content

Commit

Permalink
fix(cardav): only show useres from enabled addressBooks in contacts menu
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <[email protected]>
  • Loading branch information
hamza221 committed Mar 10, 2025
1 parent 59cda8e commit bb667c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ private function setupSystemContactsProvider(IContactsManager $contactsManager,
/** @var ContactsManager $cm */
$cm = $container->query(ContactsManager::class);
$urlGenerator = $container->getServer()->getURLGenerator();
$cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
$user = Server::get(IUserSession::class)->getUser();
$cm->setupSystemContactsProvider($contactsManager, $user->getUID(), $urlGenerator);
}

public function registerCalendarManager(ICalendarManager $calendarManager,
Expand Down
14 changes: 14 additions & 0 deletions apps/dav/lib/CardDAV/AddressBookImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\DAV\CardDAV;

use OCA\DAV\Db\PropertyMapper;
use OCP\Constants;
use OCP\IAddressBook;
use OCP\IURLGenerator;
Expand All @@ -30,6 +31,8 @@ public function __construct(
private array $addressBookInfo,
private CardDavBackend $backend,
private IURLGenerator $urlGenerator,
private PropertyMapper $propertyMapper,
private $userId,
) {
}

Expand Down Expand Up @@ -308,4 +311,15 @@ public function isSystemAddressBook(): bool {
$this->addressBookInfo['{DAV:}displayname'] === $this->urlGenerator->getBaseUrl()
);
}

public function isEnabled(): bool {
$user = $this->isSystemAddressBook() ? $this->userId : str_replace('principals/users/', '', $this->addressBookInfo['principaluri']);
$uri = $this->isSystemAddressBook() ? 'z-server-generated--system' : $this->addressBookInfo['uri'];
$path = 'addressbooks/users/' . $user . '/' . $uri;
$properties = $this->propertyMapper->findPropertyByPathAndName($user, $path, '{http://owncloud.org/ns}enabled');
if (count($properties) > 0) {
return (bool)$properties[0]->getPropertyvalue();
}
return true;
}
}
18 changes: 12 additions & 6 deletions apps/dav/lib/CardDAV/ContactsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\DAV\CardDAV;

use OCA\DAV\Db\PropertyMapper;
use OCP\Contacts\IManager;
use OCP\IL10N;
use OCP\IURLGenerator;
Expand All @@ -21,6 +22,7 @@ class ContactsManager {
public function __construct(
private CardDavBackend $backend,
private IL10N $l10n,
private PropertyMapper $propertyMapper,
) {
}

Expand All @@ -31,33 +33,37 @@ public function __construct(
*/
public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlGenerator) {
$addressBooks = $this->backend->getAddressBooksForUser("principals/users/$userId");
$this->register($cm, $addressBooks, $urlGenerator);
$this->setupSystemContactsProvider($cm, $urlGenerator);
$this->register($cm, $addressBooks, $urlGenerator, $userId);
$this->setupSystemContactsProvider($cm, $userId, $urlGenerator);
}

/**
* @param IManager $cm
* @param string $userId
* @param IURLGenerator $urlGenerator
*/
public function setupSystemContactsProvider(IManager $cm, IURLGenerator $urlGenerator) {
public function setupSystemContactsProvider(IManager $cm, string $userId, IURLGenerator $urlGenerator) {
$addressBooks = $this->backend->getAddressBooksForUser('principals/system/system');
$this->register($cm, $addressBooks, $urlGenerator);
$this->register($cm, $addressBooks, $urlGenerator, $userId);
}

/**
* @param IManager $cm
* @param $addressBooks
* @param IURLGenerator $urlGenerator
* @param string $userId
*/
private function register(IManager $cm, $addressBooks, $urlGenerator) {
private function register(IManager $cm, $addressBooks, $urlGenerator, string $userId) {
foreach ($addressBooks as $addressBookInfo) {
$addressBook = new AddressBook($this->backend, $addressBookInfo, $this->l10n);
$cm->registerAddressBook(
new AddressBookImpl(
$addressBook,
$addressBookInfo,
$this->backend,
$urlGenerator
$urlGenerator,
$this->propertyMapper,
$userId,
)
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/private/ContactsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function search($pattern, $searchProperties = [], $options = []) {
$this->loadAddressBooks();
$result = [];
foreach ($this->addressBooks as $addressBook) {
if (!$addressBook->isEnabled()) {
continue;
}
$searchOptions = $options;
$strictSearch = array_key_exists('strict_search', $options) && $options['strict_search'] === true;

Expand Down
2 changes: 2 additions & 0 deletions lib/public/IAddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ public function isShared(): bool;
* @since 20.0.0
*/
public function isSystemAddressBook(): bool;

public function isEnabled(): bool;
}
}

0 comments on commit bb667c0

Please sign in to comment.