@@ -37,221 +37,221 @@ |
||
| 37 | 37 | |
| 38 | 38 | class ContactsStore implements IContactsStore { |
| 39 | 39 | |
| 40 | - /** @var IManager */ |
|
| 41 | - private $contactsManager; |
|
| 42 | - |
|
| 43 | - /** @var IConfig */ |
|
| 44 | - private $config; |
|
| 45 | - |
|
| 46 | - /** @var IUserManager */ |
|
| 47 | - private $userManager; |
|
| 48 | - |
|
| 49 | - /** @var IGroupManager */ |
|
| 50 | - private $groupManager; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param IManager $contactsManager |
|
| 54 | - * @param IConfig $config |
|
| 55 | - * @param IUserManager $userManager |
|
| 56 | - * @param IGroupManager $groupManager |
|
| 57 | - */ |
|
| 58 | - public function __construct(IManager $contactsManager, |
|
| 59 | - IConfig $config, |
|
| 60 | - IUserManager $userManager, |
|
| 61 | - IGroupManager $groupManager) { |
|
| 62 | - $this->contactsManager = $contactsManager; |
|
| 63 | - $this->config = $config; |
|
| 64 | - $this->userManager = $userManager; |
|
| 65 | - $this->groupManager = $groupManager; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param IUser $user |
|
| 70 | - * @param string|null $filter |
|
| 71 | - * @return IEntry[] |
|
| 72 | - */ |
|
| 73 | - public function getContacts(IUser $user, $filter) { |
|
| 74 | - $allContacts = $this->contactsManager->search($filter ?: '', [ |
|
| 75 | - 'FN', |
|
| 76 | - 'EMAIL' |
|
| 77 | - ]); |
|
| 78 | - |
|
| 79 | - $entries = array_map(function(array $contact) { |
|
| 80 | - return $this->contactArrayToEntry($contact); |
|
| 81 | - }, $allContacts); |
|
| 82 | - return $this->filterContacts( |
|
| 83 | - $user, |
|
| 84 | - $entries, |
|
| 85 | - $filter |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Filters the contacts. Applies 3 filters: |
|
| 91 | - * 1. filter the current user |
|
| 92 | - * 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is |
|
| 93 | - * enabled it will filter all local users |
|
| 94 | - * 3. if the `shareapi_exclude_groups` config option is enabled and the |
|
| 95 | - * current user is in an excluded group it will filter all local users. |
|
| 96 | - * 4. if the `shareapi_only_share_with_group_members` config option is |
|
| 97 | - * enabled it will filter all users which doens't have a common group |
|
| 98 | - * with the current user. |
|
| 99 | - * |
|
| 100 | - * @param IUser $self |
|
| 101 | - * @param Entry[] $entries |
|
| 102 | - * @param string $filter |
|
| 103 | - * @return Entry[] the filtered contacts |
|
| 104 | - */ |
|
| 105 | - private function filterContacts(IUser $self, |
|
| 106 | - array $entries, |
|
| 107 | - $filter) { |
|
| 108 | - $disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes'; |
|
| 109 | - $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes'; |
|
| 110 | - |
|
| 111 | - // whether to filter out local users |
|
| 112 | - $skipLocal = false; |
|
| 113 | - // whether to filter out all users which doesn't have the same group as the current user |
|
| 114 | - $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 115 | - |
|
| 116 | - $selfGroups = $this->groupManager->getUserGroupIds($self); |
|
| 117 | - |
|
| 118 | - if ($excludedGroups) { |
|
| 119 | - $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 120 | - $decodedExcludeGroups = json_decode($excludedGroups, true); |
|
| 121 | - $excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups : []; |
|
| 122 | - |
|
| 123 | - if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) { |
|
| 124 | - // a group of the current user is excluded -> filter all local users |
|
| 125 | - $skipLocal = true; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $selfUID = $self->getUID(); |
|
| 130 | - |
|
| 131 | - return array_values(array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) { |
|
| 132 | - if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) { |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Prevent enumerating local users |
|
| 137 | - if($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) { |
|
| 138 | - $filterUser = true; |
|
| 139 | - |
|
| 140 | - $mailAddresses = $entry->getEMailAddresses(); |
|
| 141 | - foreach($mailAddresses as $mailAddress) { |
|
| 142 | - if($mailAddress === $filter) { |
|
| 143 | - $filterUser = false; |
|
| 144 | - break; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) { |
|
| 149 | - $filterUser = false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if($filterUser) { |
|
| 153 | - return false; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) { |
|
| 158 | - $contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID'))); |
|
| 159 | - if (count(array_intersect($contactGroups, $selfGroups)) === 0) { |
|
| 160 | - // no groups in common, so shouldn't see the contact |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $entry->getProperty('UID') !== $selfUID; |
|
| 166 | - })); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param IUser $user |
|
| 171 | - * @param integer $shareType |
|
| 172 | - * @param string $shareWith |
|
| 173 | - * @return IEntry|null |
|
| 174 | - */ |
|
| 175 | - public function findOne(IUser $user, $shareType, $shareWith) { |
|
| 176 | - switch($shareType) { |
|
| 177 | - case 0: |
|
| 178 | - case 6: |
|
| 179 | - $filter = ['UID']; |
|
| 180 | - break; |
|
| 181 | - case 4: |
|
| 182 | - $filter = ['EMAIL']; |
|
| 183 | - break; |
|
| 184 | - default: |
|
| 185 | - return null; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $userId = $user->getUID(); |
|
| 189 | - $allContacts = $this->contactsManager->search($shareWith, $filter); |
|
| 190 | - $contacts = array_filter($allContacts, function($contact) use ($userId) { |
|
| 191 | - return $contact['UID'] !== $userId; |
|
| 192 | - }); |
|
| 193 | - $match = null; |
|
| 194 | - |
|
| 195 | - foreach ($contacts as $contact) { |
|
| 196 | - if ($shareType === 4 && isset($contact['EMAIL'])) { |
|
| 197 | - if (in_array($shareWith, $contact['EMAIL'])) { |
|
| 198 | - $match = $contact; |
|
| 199 | - break; |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - if ($shareType === 0 || $shareType === 6) { |
|
| 203 | - $isLocal = $contact['isLocalSystemBook'] ?? false; |
|
| 204 | - if ($contact['UID'] === $shareWith && $isLocal === true) { |
|
| 205 | - $match = $contact; |
|
| 206 | - break; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($match) { |
|
| 212 | - $match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith); |
|
| 213 | - if (count($match) === 1) { |
|
| 214 | - $match = $match[0]; |
|
| 215 | - } else { |
|
| 216 | - $match = null; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - return $match; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @param array $contact |
|
| 226 | - * @return Entry |
|
| 227 | - */ |
|
| 228 | - private function contactArrayToEntry(array $contact) { |
|
| 229 | - $entry = new Entry(); |
|
| 230 | - |
|
| 231 | - if (isset($contact['id'])) { |
|
| 232 | - $entry->setId($contact['id']); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - if (isset($contact['FN'])) { |
|
| 236 | - $entry->setFullName($contact['FN']); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $avatarPrefix = "VALUE=uri:"; |
|
| 240 | - if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) { |
|
| 241 | - $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix))); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - if (isset($contact['EMAIL'])) { |
|
| 245 | - foreach ($contact['EMAIL'] as $email) { |
|
| 246 | - $entry->addEMailAddress($email); |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - // Attach all other properties to the entry too because some |
|
| 251 | - // providers might make use of it. |
|
| 252 | - $entry->setProperties($contact); |
|
| 253 | - |
|
| 254 | - return $entry; |
|
| 255 | - } |
|
| 40 | + /** @var IManager */ |
|
| 41 | + private $contactsManager; |
|
| 42 | + |
|
| 43 | + /** @var IConfig */ |
|
| 44 | + private $config; |
|
| 45 | + |
|
| 46 | + /** @var IUserManager */ |
|
| 47 | + private $userManager; |
|
| 48 | + |
|
| 49 | + /** @var IGroupManager */ |
|
| 50 | + private $groupManager; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param IManager $contactsManager |
|
| 54 | + * @param IConfig $config |
|
| 55 | + * @param IUserManager $userManager |
|
| 56 | + * @param IGroupManager $groupManager |
|
| 57 | + */ |
|
| 58 | + public function __construct(IManager $contactsManager, |
|
| 59 | + IConfig $config, |
|
| 60 | + IUserManager $userManager, |
|
| 61 | + IGroupManager $groupManager) { |
|
| 62 | + $this->contactsManager = $contactsManager; |
|
| 63 | + $this->config = $config; |
|
| 64 | + $this->userManager = $userManager; |
|
| 65 | + $this->groupManager = $groupManager; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param IUser $user |
|
| 70 | + * @param string|null $filter |
|
| 71 | + * @return IEntry[] |
|
| 72 | + */ |
|
| 73 | + public function getContacts(IUser $user, $filter) { |
|
| 74 | + $allContacts = $this->contactsManager->search($filter ?: '', [ |
|
| 75 | + 'FN', |
|
| 76 | + 'EMAIL' |
|
| 77 | + ]); |
|
| 78 | + |
|
| 79 | + $entries = array_map(function(array $contact) { |
|
| 80 | + return $this->contactArrayToEntry($contact); |
|
| 81 | + }, $allContacts); |
|
| 82 | + return $this->filterContacts( |
|
| 83 | + $user, |
|
| 84 | + $entries, |
|
| 85 | + $filter |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Filters the contacts. Applies 3 filters: |
|
| 91 | + * 1. filter the current user |
|
| 92 | + * 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is |
|
| 93 | + * enabled it will filter all local users |
|
| 94 | + * 3. if the `shareapi_exclude_groups` config option is enabled and the |
|
| 95 | + * current user is in an excluded group it will filter all local users. |
|
| 96 | + * 4. if the `shareapi_only_share_with_group_members` config option is |
|
| 97 | + * enabled it will filter all users which doens't have a common group |
|
| 98 | + * with the current user. |
|
| 99 | + * |
|
| 100 | + * @param IUser $self |
|
| 101 | + * @param Entry[] $entries |
|
| 102 | + * @param string $filter |
|
| 103 | + * @return Entry[] the filtered contacts |
|
| 104 | + */ |
|
| 105 | + private function filterContacts(IUser $self, |
|
| 106 | + array $entries, |
|
| 107 | + $filter) { |
|
| 108 | + $disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes'; |
|
| 109 | + $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes'; |
|
| 110 | + |
|
| 111 | + // whether to filter out local users |
|
| 112 | + $skipLocal = false; |
|
| 113 | + // whether to filter out all users which doesn't have the same group as the current user |
|
| 114 | + $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 115 | + |
|
| 116 | + $selfGroups = $this->groupManager->getUserGroupIds($self); |
|
| 117 | + |
|
| 118 | + if ($excludedGroups) { |
|
| 119 | + $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
| 120 | + $decodedExcludeGroups = json_decode($excludedGroups, true); |
|
| 121 | + $excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups : []; |
|
| 122 | + |
|
| 123 | + if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) { |
|
| 124 | + // a group of the current user is excluded -> filter all local users |
|
| 125 | + $skipLocal = true; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $selfUID = $self->getUID(); |
|
| 130 | + |
|
| 131 | + return array_values(array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) { |
|
| 132 | + if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) { |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Prevent enumerating local users |
|
| 137 | + if($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) { |
|
| 138 | + $filterUser = true; |
|
| 139 | + |
|
| 140 | + $mailAddresses = $entry->getEMailAddresses(); |
|
| 141 | + foreach($mailAddresses as $mailAddress) { |
|
| 142 | + if($mailAddress === $filter) { |
|
| 143 | + $filterUser = false; |
|
| 144 | + break; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) { |
|
| 149 | + $filterUser = false; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if($filterUser) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) { |
|
| 158 | + $contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID'))); |
|
| 159 | + if (count(array_intersect($contactGroups, $selfGroups)) === 0) { |
|
| 160 | + // no groups in common, so shouldn't see the contact |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $entry->getProperty('UID') !== $selfUID; |
|
| 166 | + })); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param IUser $user |
|
| 171 | + * @param integer $shareType |
|
| 172 | + * @param string $shareWith |
|
| 173 | + * @return IEntry|null |
|
| 174 | + */ |
|
| 175 | + public function findOne(IUser $user, $shareType, $shareWith) { |
|
| 176 | + switch($shareType) { |
|
| 177 | + case 0: |
|
| 178 | + case 6: |
|
| 179 | + $filter = ['UID']; |
|
| 180 | + break; |
|
| 181 | + case 4: |
|
| 182 | + $filter = ['EMAIL']; |
|
| 183 | + break; |
|
| 184 | + default: |
|
| 185 | + return null; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $userId = $user->getUID(); |
|
| 189 | + $allContacts = $this->contactsManager->search($shareWith, $filter); |
|
| 190 | + $contacts = array_filter($allContacts, function($contact) use ($userId) { |
|
| 191 | + return $contact['UID'] !== $userId; |
|
| 192 | + }); |
|
| 193 | + $match = null; |
|
| 194 | + |
|
| 195 | + foreach ($contacts as $contact) { |
|
| 196 | + if ($shareType === 4 && isset($contact['EMAIL'])) { |
|
| 197 | + if (in_array($shareWith, $contact['EMAIL'])) { |
|
| 198 | + $match = $contact; |
|
| 199 | + break; |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + if ($shareType === 0 || $shareType === 6) { |
|
| 203 | + $isLocal = $contact['isLocalSystemBook'] ?? false; |
|
| 204 | + if ($contact['UID'] === $shareWith && $isLocal === true) { |
|
| 205 | + $match = $contact; |
|
| 206 | + break; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($match) { |
|
| 212 | + $match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith); |
|
| 213 | + if (count($match) === 1) { |
|
| 214 | + $match = $match[0]; |
|
| 215 | + } else { |
|
| 216 | + $match = null; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + return $match; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @param array $contact |
|
| 226 | + * @return Entry |
|
| 227 | + */ |
|
| 228 | + private function contactArrayToEntry(array $contact) { |
|
| 229 | + $entry = new Entry(); |
|
| 230 | + |
|
| 231 | + if (isset($contact['id'])) { |
|
| 232 | + $entry->setId($contact['id']); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + if (isset($contact['FN'])) { |
|
| 236 | + $entry->setFullName($contact['FN']); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $avatarPrefix = "VALUE=uri:"; |
|
| 240 | + if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) { |
|
| 241 | + $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix))); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + if (isset($contact['EMAIL'])) { |
|
| 245 | + foreach ($contact['EMAIL'] as $email) { |
|
| 246 | + $entry->addEMailAddress($email); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + // Attach all other properties to the entry too because some |
|
| 251 | + // providers might make use of it. |
|
| 252 | + $entry->setProperties($contact); |
|
| 253 | + |
|
| 254 | + return $entry; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | 257 | } |