@@ -40,309 +40,309 @@ |
||
| 40 | 40 | use Sabre\DAVACL\PrincipalBackend\BackendInterface; |
| 41 | 41 | |
| 42 | 42 | class GroupPrincipalBackend implements BackendInterface { |
| 43 | - public const PRINCIPAL_PREFIX = 'principals/groups'; |
|
| 44 | - |
|
| 45 | - /** @var IGroupManager */ |
|
| 46 | - private $groupManager; |
|
| 47 | - |
|
| 48 | - /** @var IUserSession */ |
|
| 49 | - private $userSession; |
|
| 50 | - |
|
| 51 | - /** @var IShareManager */ |
|
| 52 | - private $shareManager; |
|
| 53 | - /** @var IConfig */ |
|
| 54 | - private $config; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param IGroupManager $IGroupManager |
|
| 58 | - * @param IUserSession $userSession |
|
| 59 | - * @param IShareManager $shareManager |
|
| 60 | - */ |
|
| 61 | - public function __construct( |
|
| 62 | - IGroupManager $IGroupManager, |
|
| 63 | - IUserSession $userSession, |
|
| 64 | - IShareManager $shareManager, |
|
| 65 | - IConfig $config |
|
| 66 | - ) { |
|
| 67 | - $this->groupManager = $IGroupManager; |
|
| 68 | - $this->userSession = $userSession; |
|
| 69 | - $this->shareManager = $shareManager; |
|
| 70 | - $this->config = $config; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Returns a list of principals based on a prefix. |
|
| 75 | - * |
|
| 76 | - * This prefix will often contain something like 'principals'. You are only |
|
| 77 | - * expected to return principals that are in this base path. |
|
| 78 | - * |
|
| 79 | - * You are expected to return at least a 'uri' for every user, you can |
|
| 80 | - * return any additional properties if you wish so. Common properties are: |
|
| 81 | - * {DAV:}displayname |
|
| 82 | - * |
|
| 83 | - * @param string $prefixPath |
|
| 84 | - * @return string[] |
|
| 85 | - */ |
|
| 86 | - public function getPrincipalsByPrefix($prefixPath) { |
|
| 87 | - $principals = []; |
|
| 88 | - |
|
| 89 | - if ($prefixPath === self::PRINCIPAL_PREFIX) { |
|
| 90 | - foreach ($this->groupManager->search('') as $user) { |
|
| 91 | - $principals[] = $this->groupToPrincipal($user); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - return $principals; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Returns a specific principal, specified by it's path. |
|
| 100 | - * The returned structure should be the exact same as from |
|
| 101 | - * getPrincipalsByPrefix. |
|
| 102 | - * |
|
| 103 | - * @param string $path |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - public function getPrincipalByPath($path) { |
|
| 107 | - $elements = explode('/', $path, 3); |
|
| 108 | - if ($elements[0] !== 'principals') { |
|
| 109 | - return null; |
|
| 110 | - } |
|
| 111 | - if ($elements[1] !== 'groups') { |
|
| 112 | - return null; |
|
| 113 | - } |
|
| 114 | - $name = urldecode($elements[2]); |
|
| 115 | - $group = $this->groupManager->get($name); |
|
| 116 | - |
|
| 117 | - if (!is_null($group)) { |
|
| 118 | - return $this->groupToPrincipal($group); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return null; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Returns the list of members for a group-principal |
|
| 126 | - * |
|
| 127 | - * @param string $principal |
|
| 128 | - * @return string[] |
|
| 129 | - * @throws Exception |
|
| 130 | - */ |
|
| 131 | - public function getGroupMemberSet($principal) { |
|
| 132 | - $elements = explode('/', $principal); |
|
| 133 | - if ($elements[0] !== 'principals') { |
|
| 134 | - return []; |
|
| 135 | - } |
|
| 136 | - if ($elements[1] !== 'groups') { |
|
| 137 | - return []; |
|
| 138 | - } |
|
| 139 | - $name = $elements[2]; |
|
| 140 | - $group = $this->groupManager->get($name); |
|
| 141 | - |
|
| 142 | - if (is_null($group)) { |
|
| 143 | - return []; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return array_map(function ($user) { |
|
| 147 | - return $this->userToPrincipal($user); |
|
| 148 | - }, $group->getUsers()); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Returns the list of groups a principal is a member of |
|
| 153 | - * |
|
| 154 | - * @param string $principal |
|
| 155 | - * @return array |
|
| 156 | - * @throws Exception |
|
| 157 | - */ |
|
| 158 | - public function getGroupMembership($principal) { |
|
| 159 | - return []; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Updates the list of group members for a group principal. |
|
| 164 | - * |
|
| 165 | - * The principals should be passed as a list of uri's. |
|
| 166 | - * |
|
| 167 | - * @param string $principal |
|
| 168 | - * @param string[] $members |
|
| 169 | - * @throws Exception |
|
| 170 | - */ |
|
| 171 | - public function setGroupMemberSet($principal, array $members) { |
|
| 172 | - throw new Exception('Setting members of the group is not supported yet'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param string $path |
|
| 177 | - * @param PropPatch $propPatch |
|
| 178 | - * @return int |
|
| 179 | - */ |
|
| 180 | - public function updatePrincipal($path, PropPatch $propPatch) { |
|
| 181 | - return 0; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @param string $prefixPath |
|
| 186 | - * @param array $searchProperties |
|
| 187 | - * @param string $test |
|
| 188 | - * @return array |
|
| 189 | - */ |
|
| 190 | - public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
| 191 | - $results = []; |
|
| 192 | - |
|
| 193 | - if (\count($searchProperties) === 0) { |
|
| 194 | - return []; |
|
| 195 | - } |
|
| 196 | - if ($prefixPath !== self::PRINCIPAL_PREFIX) { |
|
| 197 | - return []; |
|
| 198 | - } |
|
| 199 | - // If sharing or group sharing is disabled, return the empty array |
|
| 200 | - if (!$this->groupSharingEnabled()) { |
|
| 201 | - return []; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // If sharing is restricted to group members only, |
|
| 205 | - // return only members that have groups in common |
|
| 206 | - $restrictGroups = false; |
|
| 207 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 208 | - $user = $this->userSession->getUser(); |
|
| 209 | - if (!$user) { |
|
| 210 | - return []; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $searchLimit = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
| 217 | - if ($searchLimit <= 0) { |
|
| 218 | - $searchLimit = null; |
|
| 219 | - } |
|
| 220 | - foreach ($searchProperties as $prop => $value) { |
|
| 221 | - switch ($prop) { |
|
| 222 | - case '{DAV:}displayname': |
|
| 223 | - $groups = $this->groupManager->search($value, $searchLimit); |
|
| 224 | - |
|
| 225 | - $results[] = array_reduce($groups, function (array $carry, IGroup $group) use ($restrictGroups) { |
|
| 226 | - $gid = $group->getGID(); |
|
| 227 | - // is sharing restricted to groups only? |
|
| 228 | - if ($restrictGroups !== false) { |
|
| 229 | - if (!\in_array($gid, $restrictGroups, true)) { |
|
| 230 | - return $carry; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $carry[] = self::PRINCIPAL_PREFIX . '/' . urlencode($gid); |
|
| 235 | - return $carry; |
|
| 236 | - }, []); |
|
| 237 | - break; |
|
| 238 | - |
|
| 239 | - case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set': |
|
| 240 | - // If you add support for more search properties that qualify as a user-address, |
|
| 241 | - // please also add them to the array below |
|
| 242 | - $results[] = $this->searchPrincipals(self::PRINCIPAL_PREFIX, [ |
|
| 243 | - ], 'anyof'); |
|
| 244 | - break; |
|
| 245 | - |
|
| 246 | - default: |
|
| 247 | - $results[] = []; |
|
| 248 | - break; |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - // results is an array of arrays, so this is not the first search result |
|
| 253 | - // but the results of the first searchProperty |
|
| 254 | - if (count($results) === 1) { |
|
| 255 | - return $results[0]; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - switch ($test) { |
|
| 259 | - case 'anyof': |
|
| 260 | - return array_values(array_unique(array_merge(...$results))); |
|
| 261 | - |
|
| 262 | - case 'allof': |
|
| 263 | - default: |
|
| 264 | - return array_values(array_intersect(...$results)); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param string $uri |
|
| 270 | - * @param string $principalPrefix |
|
| 271 | - * @return string |
|
| 272 | - */ |
|
| 273 | - public function findByUri($uri, $principalPrefix) { |
|
| 274 | - // If sharing is disabled, return the empty array |
|
| 275 | - if (!$this->groupSharingEnabled()) { |
|
| 276 | - return null; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - // If sharing is restricted to group members only, |
|
| 280 | - // return only members that have groups in common |
|
| 281 | - $restrictGroups = false; |
|
| 282 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 283 | - $user = $this->userSession->getUser(); |
|
| 284 | - if (!$user) { |
|
| 285 | - return null; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if (strpos($uri, 'principal:principals/groups/') === 0) { |
|
| 292 | - $name = urlencode(substr($uri, 28)); |
|
| 293 | - if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) { |
|
| 294 | - return null; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return substr($uri, 10); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - return null; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * @param IGroup $group |
|
| 305 | - * @return array |
|
| 306 | - */ |
|
| 307 | - protected function groupToPrincipal($group) { |
|
| 308 | - $groupId = $group->getGID(); |
|
| 309 | - // getDisplayName returns UID if none |
|
| 310 | - $displayName = $group->getDisplayName(); |
|
| 311 | - |
|
| 312 | - return [ |
|
| 313 | - 'uri' => 'principals/groups/' . urlencode($groupId), |
|
| 314 | - '{DAV:}displayname' => $displayName, |
|
| 315 | - '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP', |
|
| 316 | - ]; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param IUser $user |
|
| 321 | - * @return array |
|
| 322 | - */ |
|
| 323 | - protected function userToPrincipal($user) { |
|
| 324 | - $userId = $user->getUID(); |
|
| 325 | - // getDisplayName returns UID if none |
|
| 326 | - $displayName = $user->getDisplayName(); |
|
| 327 | - |
|
| 328 | - $principal = [ |
|
| 329 | - 'uri' => 'principals/users/' . $userId, |
|
| 330 | - '{DAV:}displayname' => $displayName, |
|
| 331 | - '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
|
| 332 | - ]; |
|
| 333 | - |
|
| 334 | - $email = $user->getEMailAddress(); |
|
| 335 | - if (!empty($email)) { |
|
| 336 | - $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - return $principal; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * @return bool |
|
| 344 | - */ |
|
| 345 | - private function groupSharingEnabled(): bool { |
|
| 346 | - return $this->shareManager->shareApiEnabled() && $this->shareManager->allowGroupSharing(); |
|
| 347 | - } |
|
| 43 | + public const PRINCIPAL_PREFIX = 'principals/groups'; |
|
| 44 | + |
|
| 45 | + /** @var IGroupManager */ |
|
| 46 | + private $groupManager; |
|
| 47 | + |
|
| 48 | + /** @var IUserSession */ |
|
| 49 | + private $userSession; |
|
| 50 | + |
|
| 51 | + /** @var IShareManager */ |
|
| 52 | + private $shareManager; |
|
| 53 | + /** @var IConfig */ |
|
| 54 | + private $config; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param IGroupManager $IGroupManager |
|
| 58 | + * @param IUserSession $userSession |
|
| 59 | + * @param IShareManager $shareManager |
|
| 60 | + */ |
|
| 61 | + public function __construct( |
|
| 62 | + IGroupManager $IGroupManager, |
|
| 63 | + IUserSession $userSession, |
|
| 64 | + IShareManager $shareManager, |
|
| 65 | + IConfig $config |
|
| 66 | + ) { |
|
| 67 | + $this->groupManager = $IGroupManager; |
|
| 68 | + $this->userSession = $userSession; |
|
| 69 | + $this->shareManager = $shareManager; |
|
| 70 | + $this->config = $config; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Returns a list of principals based on a prefix. |
|
| 75 | + * |
|
| 76 | + * This prefix will often contain something like 'principals'. You are only |
|
| 77 | + * expected to return principals that are in this base path. |
|
| 78 | + * |
|
| 79 | + * You are expected to return at least a 'uri' for every user, you can |
|
| 80 | + * return any additional properties if you wish so. Common properties are: |
|
| 81 | + * {DAV:}displayname |
|
| 82 | + * |
|
| 83 | + * @param string $prefixPath |
|
| 84 | + * @return string[] |
|
| 85 | + */ |
|
| 86 | + public function getPrincipalsByPrefix($prefixPath) { |
|
| 87 | + $principals = []; |
|
| 88 | + |
|
| 89 | + if ($prefixPath === self::PRINCIPAL_PREFIX) { |
|
| 90 | + foreach ($this->groupManager->search('') as $user) { |
|
| 91 | + $principals[] = $this->groupToPrincipal($user); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + return $principals; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Returns a specific principal, specified by it's path. |
|
| 100 | + * The returned structure should be the exact same as from |
|
| 101 | + * getPrincipalsByPrefix. |
|
| 102 | + * |
|
| 103 | + * @param string $path |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + public function getPrincipalByPath($path) { |
|
| 107 | + $elements = explode('/', $path, 3); |
|
| 108 | + if ($elements[0] !== 'principals') { |
|
| 109 | + return null; |
|
| 110 | + } |
|
| 111 | + if ($elements[1] !== 'groups') { |
|
| 112 | + return null; |
|
| 113 | + } |
|
| 114 | + $name = urldecode($elements[2]); |
|
| 115 | + $group = $this->groupManager->get($name); |
|
| 116 | + |
|
| 117 | + if (!is_null($group)) { |
|
| 118 | + return $this->groupToPrincipal($group); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return null; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Returns the list of members for a group-principal |
|
| 126 | + * |
|
| 127 | + * @param string $principal |
|
| 128 | + * @return string[] |
|
| 129 | + * @throws Exception |
|
| 130 | + */ |
|
| 131 | + public function getGroupMemberSet($principal) { |
|
| 132 | + $elements = explode('/', $principal); |
|
| 133 | + if ($elements[0] !== 'principals') { |
|
| 134 | + return []; |
|
| 135 | + } |
|
| 136 | + if ($elements[1] !== 'groups') { |
|
| 137 | + return []; |
|
| 138 | + } |
|
| 139 | + $name = $elements[2]; |
|
| 140 | + $group = $this->groupManager->get($name); |
|
| 141 | + |
|
| 142 | + if (is_null($group)) { |
|
| 143 | + return []; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return array_map(function ($user) { |
|
| 147 | + return $this->userToPrincipal($user); |
|
| 148 | + }, $group->getUsers()); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Returns the list of groups a principal is a member of |
|
| 153 | + * |
|
| 154 | + * @param string $principal |
|
| 155 | + * @return array |
|
| 156 | + * @throws Exception |
|
| 157 | + */ |
|
| 158 | + public function getGroupMembership($principal) { |
|
| 159 | + return []; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Updates the list of group members for a group principal. |
|
| 164 | + * |
|
| 165 | + * The principals should be passed as a list of uri's. |
|
| 166 | + * |
|
| 167 | + * @param string $principal |
|
| 168 | + * @param string[] $members |
|
| 169 | + * @throws Exception |
|
| 170 | + */ |
|
| 171 | + public function setGroupMemberSet($principal, array $members) { |
|
| 172 | + throw new Exception('Setting members of the group is not supported yet'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param string $path |
|
| 177 | + * @param PropPatch $propPatch |
|
| 178 | + * @return int |
|
| 179 | + */ |
|
| 180 | + public function updatePrincipal($path, PropPatch $propPatch) { |
|
| 181 | + return 0; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @param string $prefixPath |
|
| 186 | + * @param array $searchProperties |
|
| 187 | + * @param string $test |
|
| 188 | + * @return array |
|
| 189 | + */ |
|
| 190 | + public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
| 191 | + $results = []; |
|
| 192 | + |
|
| 193 | + if (\count($searchProperties) === 0) { |
|
| 194 | + return []; |
|
| 195 | + } |
|
| 196 | + if ($prefixPath !== self::PRINCIPAL_PREFIX) { |
|
| 197 | + return []; |
|
| 198 | + } |
|
| 199 | + // If sharing or group sharing is disabled, return the empty array |
|
| 200 | + if (!$this->groupSharingEnabled()) { |
|
| 201 | + return []; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // If sharing is restricted to group members only, |
|
| 205 | + // return only members that have groups in common |
|
| 206 | + $restrictGroups = false; |
|
| 207 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 208 | + $user = $this->userSession->getUser(); |
|
| 209 | + if (!$user) { |
|
| 210 | + return []; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $searchLimit = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
| 217 | + if ($searchLimit <= 0) { |
|
| 218 | + $searchLimit = null; |
|
| 219 | + } |
|
| 220 | + foreach ($searchProperties as $prop => $value) { |
|
| 221 | + switch ($prop) { |
|
| 222 | + case '{DAV:}displayname': |
|
| 223 | + $groups = $this->groupManager->search($value, $searchLimit); |
|
| 224 | + |
|
| 225 | + $results[] = array_reduce($groups, function (array $carry, IGroup $group) use ($restrictGroups) { |
|
| 226 | + $gid = $group->getGID(); |
|
| 227 | + // is sharing restricted to groups only? |
|
| 228 | + if ($restrictGroups !== false) { |
|
| 229 | + if (!\in_array($gid, $restrictGroups, true)) { |
|
| 230 | + return $carry; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $carry[] = self::PRINCIPAL_PREFIX . '/' . urlencode($gid); |
|
| 235 | + return $carry; |
|
| 236 | + }, []); |
|
| 237 | + break; |
|
| 238 | + |
|
| 239 | + case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set': |
|
| 240 | + // If you add support for more search properties that qualify as a user-address, |
|
| 241 | + // please also add them to the array below |
|
| 242 | + $results[] = $this->searchPrincipals(self::PRINCIPAL_PREFIX, [ |
|
| 243 | + ], 'anyof'); |
|
| 244 | + break; |
|
| 245 | + |
|
| 246 | + default: |
|
| 247 | + $results[] = []; |
|
| 248 | + break; |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + // results is an array of arrays, so this is not the first search result |
|
| 253 | + // but the results of the first searchProperty |
|
| 254 | + if (count($results) === 1) { |
|
| 255 | + return $results[0]; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + switch ($test) { |
|
| 259 | + case 'anyof': |
|
| 260 | + return array_values(array_unique(array_merge(...$results))); |
|
| 261 | + |
|
| 262 | + case 'allof': |
|
| 263 | + default: |
|
| 264 | + return array_values(array_intersect(...$results)); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param string $uri |
|
| 270 | + * @param string $principalPrefix |
|
| 271 | + * @return string |
|
| 272 | + */ |
|
| 273 | + public function findByUri($uri, $principalPrefix) { |
|
| 274 | + // If sharing is disabled, return the empty array |
|
| 275 | + if (!$this->groupSharingEnabled()) { |
|
| 276 | + return null; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + // If sharing is restricted to group members only, |
|
| 280 | + // return only members that have groups in common |
|
| 281 | + $restrictGroups = false; |
|
| 282 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 283 | + $user = $this->userSession->getUser(); |
|
| 284 | + if (!$user) { |
|
| 285 | + return null; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if (strpos($uri, 'principal:principals/groups/') === 0) { |
|
| 292 | + $name = urlencode(substr($uri, 28)); |
|
| 293 | + if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) { |
|
| 294 | + return null; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return substr($uri, 10); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + return null; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * @param IGroup $group |
|
| 305 | + * @return array |
|
| 306 | + */ |
|
| 307 | + protected function groupToPrincipal($group) { |
|
| 308 | + $groupId = $group->getGID(); |
|
| 309 | + // getDisplayName returns UID if none |
|
| 310 | + $displayName = $group->getDisplayName(); |
|
| 311 | + |
|
| 312 | + return [ |
|
| 313 | + 'uri' => 'principals/groups/' . urlencode($groupId), |
|
| 314 | + '{DAV:}displayname' => $displayName, |
|
| 315 | + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP', |
|
| 316 | + ]; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param IUser $user |
|
| 321 | + * @return array |
|
| 322 | + */ |
|
| 323 | + protected function userToPrincipal($user) { |
|
| 324 | + $userId = $user->getUID(); |
|
| 325 | + // getDisplayName returns UID if none |
|
| 326 | + $displayName = $user->getDisplayName(); |
|
| 327 | + |
|
| 328 | + $principal = [ |
|
| 329 | + 'uri' => 'principals/users/' . $userId, |
|
| 330 | + '{DAV:}displayname' => $displayName, |
|
| 331 | + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
|
| 332 | + ]; |
|
| 333 | + |
|
| 334 | + $email = $user->getEMailAddress(); |
|
| 335 | + if (!empty($email)) { |
|
| 336 | + $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + return $principal; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * @return bool |
|
| 344 | + */ |
|
| 345 | + private function groupSharingEnabled(): bool { |
|
| 346 | + return $this->shareManager->shareApiEnabled() && $this->shareManager->allowGroupSharing(); |
|
| 347 | + } |
|
| 348 | 348 | } |
@@ -57,385 +57,385 @@ |
||
| 57 | 57 | |
| 58 | 58 | class ShareesAPIController extends OCSController { |
| 59 | 59 | |
| 60 | - /** @var string */ |
|
| 61 | - protected $userId; |
|
| 62 | - |
|
| 63 | - /** @var IConfig */ |
|
| 64 | - protected $config; |
|
| 65 | - |
|
| 66 | - /** @var IURLGenerator */ |
|
| 67 | - protected $urlGenerator; |
|
| 68 | - |
|
| 69 | - /** @var IManager */ |
|
| 70 | - protected $shareManager; |
|
| 71 | - |
|
| 72 | - /** @var int */ |
|
| 73 | - protected $offset = 0; |
|
| 74 | - |
|
| 75 | - /** @var int */ |
|
| 76 | - protected $limit = 10; |
|
| 77 | - |
|
| 78 | - /** @var array */ |
|
| 79 | - protected $result = [ |
|
| 80 | - 'exact' => [ |
|
| 81 | - 'users' => [], |
|
| 82 | - 'groups' => [], |
|
| 83 | - 'remotes' => [], |
|
| 84 | - 'remote_groups' => [], |
|
| 85 | - 'emails' => [], |
|
| 86 | - 'circles' => [], |
|
| 87 | - 'rooms' => [], |
|
| 88 | - 'deck' => [], |
|
| 89 | - ], |
|
| 90 | - 'users' => [], |
|
| 91 | - 'groups' => [], |
|
| 92 | - 'remotes' => [], |
|
| 93 | - 'remote_groups' => [], |
|
| 94 | - 'emails' => [], |
|
| 95 | - 'lookup' => [], |
|
| 96 | - 'circles' => [], |
|
| 97 | - 'rooms' => [], |
|
| 98 | - 'deck' => [], |
|
| 99 | - 'lookupEnabled' => false, |
|
| 100 | - ]; |
|
| 101 | - |
|
| 102 | - protected $reachedEndFor = []; |
|
| 103 | - /** @var ISearch */ |
|
| 104 | - private $collaboratorSearch; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param string $UserId |
|
| 108 | - * @param string $appName |
|
| 109 | - * @param IRequest $request |
|
| 110 | - * @param IConfig $config |
|
| 111 | - * @param IURLGenerator $urlGenerator |
|
| 112 | - * @param IManager $shareManager |
|
| 113 | - * @param ISearch $collaboratorSearch |
|
| 114 | - */ |
|
| 115 | - public function __construct( |
|
| 116 | - $UserId, |
|
| 117 | - string $appName, |
|
| 118 | - IRequest $request, |
|
| 119 | - IConfig $config, |
|
| 120 | - IURLGenerator $urlGenerator, |
|
| 121 | - IManager $shareManager, |
|
| 122 | - ISearch $collaboratorSearch |
|
| 123 | - ) { |
|
| 124 | - parent::__construct($appName, $request); |
|
| 125 | - $this->userId = $UserId; |
|
| 126 | - $this->config = $config; |
|
| 127 | - $this->urlGenerator = $urlGenerator; |
|
| 128 | - $this->shareManager = $shareManager; |
|
| 129 | - $this->collaboratorSearch = $collaboratorSearch; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @NoAdminRequired |
|
| 134 | - * |
|
| 135 | - * @param string $search |
|
| 136 | - * @param string $itemType |
|
| 137 | - * @param int $page |
|
| 138 | - * @param int $perPage |
|
| 139 | - * @param int|int[] $shareType |
|
| 140 | - * @param bool $lookup |
|
| 141 | - * @return DataResponse |
|
| 142 | - * @throws OCSBadRequestException |
|
| 143 | - */ |
|
| 144 | - public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = false): DataResponse { |
|
| 145 | - |
|
| 146 | - // only search for string larger than a given threshold |
|
| 147 | - $threshold = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
| 148 | - if (strlen($search) < $threshold) { |
|
| 149 | - return new DataResponse($this->result); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // never return more than the max. number of results configured in the config.php |
|
| 153 | - $maxResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
| 154 | - if ($maxResults > 0) { |
|
| 155 | - $perPage = min($perPage, $maxResults); |
|
| 156 | - } |
|
| 157 | - if ($perPage <= 0) { |
|
| 158 | - throw new OCSBadRequestException('Invalid perPage argument'); |
|
| 159 | - } |
|
| 160 | - if ($page <= 0) { |
|
| 161 | - throw new OCSBadRequestException('Invalid page'); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $shareTypes = [ |
|
| 165 | - IShare::TYPE_USER, |
|
| 166 | - ]; |
|
| 167 | - |
|
| 168 | - if ($itemType === null) { |
|
| 169 | - throw new OCSBadRequestException('Missing itemType'); |
|
| 170 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 171 | - if ($this->shareManager->allowGroupSharing()) { |
|
| 172 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 176 | - $shareTypes[] = IShare::TYPE_REMOTE; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
| 180 | - $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 184 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
| 188 | - $shareTypes[] = IShare::TYPE_ROOM; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 192 | - $shareTypes[] = IShare::TYPE_DECK; |
|
| 193 | - } |
|
| 194 | - } else { |
|
| 195 | - if ($this->shareManager->allowGroupSharing()) { |
|
| 196 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
| 197 | - } |
|
| 198 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // FIXME: DI |
|
| 202 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 203 | - $shareTypes[] = IShare::TYPE_CIRCLE; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 207 | - $shareTypes[] = IShare::TYPE_DECK; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - if ($shareType !== null && is_array($shareType)) { |
|
| 211 | - $shareTypes = array_intersect($shareTypes, $shareType); |
|
| 212 | - } elseif (is_numeric($shareType)) { |
|
| 213 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 214 | - } |
|
| 215 | - sort($shareTypes); |
|
| 216 | - |
|
| 217 | - $this->limit = $perPage; |
|
| 218 | - $this->offset = $perPage * ($page - 1); |
|
| 219 | - |
|
| 220 | - // In global scale mode we always search the loogup server |
|
| 221 | - if ($this->config->getSystemValueBool('gs.enabled', false)) { |
|
| 222 | - $lookup = true; |
|
| 223 | - $this->result['lookupEnabled'] = true; |
|
| 224 | - } else { |
|
| 225 | - $this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes'; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - [$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
| 229 | - |
|
| 230 | - // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
| 231 | - if (isset($result['exact'])) { |
|
| 232 | - $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
| 233 | - } |
|
| 234 | - $this->result = array_merge($this->result, $result); |
|
| 235 | - $response = new DataResponse($this->result); |
|
| 236 | - |
|
| 237 | - if ($hasMoreResults) { |
|
| 238 | - $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
| 239 | - 'search' => $search, |
|
| 240 | - 'itemType' => $itemType, |
|
| 241 | - 'shareType' => $shareTypes, |
|
| 242 | - 'perPage' => $perPage, |
|
| 243 | - ])); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return $response; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @param string $user |
|
| 251 | - * @param int $shareType |
|
| 252 | - * |
|
| 253 | - * @return Generator<array<string>> |
|
| 254 | - */ |
|
| 255 | - private function getAllShareesByType(string $user, int $shareType): Generator { |
|
| 256 | - $offset = 0; |
|
| 257 | - $pageSize = 50; |
|
| 258 | - |
|
| 259 | - while (count($page = $this->shareManager->getSharesBy( |
|
| 260 | - $user, |
|
| 261 | - $shareType, |
|
| 262 | - null, |
|
| 263 | - false, |
|
| 264 | - $pageSize, |
|
| 265 | - $offset |
|
| 266 | - ))) { |
|
| 267 | - foreach ($page as $share) { |
|
| 268 | - yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()]; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $offset += $pageSize; |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - private function sortShareesByFrequency(array $sharees): array { |
|
| 276 | - usort($sharees, function (array $s1, array $s2) { |
|
| 277 | - return $s2['count'] - $s1['count']; |
|
| 278 | - }); |
|
| 279 | - return $sharees; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - private $searchResultTypeMap = [ |
|
| 283 | - IShare::TYPE_USER => 'users', |
|
| 284 | - IShare::TYPE_GROUP => 'groups', |
|
| 285 | - IShare::TYPE_REMOTE => 'remotes', |
|
| 286 | - IShare::TYPE_REMOTE_GROUP => 'remote_groups', |
|
| 287 | - IShare::TYPE_EMAIL => 'emails', |
|
| 288 | - ]; |
|
| 289 | - |
|
| 290 | - private function getAllSharees(string $user, array $shareTypes): ISearchResult { |
|
| 291 | - $result = []; |
|
| 292 | - foreach ($shareTypes as $shareType) { |
|
| 293 | - $sharees = $this->getAllShareesByType($user, $shareType); |
|
| 294 | - $shareTypeResults = []; |
|
| 295 | - foreach ($sharees as [$sharee, $displayname]) { |
|
| 296 | - if (!isset($this->searchResultTypeMap[$shareType])) { |
|
| 297 | - continue; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - if (!isset($shareTypeResults[$sharee])) { |
|
| 301 | - $shareTypeResults[$sharee] = [ |
|
| 302 | - 'count' => 1, |
|
| 303 | - 'label' => $displayname, |
|
| 304 | - 'value' => [ |
|
| 305 | - 'shareType' => $shareType, |
|
| 306 | - 'shareWith' => $sharee, |
|
| 307 | - ], |
|
| 308 | - ]; |
|
| 309 | - } else { |
|
| 310 | - $shareTypeResults[$sharee]['count']++; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - $result = array_merge($result, array_values($shareTypeResults)); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $top5 = array_slice( |
|
| 317 | - $this->sortShareesByFrequency($result), |
|
| 318 | - 0, |
|
| 319 | - 5 |
|
| 320 | - ); |
|
| 321 | - |
|
| 322 | - $searchResult = new SearchResult(); |
|
| 323 | - foreach ($this->searchResultTypeMap as $int => $str) { |
|
| 324 | - $searchResult->addResultSet(new SearchResultType($str), [], []); |
|
| 325 | - foreach ($top5 as $x) { |
|
| 326 | - if ($x['value']['shareType'] === $int) { |
|
| 327 | - $searchResult->addResultSet(new SearchResultType($str), [], [$x]); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - return $searchResult; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @NoAdminRequired |
|
| 336 | - * |
|
| 337 | - * @param string $itemType |
|
| 338 | - * @return DataResponse |
|
| 339 | - * @throws OCSBadRequestException |
|
| 340 | - */ |
|
| 341 | - public function findRecommended(string $itemType = null, $shareType = null): DataResponse { |
|
| 342 | - $shareTypes = [ |
|
| 343 | - IShare::TYPE_USER, |
|
| 344 | - ]; |
|
| 345 | - |
|
| 346 | - if ($itemType === null) { |
|
| 347 | - throw new OCSBadRequestException('Missing itemType'); |
|
| 348 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 349 | - if ($this->shareManager->allowGroupSharing()) { |
|
| 350 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 354 | - $shareTypes[] = IShare::TYPE_REMOTE; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
| 358 | - $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 362 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
| 366 | - $shareTypes[] = IShare::TYPE_ROOM; |
|
| 367 | - } |
|
| 368 | - } else { |
|
| 369 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
| 370 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - // FIXME: DI |
|
| 374 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 375 | - $shareTypes[] = IShare::TYPE_CIRCLE; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
| 379 | - $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
| 380 | - sort($shareTypes); |
|
| 381 | - } elseif (is_numeric($shareType)) { |
|
| 382 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 383 | - sort($shareTypes); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - return new DataResponse( |
|
| 387 | - $this->getAllSharees($this->userId, $shareTypes)->asArray() |
|
| 388 | - ); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Method to get out the static call for better testing |
|
| 393 | - * |
|
| 394 | - * @param string $itemType |
|
| 395 | - * @return bool |
|
| 396 | - */ |
|
| 397 | - protected function isRemoteSharingAllowed(string $itemType): bool { |
|
| 398 | - try { |
|
| 399 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 400 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
| 401 | - return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE); |
|
| 402 | - } catch (\Exception $e) { |
|
| 403 | - return false; |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - protected function isRemoteGroupSharingAllowed(string $itemType): bool { |
|
| 408 | - try { |
|
| 409 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 410 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
| 411 | - return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP); |
|
| 412 | - } catch (\Exception $e) { |
|
| 413 | - return false; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Generates a bunch of pagination links for the current page |
|
| 420 | - * |
|
| 421 | - * @param int $page Current page |
|
| 422 | - * @param array $params Parameters for the URL |
|
| 423 | - * @return string |
|
| 424 | - */ |
|
| 425 | - protected function getPaginationLink(int $page, array $params): string { |
|
| 426 | - if ($this->isV2()) { |
|
| 427 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 428 | - } else { |
|
| 429 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 430 | - } |
|
| 431 | - $params['page'] = $page + 1; |
|
| 432 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @return bool |
|
| 437 | - */ |
|
| 438 | - protected function isV2(): bool { |
|
| 439 | - return $this->request->getScriptName() === '/ocs/v2.php'; |
|
| 440 | - } |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $userId; |
|
| 62 | + |
|
| 63 | + /** @var IConfig */ |
|
| 64 | + protected $config; |
|
| 65 | + |
|
| 66 | + /** @var IURLGenerator */ |
|
| 67 | + protected $urlGenerator; |
|
| 68 | + |
|
| 69 | + /** @var IManager */ |
|
| 70 | + protected $shareManager; |
|
| 71 | + |
|
| 72 | + /** @var int */ |
|
| 73 | + protected $offset = 0; |
|
| 74 | + |
|
| 75 | + /** @var int */ |
|
| 76 | + protected $limit = 10; |
|
| 77 | + |
|
| 78 | + /** @var array */ |
|
| 79 | + protected $result = [ |
|
| 80 | + 'exact' => [ |
|
| 81 | + 'users' => [], |
|
| 82 | + 'groups' => [], |
|
| 83 | + 'remotes' => [], |
|
| 84 | + 'remote_groups' => [], |
|
| 85 | + 'emails' => [], |
|
| 86 | + 'circles' => [], |
|
| 87 | + 'rooms' => [], |
|
| 88 | + 'deck' => [], |
|
| 89 | + ], |
|
| 90 | + 'users' => [], |
|
| 91 | + 'groups' => [], |
|
| 92 | + 'remotes' => [], |
|
| 93 | + 'remote_groups' => [], |
|
| 94 | + 'emails' => [], |
|
| 95 | + 'lookup' => [], |
|
| 96 | + 'circles' => [], |
|
| 97 | + 'rooms' => [], |
|
| 98 | + 'deck' => [], |
|
| 99 | + 'lookupEnabled' => false, |
|
| 100 | + ]; |
|
| 101 | + |
|
| 102 | + protected $reachedEndFor = []; |
|
| 103 | + /** @var ISearch */ |
|
| 104 | + private $collaboratorSearch; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param string $UserId |
|
| 108 | + * @param string $appName |
|
| 109 | + * @param IRequest $request |
|
| 110 | + * @param IConfig $config |
|
| 111 | + * @param IURLGenerator $urlGenerator |
|
| 112 | + * @param IManager $shareManager |
|
| 113 | + * @param ISearch $collaboratorSearch |
|
| 114 | + */ |
|
| 115 | + public function __construct( |
|
| 116 | + $UserId, |
|
| 117 | + string $appName, |
|
| 118 | + IRequest $request, |
|
| 119 | + IConfig $config, |
|
| 120 | + IURLGenerator $urlGenerator, |
|
| 121 | + IManager $shareManager, |
|
| 122 | + ISearch $collaboratorSearch |
|
| 123 | + ) { |
|
| 124 | + parent::__construct($appName, $request); |
|
| 125 | + $this->userId = $UserId; |
|
| 126 | + $this->config = $config; |
|
| 127 | + $this->urlGenerator = $urlGenerator; |
|
| 128 | + $this->shareManager = $shareManager; |
|
| 129 | + $this->collaboratorSearch = $collaboratorSearch; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @NoAdminRequired |
|
| 134 | + * |
|
| 135 | + * @param string $search |
|
| 136 | + * @param string $itemType |
|
| 137 | + * @param int $page |
|
| 138 | + * @param int $perPage |
|
| 139 | + * @param int|int[] $shareType |
|
| 140 | + * @param bool $lookup |
|
| 141 | + * @return DataResponse |
|
| 142 | + * @throws OCSBadRequestException |
|
| 143 | + */ |
|
| 144 | + public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = false): DataResponse { |
|
| 145 | + |
|
| 146 | + // only search for string larger than a given threshold |
|
| 147 | + $threshold = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
| 148 | + if (strlen($search) < $threshold) { |
|
| 149 | + return new DataResponse($this->result); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // never return more than the max. number of results configured in the config.php |
|
| 153 | + $maxResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
| 154 | + if ($maxResults > 0) { |
|
| 155 | + $perPage = min($perPage, $maxResults); |
|
| 156 | + } |
|
| 157 | + if ($perPage <= 0) { |
|
| 158 | + throw new OCSBadRequestException('Invalid perPage argument'); |
|
| 159 | + } |
|
| 160 | + if ($page <= 0) { |
|
| 161 | + throw new OCSBadRequestException('Invalid page'); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $shareTypes = [ |
|
| 165 | + IShare::TYPE_USER, |
|
| 166 | + ]; |
|
| 167 | + |
|
| 168 | + if ($itemType === null) { |
|
| 169 | + throw new OCSBadRequestException('Missing itemType'); |
|
| 170 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 171 | + if ($this->shareManager->allowGroupSharing()) { |
|
| 172 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 176 | + $shareTypes[] = IShare::TYPE_REMOTE; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
| 180 | + $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 184 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
| 188 | + $shareTypes[] = IShare::TYPE_ROOM; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 192 | + $shareTypes[] = IShare::TYPE_DECK; |
|
| 193 | + } |
|
| 194 | + } else { |
|
| 195 | + if ($this->shareManager->allowGroupSharing()) { |
|
| 196 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
| 197 | + } |
|
| 198 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // FIXME: DI |
|
| 202 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 203 | + $shareTypes[] = IShare::TYPE_CIRCLE; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 207 | + $shareTypes[] = IShare::TYPE_DECK; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + if ($shareType !== null && is_array($shareType)) { |
|
| 211 | + $shareTypes = array_intersect($shareTypes, $shareType); |
|
| 212 | + } elseif (is_numeric($shareType)) { |
|
| 213 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 214 | + } |
|
| 215 | + sort($shareTypes); |
|
| 216 | + |
|
| 217 | + $this->limit = $perPage; |
|
| 218 | + $this->offset = $perPage * ($page - 1); |
|
| 219 | + |
|
| 220 | + // In global scale mode we always search the loogup server |
|
| 221 | + if ($this->config->getSystemValueBool('gs.enabled', false)) { |
|
| 222 | + $lookup = true; |
|
| 223 | + $this->result['lookupEnabled'] = true; |
|
| 224 | + } else { |
|
| 225 | + $this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes'; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + [$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
| 229 | + |
|
| 230 | + // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
| 231 | + if (isset($result['exact'])) { |
|
| 232 | + $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
| 233 | + } |
|
| 234 | + $this->result = array_merge($this->result, $result); |
|
| 235 | + $response = new DataResponse($this->result); |
|
| 236 | + |
|
| 237 | + if ($hasMoreResults) { |
|
| 238 | + $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
| 239 | + 'search' => $search, |
|
| 240 | + 'itemType' => $itemType, |
|
| 241 | + 'shareType' => $shareTypes, |
|
| 242 | + 'perPage' => $perPage, |
|
| 243 | + ])); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return $response; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @param string $user |
|
| 251 | + * @param int $shareType |
|
| 252 | + * |
|
| 253 | + * @return Generator<array<string>> |
|
| 254 | + */ |
|
| 255 | + private function getAllShareesByType(string $user, int $shareType): Generator { |
|
| 256 | + $offset = 0; |
|
| 257 | + $pageSize = 50; |
|
| 258 | + |
|
| 259 | + while (count($page = $this->shareManager->getSharesBy( |
|
| 260 | + $user, |
|
| 261 | + $shareType, |
|
| 262 | + null, |
|
| 263 | + false, |
|
| 264 | + $pageSize, |
|
| 265 | + $offset |
|
| 266 | + ))) { |
|
| 267 | + foreach ($page as $share) { |
|
| 268 | + yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()]; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $offset += $pageSize; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + private function sortShareesByFrequency(array $sharees): array { |
|
| 276 | + usort($sharees, function (array $s1, array $s2) { |
|
| 277 | + return $s2['count'] - $s1['count']; |
|
| 278 | + }); |
|
| 279 | + return $sharees; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + private $searchResultTypeMap = [ |
|
| 283 | + IShare::TYPE_USER => 'users', |
|
| 284 | + IShare::TYPE_GROUP => 'groups', |
|
| 285 | + IShare::TYPE_REMOTE => 'remotes', |
|
| 286 | + IShare::TYPE_REMOTE_GROUP => 'remote_groups', |
|
| 287 | + IShare::TYPE_EMAIL => 'emails', |
|
| 288 | + ]; |
|
| 289 | + |
|
| 290 | + private function getAllSharees(string $user, array $shareTypes): ISearchResult { |
|
| 291 | + $result = []; |
|
| 292 | + foreach ($shareTypes as $shareType) { |
|
| 293 | + $sharees = $this->getAllShareesByType($user, $shareType); |
|
| 294 | + $shareTypeResults = []; |
|
| 295 | + foreach ($sharees as [$sharee, $displayname]) { |
|
| 296 | + if (!isset($this->searchResultTypeMap[$shareType])) { |
|
| 297 | + continue; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + if (!isset($shareTypeResults[$sharee])) { |
|
| 301 | + $shareTypeResults[$sharee] = [ |
|
| 302 | + 'count' => 1, |
|
| 303 | + 'label' => $displayname, |
|
| 304 | + 'value' => [ |
|
| 305 | + 'shareType' => $shareType, |
|
| 306 | + 'shareWith' => $sharee, |
|
| 307 | + ], |
|
| 308 | + ]; |
|
| 309 | + } else { |
|
| 310 | + $shareTypeResults[$sharee]['count']++; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + $result = array_merge($result, array_values($shareTypeResults)); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $top5 = array_slice( |
|
| 317 | + $this->sortShareesByFrequency($result), |
|
| 318 | + 0, |
|
| 319 | + 5 |
|
| 320 | + ); |
|
| 321 | + |
|
| 322 | + $searchResult = new SearchResult(); |
|
| 323 | + foreach ($this->searchResultTypeMap as $int => $str) { |
|
| 324 | + $searchResult->addResultSet(new SearchResultType($str), [], []); |
|
| 325 | + foreach ($top5 as $x) { |
|
| 326 | + if ($x['value']['shareType'] === $int) { |
|
| 327 | + $searchResult->addResultSet(new SearchResultType($str), [], [$x]); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + return $searchResult; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @NoAdminRequired |
|
| 336 | + * |
|
| 337 | + * @param string $itemType |
|
| 338 | + * @return DataResponse |
|
| 339 | + * @throws OCSBadRequestException |
|
| 340 | + */ |
|
| 341 | + public function findRecommended(string $itemType = null, $shareType = null): DataResponse { |
|
| 342 | + $shareTypes = [ |
|
| 343 | + IShare::TYPE_USER, |
|
| 344 | + ]; |
|
| 345 | + |
|
| 346 | + if ($itemType === null) { |
|
| 347 | + throw new OCSBadRequestException('Missing itemType'); |
|
| 348 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
| 349 | + if ($this->shareManager->allowGroupSharing()) { |
|
| 350 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
| 354 | + $shareTypes[] = IShare::TYPE_REMOTE; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
| 358 | + $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 362 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
| 366 | + $shareTypes[] = IShare::TYPE_ROOM; |
|
| 367 | + } |
|
| 368 | + } else { |
|
| 369 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
| 370 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + // FIXME: DI |
|
| 374 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 375 | + $shareTypes[] = IShare::TYPE_CIRCLE; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
| 379 | + $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
| 380 | + sort($shareTypes); |
|
| 381 | + } elseif (is_numeric($shareType)) { |
|
| 382 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
| 383 | + sort($shareTypes); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + return new DataResponse( |
|
| 387 | + $this->getAllSharees($this->userId, $shareTypes)->asArray() |
|
| 388 | + ); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Method to get out the static call for better testing |
|
| 393 | + * |
|
| 394 | + * @param string $itemType |
|
| 395 | + * @return bool |
|
| 396 | + */ |
|
| 397 | + protected function isRemoteSharingAllowed(string $itemType): bool { |
|
| 398 | + try { |
|
| 399 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 400 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
| 401 | + return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE); |
|
| 402 | + } catch (\Exception $e) { |
|
| 403 | + return false; |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + protected function isRemoteGroupSharingAllowed(string $itemType): bool { |
|
| 408 | + try { |
|
| 409 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
| 410 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
| 411 | + return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP); |
|
| 412 | + } catch (\Exception $e) { |
|
| 413 | + return false; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Generates a bunch of pagination links for the current page |
|
| 420 | + * |
|
| 421 | + * @param int $page Current page |
|
| 422 | + * @param array $params Parameters for the URL |
|
| 423 | + * @return string |
|
| 424 | + */ |
|
| 425 | + protected function getPaginationLink(int $page, array $params): string { |
|
| 426 | + if ($this->isV2()) { |
|
| 427 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 428 | + } else { |
|
| 429 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
| 430 | + } |
|
| 431 | + $params['page'] = $page + 1; |
|
| 432 | + return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @return bool |
|
| 437 | + */ |
|
| 438 | + protected function isV2(): bool { |
|
| 439 | + return $this->request->getScriptName() === '/ocs/v2.php'; |
|
| 440 | + } |
|
| 441 | 441 | } |
@@ -38,115 +38,115 @@ |
||
| 38 | 38 | use OCP\Share\IShare; |
| 39 | 39 | |
| 40 | 40 | class GroupPlugin implements ISearchPlugin { |
| 41 | - /** @var bool */ |
|
| 42 | - protected $shareeEnumeration; |
|
| 43 | - /** @var bool */ |
|
| 44 | - protected $shareWithGroupOnly; |
|
| 45 | - /** @var bool */ |
|
| 46 | - protected $shareeEnumerationInGroupOnly; |
|
| 47 | - /** @var bool */ |
|
| 48 | - protected $groupSharingDisabled; |
|
| 49 | - |
|
| 50 | - /** @var IGroupManager */ |
|
| 51 | - private $groupManager; |
|
| 52 | - /** @var IConfig */ |
|
| 53 | - private $config; |
|
| 54 | - /** @var IUserSession */ |
|
| 55 | - private $userSession; |
|
| 56 | - |
|
| 57 | - public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 58 | - $this->groupManager = $groupManager; |
|
| 59 | - $this->config = $config; |
|
| 60 | - $this->userSession = $userSession; |
|
| 61 | - |
|
| 62 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 63 | - $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 64 | - $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
|
| 65 | - $this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no'; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 69 | - if ($this->groupSharingDisabled) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $hasMoreResults = false; |
|
| 74 | - $result = ['wide' => [], 'exact' => []]; |
|
| 75 | - |
|
| 76 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 77 | - $groupIds = array_map(function (IGroup $group) { |
|
| 78 | - return $group->getGID(); |
|
| 79 | - }, $groups); |
|
| 80 | - |
|
| 81 | - if (!$this->shareeEnumeration || count($groups) < $limit) { |
|
| 82 | - $hasMoreResults = true; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $userGroups = []; |
|
| 86 | - if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) { |
|
| 87 | - // Intersect all the groups that match with the groups this user is a member of |
|
| 88 | - $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); |
|
| 89 | - $userGroups = array_map(function (IGroup $group) { |
|
| 90 | - return $group->getGID(); |
|
| 91 | - }, $userGroups); |
|
| 92 | - $groupIds = array_intersect($groupIds, $userGroups); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $lowerSearch = strtolower($search); |
|
| 96 | - foreach ($groups as $group) { |
|
| 97 | - if ($group->hideFromCollaboration()) { |
|
| 98 | - continue; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // FIXME: use a more efficient approach |
|
| 102 | - $gid = $group->getGID(); |
|
| 103 | - if (!in_array($gid, $groupIds)) { |
|
| 104 | - continue; |
|
| 105 | - } |
|
| 106 | - if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) { |
|
| 107 | - $result['exact'][] = [ |
|
| 108 | - 'label' => $group->getDisplayName(), |
|
| 109 | - 'value' => [ |
|
| 110 | - 'shareType' => IShare::TYPE_GROUP, |
|
| 111 | - 'shareWith' => $gid, |
|
| 112 | - ], |
|
| 113 | - ]; |
|
| 114 | - } else { |
|
| 115 | - if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) { |
|
| 116 | - continue; |
|
| 117 | - } |
|
| 118 | - $result['wide'][] = [ |
|
| 119 | - 'label' => $group->getDisplayName(), |
|
| 120 | - 'value' => [ |
|
| 121 | - 'shareType' => IShare::TYPE_GROUP, |
|
| 122 | - 'shareWith' => $gid, |
|
| 123 | - ], |
|
| 124 | - ]; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ($offset === 0 && empty($result['exact'])) { |
|
| 129 | - // On page one we try if the search result has a direct hit on the |
|
| 130 | - // user id and if so, we add that to the exact match list |
|
| 131 | - $group = $this->groupManager->get($search); |
|
| 132 | - if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) { |
|
| 133 | - $result['exact'][] = [ |
|
| 134 | - 'label' => $group->getDisplayName(), |
|
| 135 | - 'value' => [ |
|
| 136 | - 'shareType' => IShare::TYPE_GROUP, |
|
| 137 | - 'shareWith' => $group->getGID(), |
|
| 138 | - ], |
|
| 139 | - ]; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if (!$this->shareeEnumeration) { |
|
| 144 | - $result['wide'] = []; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $type = new SearchResultType('groups'); |
|
| 148 | - $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
|
| 149 | - |
|
| 150 | - return $hasMoreResults; |
|
| 151 | - } |
|
| 41 | + /** @var bool */ |
|
| 42 | + protected $shareeEnumeration; |
|
| 43 | + /** @var bool */ |
|
| 44 | + protected $shareWithGroupOnly; |
|
| 45 | + /** @var bool */ |
|
| 46 | + protected $shareeEnumerationInGroupOnly; |
|
| 47 | + /** @var bool */ |
|
| 48 | + protected $groupSharingDisabled; |
|
| 49 | + |
|
| 50 | + /** @var IGroupManager */ |
|
| 51 | + private $groupManager; |
|
| 52 | + /** @var IConfig */ |
|
| 53 | + private $config; |
|
| 54 | + /** @var IUserSession */ |
|
| 55 | + private $userSession; |
|
| 56 | + |
|
| 57 | + public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 58 | + $this->groupManager = $groupManager; |
|
| 59 | + $this->config = $config; |
|
| 60 | + $this->userSession = $userSession; |
|
| 61 | + |
|
| 62 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 63 | + $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 64 | + $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
|
| 65 | + $this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no'; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 69 | + if ($this->groupSharingDisabled) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $hasMoreResults = false; |
|
| 74 | + $result = ['wide' => [], 'exact' => []]; |
|
| 75 | + |
|
| 76 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 77 | + $groupIds = array_map(function (IGroup $group) { |
|
| 78 | + return $group->getGID(); |
|
| 79 | + }, $groups); |
|
| 80 | + |
|
| 81 | + if (!$this->shareeEnumeration || count($groups) < $limit) { |
|
| 82 | + $hasMoreResults = true; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $userGroups = []; |
|
| 86 | + if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) { |
|
| 87 | + // Intersect all the groups that match with the groups this user is a member of |
|
| 88 | + $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); |
|
| 89 | + $userGroups = array_map(function (IGroup $group) { |
|
| 90 | + return $group->getGID(); |
|
| 91 | + }, $userGroups); |
|
| 92 | + $groupIds = array_intersect($groupIds, $userGroups); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $lowerSearch = strtolower($search); |
|
| 96 | + foreach ($groups as $group) { |
|
| 97 | + if ($group->hideFromCollaboration()) { |
|
| 98 | + continue; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // FIXME: use a more efficient approach |
|
| 102 | + $gid = $group->getGID(); |
|
| 103 | + if (!in_array($gid, $groupIds)) { |
|
| 104 | + continue; |
|
| 105 | + } |
|
| 106 | + if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) { |
|
| 107 | + $result['exact'][] = [ |
|
| 108 | + 'label' => $group->getDisplayName(), |
|
| 109 | + 'value' => [ |
|
| 110 | + 'shareType' => IShare::TYPE_GROUP, |
|
| 111 | + 'shareWith' => $gid, |
|
| 112 | + ], |
|
| 113 | + ]; |
|
| 114 | + } else { |
|
| 115 | + if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) { |
|
| 116 | + continue; |
|
| 117 | + } |
|
| 118 | + $result['wide'][] = [ |
|
| 119 | + 'label' => $group->getDisplayName(), |
|
| 120 | + 'value' => [ |
|
| 121 | + 'shareType' => IShare::TYPE_GROUP, |
|
| 122 | + 'shareWith' => $gid, |
|
| 123 | + ], |
|
| 124 | + ]; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ($offset === 0 && empty($result['exact'])) { |
|
| 129 | + // On page one we try if the search result has a direct hit on the |
|
| 130 | + // user id and if so, we add that to the exact match list |
|
| 131 | + $group = $this->groupManager->get($search); |
|
| 132 | + if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) { |
|
| 133 | + $result['exact'][] = [ |
|
| 134 | + 'label' => $group->getDisplayName(), |
|
| 135 | + 'value' => [ |
|
| 136 | + 'shareType' => IShare::TYPE_GROUP, |
|
| 137 | + 'shareWith' => $group->getGID(), |
|
| 138 | + ], |
|
| 139 | + ]; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if (!$this->shareeEnumeration) { |
|
| 144 | + $result['wide'] = []; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $type = new SearchResultType('groups'); |
|
| 148 | + $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
|
| 149 | + |
|
| 150 | + return $hasMoreResults; |
|
| 151 | + } |
|
| 152 | 152 | } |