|
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 | + * Returns a list of properties for this nodes. |
| 80 | + * |
| 81 | + * The properties list is a list of propertynames the client requested, |
| 82 | + * encoded in clark-notation {xmlnamespace}tagname |
| 83 | + * |
| 84 | + * If the array is empty, it means 'all properties' were requested. |
| 85 | + * |
| 86 | + * @param array $properties |
| 87 | + * |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + public function getProperties($properties) { |
| 91 | + $response = []; |
| 92 | + foreach ($properties as $propertyName) { |
| 93 | + if (isset($this->addressBookInfo[$propertyName])) { |
| 94 | + $response[$propertyName] = $this->addressBookInfo[$propertyName]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $response; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * No checkbox checked -> Show only the same user |
| 103 | + * 'Allow username autocompletion in share dialog' -> show everyone |
| 104 | + * 'Allow username autocompletion in share dialog' + 'Allow username autocompletion to users within the same groups' -> show only users in intersecting groups |
| 105 | + * 'Allow username autocompletion in share dialog' + 'Allow username autocompletion to users based on phone number integration' -> show only the same user |
| 106 | + * '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 |
| 107 | + */ |
| 108 | + public function getChildren() { |
58 | 109 | $shareEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
59 | 110 | $shareEnumerationGroup = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
60 | 111 | $shareEnumerationPhone = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; |
61 | | - if (!$shareEnumeration || $shareEnumerationGroup || $shareEnumerationPhone) { |
| 112 | + $user = $this->userSession->getUser(); |
| 113 | + if (!$user) { |
| 114 | + // Should never happen because we don't allow anonymous access |
62 | 115 | return []; |
63 | 116 | } |
| 117 | + if (!$shareEnumeration || !$shareEnumerationGroup && $shareEnumerationPhone) { |
| 118 | + $name = SyncService::getCardUri($user); |
| 119 | + try { |
| 120 | + return [parent::getChild($name)]; |
| 121 | + } catch (NotFound $e) { |
| 122 | + return []; |
| 123 | + } |
| 124 | + } |
| 125 | + if ($shareEnumerationGroup) { |
| 126 | + if ($this->groupManager === null) { |
| 127 | + // Group manager is not available, so we can't determine which data is safe |
| 128 | + return []; |
| 129 | + } |
| 130 | + $groups = $this->groupManager->getUserGroups($user); |
| 131 | + $names = []; |
| 132 | + foreach ($groups as $group) { |
| 133 | + $users = $group->getUsers(); |
| 134 | + foreach ($users as $groupUser) { |
| 135 | + if ($groupUser->getBackendClassName() === 'Guests') { |
| 136 | + continue; |
| 137 | + } |
| 138 | + $names[] = SyncService::getCardUri($groupUser); |
| 139 | + } |
| 140 | + } |
| 141 | + return parent::getMultipleChildren(array_unique($names)); |
| 142 | + } |
64 | 143 |
|
65 | | - return parent::getChildren(); |
| 144 | + $children = parent::getChildren(); |
| 145 | + return array_filter($children, function (Card $child) { |
| 146 | + // check only for URIs that begin with Guests: |
| 147 | + return strpos($child->getName(), 'Guests:') !== 0; |
| 148 | + }); |
66 | 149 | } |
67 | 150 |
|
68 | 151 | /** |
|
0 commit comments