|
8 | 8 | * @author Joas Schilling <[email protected]> |
9 | 9 | * @author Julius Härtl <[email protected]> |
10 | 10 | * @author Roeland Jago Douma <[email protected]> |
| 11 | + * @author Anna Larch <[email protected]> |
11 | 12 | * |
12 | 13 | * @license GNU AGPL version 3 or any later version |
13 | 14 | * |
|
31 | 32 | use OCA\Federation\TrustedServers; |
32 | 33 | use OCP\Accounts\IAccountManager; |
33 | 34 | use OCP\IConfig; |
| 35 | +use OCP\IGroupManager; |
34 | 36 | use OCP\IL10N; |
35 | 37 | use OCP\IRequest; |
| 38 | +use OCP\IUser; |
| 39 | +use OCP\IUserSession; |
36 | 40 | use Sabre\CardDAV\Backend\SyncSupport; |
37 | 41 | use Sabre\CardDAV\Backend\BackendInterface; |
38 | 42 | use Sabre\CardDAV\Card; |
39 | 43 | use Sabre\DAV\Exception\Forbidden; |
40 | 44 | use Sabre\DAV\Exception\NotFound; |
| 45 | +use Sabre\DAV\ICollection; |
41 | 46 | use Sabre\VObject\Component\VCard; |
42 | 47 | use Sabre\VObject\Reader; |
| 48 | +use function array_unique; |
43 | 49 |
|
44 | 50 | class SystemAddressbook extends AddressBook { |
| 51 | + public const URI_SHARED = 'z-server-generated--system'; |
45 | 52 | /** @var IConfig */ |
46 | 53 | private $config; |
| 54 | + private IUserSession $userSession; |
47 | 55 | private ?TrustedServers $trustedServers; |
48 | 56 | private ?IRequest $request; |
| 57 | + private ?IGroupManager $groupManager; |
49 | 58 |
|
50 | | - public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n, IConfig $config, ?IRequest $request = null, ?TrustedServers $trustedServers = null) { |
| 59 | + public function __construct(BackendInterface $carddavBackend, |
| 60 | + array $addressBookInfo, |
| 61 | + IL10N $l10n, |
| 62 | + IConfig $config, |
| 63 | + IUserSession $userSession, |
| 64 | + ?IRequest $request = null, |
| 65 | + ?TrustedServers $trustedServers = null, |
| 66 | + ?IGroupManager $groupManager) { |
51 | 67 | parent::__construct($carddavBackend, $addressBookInfo, $l10n); |
52 | 68 | $this->config = $config; |
| 69 | + $this->userSession = $userSession; |
53 | 70 | $this->request = $request; |
54 | 71 | $this->trustedServers = $trustedServers; |
| 72 | + $this->groupManager = $groupManager; |
| 73 | + |
| 74 | + $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Accounts'); |
| 75 | + $this->addressBookInfo['{' . Plugin::NS_CARDDAV . '}addressbook-description'] = $l10n->t('System address book which holds all accounts'); |
55 | 76 | } |
56 | 77 |
|
57 | | - public function getChildren(): array { |
| 78 | + /** |
| 79 | + * No checkbox checked -> Show only the same user |
| 80 | + * 'Allow username autocompletion in share dialog' -> show everyone |
| 81 | + * 'Allow username autocompletion in share dialog' + 'Allow username autocompletion to users within the same groups' -> show only users in intersecting groups |
| 82 | + * 'Allow username autocompletion in share dialog' + 'Allow username autocompletion to users based on phone number integration' -> show only the same user |
| 83 | + * 'Allow username autocompletion in share dialog' + 'Allow username autocompletion to users within the same groups' + 'Allow username autocompletion to users based on phone number integration' -> show only users in intersecting groups |
| 84 | + */ |
| 85 | + public function getChildren() { |
58 | 86 | $shareEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
59 | 87 | $shareEnumerationGroup = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
60 | 88 | $shareEnumerationPhone = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; |
61 | | - if (!$shareEnumeration || $shareEnumerationGroup || $shareEnumerationPhone) { |
| 89 | + $user = $this->userSession->getUser(); |
| 90 | + if (!$user) { |
| 91 | + // Should never happen because we don't allow anonymous access |
62 | 92 | return []; |
63 | 93 | } |
| 94 | + if (!$shareEnumeration || !$shareEnumerationGroup && $shareEnumerationPhone) { |
| 95 | + $name = SyncService::getCardUri($user); |
| 96 | + try { |
| 97 | + return [parent::getChild($name)]; |
| 98 | + } catch (NotFound $e) { |
| 99 | + return []; |
| 100 | + } |
| 101 | + } |
| 102 | + if ($shareEnumerationGroup) { |
| 103 | + if ($this->groupManager === null) { |
| 104 | + // Group manager is not available, so we can't determine which data is safe |
| 105 | + return []; |
| 106 | + } |
| 107 | + $groups = $this->groupManager->getUserGroups($user); |
| 108 | + $names = []; |
| 109 | + foreach ($groups as $group) { |
| 110 | + $users = $group->getUsers(); |
| 111 | + foreach ($users as $groupUser) { |
| 112 | + if ($groupUser->getBackendClassName() === 'Guests') { |
| 113 | + continue; |
| 114 | + } |
| 115 | + $names[] = SyncService::getCardUri($groupUser); |
| 116 | + } |
| 117 | + } |
| 118 | + return parent::getMultipleChildren(array_unique($names)); |
| 119 | + } |
64 | 120 |
|
65 | | - return parent::getChildren(); |
| 121 | + $children = parent::getChildren(); |
| 122 | + return array_filter($children, function (Card $child) { |
| 123 | + // check only for URIs that begin with Guests: |
| 124 | + return strpos($child->getName(), 'Guests:') !== 0; |
| 125 | + }); |
66 | 126 | } |
67 | 127 |
|
68 | 128 | /** |
@@ -225,4 +285,15 @@ private function extractCarddata(array $obj): ?string { |
225 | 285 |
|
226 | 286 | return $vCard->serialize(); |
227 | 287 | } |
| 288 | + |
| 289 | + /** |
| 290 | + * @return mixed |
| 291 | + * @throws Forbidden |
| 292 | + */ |
| 293 | + public function delete() { |
| 294 | + if ($this->isFederation()) { |
| 295 | + parent::delete(); |
| 296 | + } |
| 297 | + throw new Forbidden(); |
| 298 | + } |
228 | 299 | } |
0 commit comments