@@ -174,14 +174,14 @@ discard block |
||
| 174 | 174 | return; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 177 | + $ns = '{'.$this::NS_OWNCLOUD.'}'; |
|
| 178 | 178 | $requestedProps = []; |
| 179 | 179 | $filterRules = []; |
| 180 | 180 | |
| 181 | 181 | // parse report properties and gather filter info |
| 182 | 182 | foreach ($report as $reportProps) { |
| 183 | 183 | $name = $reportProps['name']; |
| 184 | - if ($name === $ns . 'filter-rules') { |
|
| 184 | + if ($name === $ns.'filter-rules') { |
|
| 185 | 185 | $filterRules = $reportProps['value']; |
| 186 | 186 | } else if ($name === '{DAV:}prop') { |
| 187 | 187 | // propfind properties |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | if (empty($filesUri)) { |
| 243 | 243 | return ''; |
| 244 | 244 | } |
| 245 | - return '/' . $filesUri; |
|
| 245 | + return '/'.$filesUri; |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** |
@@ -254,19 +254,19 @@ discard block |
||
| 254 | 254 | * @throws TagNotFoundException whenever a tag was not found |
| 255 | 255 | */ |
| 256 | 256 | protected function processFilterRules($filterRules) { |
| 257 | - $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 257 | + $ns = '{'.$this::NS_OWNCLOUD.'}'; |
|
| 258 | 258 | $resultFileIds = null; |
| 259 | 259 | $systemTagIds = []; |
| 260 | 260 | $circlesIds = []; |
| 261 | 261 | $favoriteFilter = null; |
| 262 | 262 | foreach ($filterRules as $filterRule) { |
| 263 | - if ($filterRule['name'] === $ns . 'systemtag') { |
|
| 263 | + if ($filterRule['name'] === $ns.'systemtag') { |
|
| 264 | 264 | $systemTagIds[] = $filterRule['value']; |
| 265 | 265 | } |
| 266 | 266 | if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) { |
| 267 | 267 | $circlesIds[] = $filterRule['value']; |
| 268 | 268 | } |
| 269 | - if ($filterRule['name'] === $ns . 'favorite') { |
|
| 269 | + if ($filterRule['name'] === $ns.'favorite') { |
|
| 270 | 270 | $favoriteFilter = true; |
| 271 | 271 | } |
| 272 | 272 | |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | if (!empty($unknownTagIds)) { |
| 318 | - throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found'); |
|
| 318 | + throw new TagNotFoundException('Tag with ids '.implode(', ', $unknownTagIds).' not found'); |
|
| 319 | 319 | } |
| 320 | 320 | } |
| 321 | 321 | |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | public function prepareResponses($filesUri, $requestedProps, $nodes) { |
| 368 | 368 | $responses = []; |
| 369 | 369 | foreach ($nodes as $node) { |
| 370 | - $propFind = new PropFind($filesUri . $node->getPath(), $requestedProps); |
|
| 370 | + $propFind = new PropFind($filesUri.$node->getPath(), $requestedProps); |
|
| 371 | 371 | |
| 372 | 372 | $this->server->getPropertiesByNode($propFind, $node); |
| 373 | 373 | // copied from Sabre Server's getPropertiesForPath |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | $responses[] = new Response( |
| 383 | - rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(), |
|
| 383 | + rtrim($this->server->getBaseUri(), '/').$filesUri.$node->getPath(), |
|
| 384 | 384 | $result, |
| 385 | 385 | 200 |
| 386 | 386 | ); |
@@ -43,398 +43,398 @@ |
||
| 43 | 43 | |
| 44 | 44 | class FilesReportPlugin extends ServerPlugin { |
| 45 | 45 | |
| 46 | - // namespace |
|
| 47 | - const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 48 | - const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; |
|
| 49 | - const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; |
|
| 50 | - const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Reference to main server object |
|
| 54 | - * |
|
| 55 | - * @var \Sabre\DAV\Server |
|
| 56 | - */ |
|
| 57 | - private $server; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var Tree |
|
| 61 | - */ |
|
| 62 | - private $tree; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var View |
|
| 66 | - */ |
|
| 67 | - private $fileView; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var ISystemTagManager |
|
| 71 | - */ |
|
| 72 | - private $tagManager; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var ISystemTagObjectMapper |
|
| 76 | - */ |
|
| 77 | - private $tagMapper; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Manager for private tags |
|
| 81 | - * |
|
| 82 | - * @var ITagManager |
|
| 83 | - */ |
|
| 84 | - private $fileTagger; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @var IUserSession |
|
| 88 | - */ |
|
| 89 | - private $userSession; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @var IGroupManager |
|
| 93 | - */ |
|
| 94 | - private $groupManager; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @var Folder |
|
| 98 | - */ |
|
| 99 | - private $userFolder; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @var IAppManager |
|
| 103 | - */ |
|
| 104 | - private $appManager; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param Tree $tree |
|
| 108 | - * @param View $view |
|
| 109 | - * @param ISystemTagManager $tagManager |
|
| 110 | - * @param ISystemTagObjectMapper $tagMapper |
|
| 111 | - * @param ITagManager $fileTagger manager for private tags |
|
| 112 | - * @param IUserSession $userSession |
|
| 113 | - * @param IGroupManager $groupManager |
|
| 114 | - * @param Folder $userFolder |
|
| 115 | - * @param IAppManager $appManager |
|
| 116 | - */ |
|
| 117 | - public function __construct(Tree $tree, |
|
| 118 | - View $view, |
|
| 119 | - ISystemTagManager $tagManager, |
|
| 120 | - ISystemTagObjectMapper $tagMapper, |
|
| 121 | - ITagManager $fileTagger, |
|
| 122 | - IUserSession $userSession, |
|
| 123 | - IGroupManager $groupManager, |
|
| 124 | - Folder $userFolder, |
|
| 125 | - IAppManager $appManager |
|
| 126 | - ) { |
|
| 127 | - $this->tree = $tree; |
|
| 128 | - $this->fileView = $view; |
|
| 129 | - $this->tagManager = $tagManager; |
|
| 130 | - $this->tagMapper = $tagMapper; |
|
| 131 | - $this->fileTagger = $fileTagger; |
|
| 132 | - $this->userSession = $userSession; |
|
| 133 | - $this->groupManager = $groupManager; |
|
| 134 | - $this->userFolder = $userFolder; |
|
| 135 | - $this->appManager = $appManager; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * This initializes the plugin. |
|
| 140 | - * |
|
| 141 | - * This function is called by \Sabre\DAV\Server, after |
|
| 142 | - * addPlugin is called. |
|
| 143 | - * |
|
| 144 | - * This method should set up the required event subscriptions. |
|
| 145 | - * |
|
| 146 | - * @param \Sabre\DAV\Server $server |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public function initialize(\Sabre\DAV\Server $server) { |
|
| 150 | - |
|
| 151 | - $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 152 | - |
|
| 153 | - $this->server = $server; |
|
| 154 | - $this->server->on('report', array($this, 'onReport')); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Returns a list of reports this plugin supports. |
|
| 159 | - * |
|
| 160 | - * This will be used in the {DAV:}supported-report-set property. |
|
| 161 | - * |
|
| 162 | - * @param string $uri |
|
| 163 | - * @return array |
|
| 164 | - */ |
|
| 165 | - public function getSupportedReportSet($uri) { |
|
| 166 | - return [self::REPORT_NAME]; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * REPORT operations to look for files |
|
| 171 | - * |
|
| 172 | - * @param string $reportName |
|
| 173 | - * @param $report |
|
| 174 | - * @param string $uri |
|
| 175 | - * @return bool |
|
| 176 | - * @throws BadRequest |
|
| 177 | - * @throws PreconditionFailed |
|
| 178 | - * @internal param $ [] $report |
|
| 179 | - */ |
|
| 180 | - public function onReport($reportName, $report, $uri) { |
|
| 181 | - $reportTargetNode = $this->server->tree->getNodeForPath($uri); |
|
| 182 | - if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) { |
|
| 183 | - return; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 187 | - $requestedProps = []; |
|
| 188 | - $filterRules = []; |
|
| 189 | - |
|
| 190 | - // parse report properties and gather filter info |
|
| 191 | - foreach ($report as $reportProps) { |
|
| 192 | - $name = $reportProps['name']; |
|
| 193 | - if ($name === $ns . 'filter-rules') { |
|
| 194 | - $filterRules = $reportProps['value']; |
|
| 195 | - } else if ($name === '{DAV:}prop') { |
|
| 196 | - // propfind properties |
|
| 197 | - foreach ($reportProps['value'] as $propVal) { |
|
| 198 | - $requestedProps[] = $propVal['name']; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - if (empty($filterRules)) { |
|
| 204 | - // an empty filter would return all existing files which would be slow |
|
| 205 | - throw new BadRequest('Missing filter-rule block in request'); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - // gather all file ids matching filter |
|
| 209 | - try { |
|
| 210 | - $resultFileIds = $this->processFilterRules($filterRules); |
|
| 211 | - } catch (TagNotFoundException $e) { |
|
| 212 | - throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // find sabre nodes by file id, restricted to the root node path |
|
| 216 | - $results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds); |
|
| 217 | - |
|
| 218 | - $filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath()); |
|
| 219 | - $responses = $this->prepareResponses($filesUri, $requestedProps, $results); |
|
| 220 | - |
|
| 221 | - $xml = $this->server->xml->write( |
|
| 222 | - '{DAV:}multistatus', |
|
| 223 | - new MultiStatus($responses) |
|
| 224 | - ); |
|
| 225 | - |
|
| 226 | - $this->server->httpResponse->setStatus(207); |
|
| 227 | - $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 228 | - $this->server->httpResponse->setBody($xml); |
|
| 229 | - |
|
| 230 | - return false; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Returns the base uri of the files root by removing |
|
| 235 | - * the subpath from the URI |
|
| 236 | - * |
|
| 237 | - * @param string $uri URI from this request |
|
| 238 | - * @param string $subPath subpath to remove from the URI |
|
| 239 | - * |
|
| 240 | - * @return string files base uri |
|
| 241 | - */ |
|
| 242 | - private function getFilesBaseUri($uri, $subPath) { |
|
| 243 | - $uri = trim($uri, '/'); |
|
| 244 | - $subPath = trim($subPath, '/'); |
|
| 245 | - if (empty($subPath)) { |
|
| 246 | - $filesUri = $uri; |
|
| 247 | - } else { |
|
| 248 | - $filesUri = substr($uri, 0, strlen($uri) - strlen($subPath)); |
|
| 249 | - } |
|
| 250 | - $filesUri = trim($filesUri, '/'); |
|
| 251 | - if (empty($filesUri)) { |
|
| 252 | - return ''; |
|
| 253 | - } |
|
| 254 | - return '/' . $filesUri; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Find file ids matching the given filter rules |
|
| 259 | - * |
|
| 260 | - * @param array $filterRules |
|
| 261 | - * @return array array of unique file id results |
|
| 262 | - * |
|
| 263 | - * @throws TagNotFoundException whenever a tag was not found |
|
| 264 | - */ |
|
| 265 | - protected function processFilterRules($filterRules) { |
|
| 266 | - $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 267 | - $resultFileIds = null; |
|
| 268 | - $systemTagIds = []; |
|
| 269 | - $circlesIds = []; |
|
| 270 | - $favoriteFilter = null; |
|
| 271 | - foreach ($filterRules as $filterRule) { |
|
| 272 | - if ($filterRule['name'] === $ns . 'systemtag') { |
|
| 273 | - $systemTagIds[] = $filterRule['value']; |
|
| 274 | - } |
|
| 275 | - if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) { |
|
| 276 | - $circlesIds[] = $filterRule['value']; |
|
| 277 | - } |
|
| 278 | - if ($filterRule['name'] === $ns . 'favorite') { |
|
| 279 | - $favoriteFilter = true; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - if ($favoriteFilter !== null) { |
|
| 285 | - $resultFileIds = $this->fileTagger->load('files')->getFavorites(); |
|
| 286 | - if (empty($resultFileIds)) { |
|
| 287 | - return []; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if (!empty($systemTagIds)) { |
|
| 292 | - $fileIds = $this->getSystemTagFileIds($systemTagIds); |
|
| 293 | - if (empty($resultFileIds)) { |
|
| 294 | - $resultFileIds = $fileIds; |
|
| 295 | - } else { |
|
| 296 | - $resultFileIds = array_intersect($fileIds, $resultFileIds); |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - if (!empty($circlesIds)) { |
|
| 301 | - $fileIds = $this->getCirclesFileIds($circlesIds); |
|
| 302 | - if (empty($resultFileIds)) { |
|
| 303 | - $resultFileIds = $fileIds; |
|
| 304 | - } else { |
|
| 305 | - $resultFileIds = array_intersect($fileIds, $resultFileIds); |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - return $resultFileIds; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - private function getSystemTagFileIds($systemTagIds) { |
|
| 313 | - $resultFileIds = null; |
|
| 314 | - |
|
| 315 | - // check user permissions, if applicable |
|
| 316 | - if (!$this->isAdmin()) { |
|
| 317 | - // check visibility/permission |
|
| 318 | - $tags = $this->tagManager->getTagsByIds($systemTagIds); |
|
| 319 | - $unknownTagIds = []; |
|
| 320 | - foreach ($tags as $tag) { |
|
| 321 | - if (!$tag->isUserVisible()) { |
|
| 322 | - $unknownTagIds[] = $tag->getId(); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - if (!empty($unknownTagIds)) { |
|
| 327 | - throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found'); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // fetch all file ids and intersect them |
|
| 332 | - foreach ($systemTagIds as $systemTagId) { |
|
| 333 | - $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files'); |
|
| 334 | - |
|
| 335 | - if (empty($fileIds)) { |
|
| 336 | - // This tag has no files, nothing can ever show up |
|
| 337 | - return []; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // first run ? |
|
| 341 | - if ($resultFileIds === null) { |
|
| 342 | - $resultFileIds = $fileIds; |
|
| 343 | - } else { |
|
| 344 | - $resultFileIds = array_intersect($resultFileIds, $fileIds); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - if (empty($resultFileIds)) { |
|
| 348 | - // Empty intersection, nothing can show up anymore |
|
| 349 | - return []; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - return $resultFileIds; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * @suppress PhanUndeclaredClassMethod |
|
| 357 | - * @param array $circlesIds |
|
| 358 | - * @return array |
|
| 359 | - */ |
|
| 360 | - private function getCirclesFileIds(array $circlesIds) { |
|
| 361 | - if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 362 | - return []; |
|
| 363 | - } |
|
| 364 | - return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Prepare propfind response for the given nodes |
|
| 370 | - * |
|
| 371 | - * @param string $filesUri $filesUri URI leading to root of the files URI, |
|
| 372 | - * with a leading slash but no trailing slash |
|
| 373 | - * @param string[] $requestedProps requested properties |
|
| 374 | - * @param Node[] nodes nodes for which to fetch and prepare responses |
|
| 375 | - * @return Response[] |
|
| 376 | - */ |
|
| 377 | - public function prepareResponses($filesUri, $requestedProps, $nodes) { |
|
| 378 | - $responses = []; |
|
| 379 | - foreach ($nodes as $node) { |
|
| 380 | - $propFind = new PropFind($filesUri . $node->getPath(), $requestedProps); |
|
| 381 | - |
|
| 382 | - $this->server->getPropertiesByNode($propFind, $node); |
|
| 383 | - // copied from Sabre Server's getPropertiesForPath |
|
| 384 | - $result = $propFind->getResultForMultiStatus(); |
|
| 385 | - $result['href'] = $propFind->getPath(); |
|
| 386 | - |
|
| 387 | - $resourceType = $this->server->getResourceTypeForNode($node); |
|
| 388 | - if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) { |
|
| 389 | - $result['href'] .= '/'; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $responses[] = new Response( |
|
| 393 | - rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(), |
|
| 394 | - $result, |
|
| 395 | - 200 |
|
| 396 | - ); |
|
| 397 | - } |
|
| 398 | - return $responses; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Find Sabre nodes by file ids |
|
| 403 | - * |
|
| 404 | - * @param Node $rootNode root node for search |
|
| 405 | - * @param array $fileIds file ids |
|
| 406 | - * @return Node[] array of Sabre nodes |
|
| 407 | - */ |
|
| 408 | - public function findNodesByFileIds($rootNode, $fileIds) { |
|
| 409 | - $folder = $this->userFolder; |
|
| 410 | - if (trim($rootNode->getPath(), '/') !== '') { |
|
| 411 | - $folder = $folder->get($rootNode->getPath()); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - $results = []; |
|
| 415 | - foreach ($fileIds as $fileId) { |
|
| 416 | - $entry = $folder->getById($fileId); |
|
| 417 | - if ($entry) { |
|
| 418 | - $entry = current($entry); |
|
| 419 | - if ($entry instanceof \OCP\Files\File) { |
|
| 420 | - $results[] = new File($this->fileView, $entry); |
|
| 421 | - } else if ($entry instanceof \OCP\Files\Folder) { |
|
| 422 | - $results[] = new Directory($this->fileView, $entry); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $results; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Returns whether the currently logged in user is an administrator |
|
| 432 | - */ |
|
| 433 | - private function isAdmin() { |
|
| 434 | - $user = $this->userSession->getUser(); |
|
| 435 | - if ($user !== null) { |
|
| 436 | - return $this->groupManager->isAdmin($user->getUID()); |
|
| 437 | - } |
|
| 438 | - return false; |
|
| 439 | - } |
|
| 46 | + // namespace |
|
| 47 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 48 | + const REPORT_NAME = '{http://owncloud.org/ns}filter-files'; |
|
| 49 | + const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag'; |
|
| 50 | + const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Reference to main server object |
|
| 54 | + * |
|
| 55 | + * @var \Sabre\DAV\Server |
|
| 56 | + */ |
|
| 57 | + private $server; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var Tree |
|
| 61 | + */ |
|
| 62 | + private $tree; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var View |
|
| 66 | + */ |
|
| 67 | + private $fileView; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var ISystemTagManager |
|
| 71 | + */ |
|
| 72 | + private $tagManager; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var ISystemTagObjectMapper |
|
| 76 | + */ |
|
| 77 | + private $tagMapper; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Manager for private tags |
|
| 81 | + * |
|
| 82 | + * @var ITagManager |
|
| 83 | + */ |
|
| 84 | + private $fileTagger; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @var IUserSession |
|
| 88 | + */ |
|
| 89 | + private $userSession; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @var IGroupManager |
|
| 93 | + */ |
|
| 94 | + private $groupManager; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @var Folder |
|
| 98 | + */ |
|
| 99 | + private $userFolder; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @var IAppManager |
|
| 103 | + */ |
|
| 104 | + private $appManager; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param Tree $tree |
|
| 108 | + * @param View $view |
|
| 109 | + * @param ISystemTagManager $tagManager |
|
| 110 | + * @param ISystemTagObjectMapper $tagMapper |
|
| 111 | + * @param ITagManager $fileTagger manager for private tags |
|
| 112 | + * @param IUserSession $userSession |
|
| 113 | + * @param IGroupManager $groupManager |
|
| 114 | + * @param Folder $userFolder |
|
| 115 | + * @param IAppManager $appManager |
|
| 116 | + */ |
|
| 117 | + public function __construct(Tree $tree, |
|
| 118 | + View $view, |
|
| 119 | + ISystemTagManager $tagManager, |
|
| 120 | + ISystemTagObjectMapper $tagMapper, |
|
| 121 | + ITagManager $fileTagger, |
|
| 122 | + IUserSession $userSession, |
|
| 123 | + IGroupManager $groupManager, |
|
| 124 | + Folder $userFolder, |
|
| 125 | + IAppManager $appManager |
|
| 126 | + ) { |
|
| 127 | + $this->tree = $tree; |
|
| 128 | + $this->fileView = $view; |
|
| 129 | + $this->tagManager = $tagManager; |
|
| 130 | + $this->tagMapper = $tagMapper; |
|
| 131 | + $this->fileTagger = $fileTagger; |
|
| 132 | + $this->userSession = $userSession; |
|
| 133 | + $this->groupManager = $groupManager; |
|
| 134 | + $this->userFolder = $userFolder; |
|
| 135 | + $this->appManager = $appManager; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * This initializes the plugin. |
|
| 140 | + * |
|
| 141 | + * This function is called by \Sabre\DAV\Server, after |
|
| 142 | + * addPlugin is called. |
|
| 143 | + * |
|
| 144 | + * This method should set up the required event subscriptions. |
|
| 145 | + * |
|
| 146 | + * @param \Sabre\DAV\Server $server |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public function initialize(\Sabre\DAV\Server $server) { |
|
| 150 | + |
|
| 151 | + $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 152 | + |
|
| 153 | + $this->server = $server; |
|
| 154 | + $this->server->on('report', array($this, 'onReport')); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Returns a list of reports this plugin supports. |
|
| 159 | + * |
|
| 160 | + * This will be used in the {DAV:}supported-report-set property. |
|
| 161 | + * |
|
| 162 | + * @param string $uri |
|
| 163 | + * @return array |
|
| 164 | + */ |
|
| 165 | + public function getSupportedReportSet($uri) { |
|
| 166 | + return [self::REPORT_NAME]; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * REPORT operations to look for files |
|
| 171 | + * |
|
| 172 | + * @param string $reportName |
|
| 173 | + * @param $report |
|
| 174 | + * @param string $uri |
|
| 175 | + * @return bool |
|
| 176 | + * @throws BadRequest |
|
| 177 | + * @throws PreconditionFailed |
|
| 178 | + * @internal param $ [] $report |
|
| 179 | + */ |
|
| 180 | + public function onReport($reportName, $report, $uri) { |
|
| 181 | + $reportTargetNode = $this->server->tree->getNodeForPath($uri); |
|
| 182 | + if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) { |
|
| 183 | + return; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 187 | + $requestedProps = []; |
|
| 188 | + $filterRules = []; |
|
| 189 | + |
|
| 190 | + // parse report properties and gather filter info |
|
| 191 | + foreach ($report as $reportProps) { |
|
| 192 | + $name = $reportProps['name']; |
|
| 193 | + if ($name === $ns . 'filter-rules') { |
|
| 194 | + $filterRules = $reportProps['value']; |
|
| 195 | + } else if ($name === '{DAV:}prop') { |
|
| 196 | + // propfind properties |
|
| 197 | + foreach ($reportProps['value'] as $propVal) { |
|
| 198 | + $requestedProps[] = $propVal['name']; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + if (empty($filterRules)) { |
|
| 204 | + // an empty filter would return all existing files which would be slow |
|
| 205 | + throw new BadRequest('Missing filter-rule block in request'); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + // gather all file ids matching filter |
|
| 209 | + try { |
|
| 210 | + $resultFileIds = $this->processFilterRules($filterRules); |
|
| 211 | + } catch (TagNotFoundException $e) { |
|
| 212 | + throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // find sabre nodes by file id, restricted to the root node path |
|
| 216 | + $results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds); |
|
| 217 | + |
|
| 218 | + $filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath()); |
|
| 219 | + $responses = $this->prepareResponses($filesUri, $requestedProps, $results); |
|
| 220 | + |
|
| 221 | + $xml = $this->server->xml->write( |
|
| 222 | + '{DAV:}multistatus', |
|
| 223 | + new MultiStatus($responses) |
|
| 224 | + ); |
|
| 225 | + |
|
| 226 | + $this->server->httpResponse->setStatus(207); |
|
| 227 | + $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
|
| 228 | + $this->server->httpResponse->setBody($xml); |
|
| 229 | + |
|
| 230 | + return false; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Returns the base uri of the files root by removing |
|
| 235 | + * the subpath from the URI |
|
| 236 | + * |
|
| 237 | + * @param string $uri URI from this request |
|
| 238 | + * @param string $subPath subpath to remove from the URI |
|
| 239 | + * |
|
| 240 | + * @return string files base uri |
|
| 241 | + */ |
|
| 242 | + private function getFilesBaseUri($uri, $subPath) { |
|
| 243 | + $uri = trim($uri, '/'); |
|
| 244 | + $subPath = trim($subPath, '/'); |
|
| 245 | + if (empty($subPath)) { |
|
| 246 | + $filesUri = $uri; |
|
| 247 | + } else { |
|
| 248 | + $filesUri = substr($uri, 0, strlen($uri) - strlen($subPath)); |
|
| 249 | + } |
|
| 250 | + $filesUri = trim($filesUri, '/'); |
|
| 251 | + if (empty($filesUri)) { |
|
| 252 | + return ''; |
|
| 253 | + } |
|
| 254 | + return '/' . $filesUri; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Find file ids matching the given filter rules |
|
| 259 | + * |
|
| 260 | + * @param array $filterRules |
|
| 261 | + * @return array array of unique file id results |
|
| 262 | + * |
|
| 263 | + * @throws TagNotFoundException whenever a tag was not found |
|
| 264 | + */ |
|
| 265 | + protected function processFilterRules($filterRules) { |
|
| 266 | + $ns = '{' . $this::NS_OWNCLOUD . '}'; |
|
| 267 | + $resultFileIds = null; |
|
| 268 | + $systemTagIds = []; |
|
| 269 | + $circlesIds = []; |
|
| 270 | + $favoriteFilter = null; |
|
| 271 | + foreach ($filterRules as $filterRule) { |
|
| 272 | + if ($filterRule['name'] === $ns . 'systemtag') { |
|
| 273 | + $systemTagIds[] = $filterRule['value']; |
|
| 274 | + } |
|
| 275 | + if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) { |
|
| 276 | + $circlesIds[] = $filterRule['value']; |
|
| 277 | + } |
|
| 278 | + if ($filterRule['name'] === $ns . 'favorite') { |
|
| 279 | + $favoriteFilter = true; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + if ($favoriteFilter !== null) { |
|
| 285 | + $resultFileIds = $this->fileTagger->load('files')->getFavorites(); |
|
| 286 | + if (empty($resultFileIds)) { |
|
| 287 | + return []; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if (!empty($systemTagIds)) { |
|
| 292 | + $fileIds = $this->getSystemTagFileIds($systemTagIds); |
|
| 293 | + if (empty($resultFileIds)) { |
|
| 294 | + $resultFileIds = $fileIds; |
|
| 295 | + } else { |
|
| 296 | + $resultFileIds = array_intersect($fileIds, $resultFileIds); |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + if (!empty($circlesIds)) { |
|
| 301 | + $fileIds = $this->getCirclesFileIds($circlesIds); |
|
| 302 | + if (empty($resultFileIds)) { |
|
| 303 | + $resultFileIds = $fileIds; |
|
| 304 | + } else { |
|
| 305 | + $resultFileIds = array_intersect($fileIds, $resultFileIds); |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + return $resultFileIds; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + private function getSystemTagFileIds($systemTagIds) { |
|
| 313 | + $resultFileIds = null; |
|
| 314 | + |
|
| 315 | + // check user permissions, if applicable |
|
| 316 | + if (!$this->isAdmin()) { |
|
| 317 | + // check visibility/permission |
|
| 318 | + $tags = $this->tagManager->getTagsByIds($systemTagIds); |
|
| 319 | + $unknownTagIds = []; |
|
| 320 | + foreach ($tags as $tag) { |
|
| 321 | + if (!$tag->isUserVisible()) { |
|
| 322 | + $unknownTagIds[] = $tag->getId(); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + if (!empty($unknownTagIds)) { |
|
| 327 | + throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found'); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // fetch all file ids and intersect them |
|
| 332 | + foreach ($systemTagIds as $systemTagId) { |
|
| 333 | + $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files'); |
|
| 334 | + |
|
| 335 | + if (empty($fileIds)) { |
|
| 336 | + // This tag has no files, nothing can ever show up |
|
| 337 | + return []; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // first run ? |
|
| 341 | + if ($resultFileIds === null) { |
|
| 342 | + $resultFileIds = $fileIds; |
|
| 343 | + } else { |
|
| 344 | + $resultFileIds = array_intersect($resultFileIds, $fileIds); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + if (empty($resultFileIds)) { |
|
| 348 | + // Empty intersection, nothing can show up anymore |
|
| 349 | + return []; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + return $resultFileIds; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * @suppress PhanUndeclaredClassMethod |
|
| 357 | + * @param array $circlesIds |
|
| 358 | + * @return array |
|
| 359 | + */ |
|
| 360 | + private function getCirclesFileIds(array $circlesIds) { |
|
| 361 | + if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 362 | + return []; |
|
| 363 | + } |
|
| 364 | + return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Prepare propfind response for the given nodes |
|
| 370 | + * |
|
| 371 | + * @param string $filesUri $filesUri URI leading to root of the files URI, |
|
| 372 | + * with a leading slash but no trailing slash |
|
| 373 | + * @param string[] $requestedProps requested properties |
|
| 374 | + * @param Node[] nodes nodes for which to fetch and prepare responses |
|
| 375 | + * @return Response[] |
|
| 376 | + */ |
|
| 377 | + public function prepareResponses($filesUri, $requestedProps, $nodes) { |
|
| 378 | + $responses = []; |
|
| 379 | + foreach ($nodes as $node) { |
|
| 380 | + $propFind = new PropFind($filesUri . $node->getPath(), $requestedProps); |
|
| 381 | + |
|
| 382 | + $this->server->getPropertiesByNode($propFind, $node); |
|
| 383 | + // copied from Sabre Server's getPropertiesForPath |
|
| 384 | + $result = $propFind->getResultForMultiStatus(); |
|
| 385 | + $result['href'] = $propFind->getPath(); |
|
| 386 | + |
|
| 387 | + $resourceType = $this->server->getResourceTypeForNode($node); |
|
| 388 | + if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) { |
|
| 389 | + $result['href'] .= '/'; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $responses[] = new Response( |
|
| 393 | + rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(), |
|
| 394 | + $result, |
|
| 395 | + 200 |
|
| 396 | + ); |
|
| 397 | + } |
|
| 398 | + return $responses; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Find Sabre nodes by file ids |
|
| 403 | + * |
|
| 404 | + * @param Node $rootNode root node for search |
|
| 405 | + * @param array $fileIds file ids |
|
| 406 | + * @return Node[] array of Sabre nodes |
|
| 407 | + */ |
|
| 408 | + public function findNodesByFileIds($rootNode, $fileIds) { |
|
| 409 | + $folder = $this->userFolder; |
|
| 410 | + if (trim($rootNode->getPath(), '/') !== '') { |
|
| 411 | + $folder = $folder->get($rootNode->getPath()); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + $results = []; |
|
| 415 | + foreach ($fileIds as $fileId) { |
|
| 416 | + $entry = $folder->getById($fileId); |
|
| 417 | + if ($entry) { |
|
| 418 | + $entry = current($entry); |
|
| 419 | + if ($entry instanceof \OCP\Files\File) { |
|
| 420 | + $results[] = new File($this->fileView, $entry); |
|
| 421 | + } else if ($entry instanceof \OCP\Files\Folder) { |
|
| 422 | + $results[] = new Directory($this->fileView, $entry); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $results; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Returns whether the currently logged in user is an administrator |
|
| 432 | + */ |
|
| 433 | + private function isAdmin() { |
|
| 434 | + $user = $this->userSession->getUser(); |
|
| 435 | + if ($user !== null) { |
|
| 436 | + return $this->groupManager->isAdmin($user->getUID()); |
|
| 437 | + } |
|
| 438 | + return false; |
|
| 439 | + } |
|
| 440 | 440 | } |
@@ -50,427 +50,427 @@ |
||
| 50 | 50 | |
| 51 | 51 | class Principal implements BackendInterface { |
| 52 | 52 | |
| 53 | - /** @var IUserManager */ |
|
| 54 | - private $userManager; |
|
| 55 | - |
|
| 56 | - /** @var IGroupManager */ |
|
| 57 | - private $groupManager; |
|
| 58 | - |
|
| 59 | - /** @var IShareManager */ |
|
| 60 | - private $shareManager; |
|
| 61 | - |
|
| 62 | - /** @var IUserSession */ |
|
| 63 | - private $userSession; |
|
| 64 | - |
|
| 65 | - /** @var IConfig */ |
|
| 66 | - private $config; |
|
| 67 | - |
|
| 68 | - /** @var IAppManager */ |
|
| 69 | - private $appManager; |
|
| 70 | - |
|
| 71 | - /** @var string */ |
|
| 72 | - private $principalPrefix; |
|
| 73 | - |
|
| 74 | - /** @var bool */ |
|
| 75 | - private $hasGroups; |
|
| 76 | - |
|
| 77 | - /** @var bool */ |
|
| 78 | - private $hasCircles; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param IUserManager $userManager |
|
| 82 | - * @param IGroupManager $groupManager |
|
| 83 | - * @param IShareManager $shareManager |
|
| 84 | - * @param IUserSession $userSession |
|
| 85 | - * @param IConfig $config |
|
| 86 | - * @param string $principalPrefix |
|
| 87 | - */ |
|
| 88 | - public function __construct(IUserManager $userManager, |
|
| 89 | - IGroupManager $groupManager, |
|
| 90 | - IShareManager $shareManager, |
|
| 91 | - IUserSession $userSession, |
|
| 92 | - IConfig $config, |
|
| 93 | - IAppManager $appManager, |
|
| 94 | - $principalPrefix = 'principals/users/') { |
|
| 95 | - $this->userManager = $userManager; |
|
| 96 | - $this->groupManager = $groupManager; |
|
| 97 | - $this->shareManager = $shareManager; |
|
| 98 | - $this->userSession = $userSession; |
|
| 99 | - $this->config = $config; |
|
| 100 | - $this->appManager = $appManager; |
|
| 101 | - $this->principalPrefix = trim($principalPrefix, '/'); |
|
| 102 | - $this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/'); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Returns a list of principals based on a prefix. |
|
| 107 | - * |
|
| 108 | - * This prefix will often contain something like 'principals'. You are only |
|
| 109 | - * expected to return principals that are in this base path. |
|
| 110 | - * |
|
| 111 | - * You are expected to return at least a 'uri' for every user, you can |
|
| 112 | - * return any additional properties if you wish so. Common properties are: |
|
| 113 | - * {DAV:}displayname |
|
| 114 | - * |
|
| 115 | - * @param string $prefixPath |
|
| 116 | - * @return string[] |
|
| 117 | - */ |
|
| 118 | - public function getPrincipalsByPrefix($prefixPath) { |
|
| 119 | - $principals = []; |
|
| 120 | - |
|
| 121 | - if ($prefixPath === $this->principalPrefix) { |
|
| 122 | - foreach($this->userManager->search('') as $user) { |
|
| 123 | - $principals[] = $this->userToPrincipal($user); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $principals; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Returns a specific principal, specified by it's path. |
|
| 132 | - * The returned structure should be the exact same as from |
|
| 133 | - * getPrincipalsByPrefix. |
|
| 134 | - * |
|
| 135 | - * @param string $path |
|
| 136 | - * @return array |
|
| 137 | - */ |
|
| 138 | - public function getPrincipalByPath($path) { |
|
| 139 | - list($prefix, $name) = \Sabre\Uri\split($path); |
|
| 140 | - |
|
| 141 | - if ($prefix === $this->principalPrefix) { |
|
| 142 | - $user = $this->userManager->get($name); |
|
| 143 | - |
|
| 144 | - if ($user !== null) { |
|
| 145 | - return $this->userToPrincipal($user); |
|
| 146 | - } |
|
| 147 | - } else if ($prefix === 'principals/circles') { |
|
| 148 | - try { |
|
| 149 | - return $this->circleToPrincipal($name); |
|
| 150 | - } catch (QueryException $e) { |
|
| 151 | - return null; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - return null; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Returns the list of members for a group-principal |
|
| 159 | - * |
|
| 160 | - * @param string $principal |
|
| 161 | - * @return string[] |
|
| 162 | - * @throws Exception |
|
| 163 | - */ |
|
| 164 | - public function getGroupMemberSet($principal) { |
|
| 165 | - // TODO: for now the group principal has only one member, the user itself |
|
| 166 | - $principal = $this->getPrincipalByPath($principal); |
|
| 167 | - if (!$principal) { |
|
| 168 | - throw new Exception('Principal not found'); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return [$principal['uri']]; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Returns the list of groups a principal is a member of |
|
| 176 | - * |
|
| 177 | - * @param string $principal |
|
| 178 | - * @param bool $needGroups |
|
| 179 | - * @return array |
|
| 180 | - * @throws Exception |
|
| 181 | - */ |
|
| 182 | - public function getGroupMembership($principal, $needGroups = false) { |
|
| 183 | - list($prefix, $name) = \Sabre\Uri\split($principal); |
|
| 184 | - |
|
| 185 | - if ($prefix === $this->principalPrefix) { |
|
| 186 | - $user = $this->userManager->get($name); |
|
| 187 | - if (!$user) { |
|
| 188 | - throw new Exception('Principal not found'); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ($this->hasGroups || $needGroups) { |
|
| 192 | - $groups = $this->groupManager->getUserGroups($user); |
|
| 193 | - $groups = array_map(function($group) { |
|
| 194 | - /** @var IGroup $group */ |
|
| 195 | - return 'principals/groups/' . urlencode($group->getGID()); |
|
| 196 | - }, $groups); |
|
| 197 | - |
|
| 198 | - return $groups; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - return []; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Updates the list of group members for a group principal. |
|
| 206 | - * |
|
| 207 | - * The principals should be passed as a list of uri's. |
|
| 208 | - * |
|
| 209 | - * @param string $principal |
|
| 210 | - * @param string[] $members |
|
| 211 | - * @throws Exception |
|
| 212 | - */ |
|
| 213 | - public function setGroupMemberSet($principal, array $members) { |
|
| 214 | - throw new Exception('Setting members of the group is not supported yet'); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @param string $path |
|
| 219 | - * @param PropPatch $propPatch |
|
| 220 | - * @return int |
|
| 221 | - */ |
|
| 222 | - function updatePrincipal($path, PropPatch $propPatch) { |
|
| 223 | - return 0; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Search user principals |
|
| 228 | - * |
|
| 229 | - * @param array $searchProperties |
|
| 230 | - * @param string $test |
|
| 231 | - * @return array |
|
| 232 | - */ |
|
| 233 | - protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
|
| 234 | - $results = []; |
|
| 235 | - |
|
| 236 | - // If sharing is disabled, return the empty array |
|
| 237 | - $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
| 238 | - if (!$shareAPIEnabled) { |
|
| 239 | - return []; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // If sharing is restricted to group members only, |
|
| 243 | - // return only members that have groups in common |
|
| 244 | - $restrictGroups = false; |
|
| 245 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 246 | - $user = $this->userSession->getUser(); |
|
| 247 | - if (!$user) { |
|
| 248 | - return []; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - foreach ($searchProperties as $prop => $value) { |
|
| 255 | - switch ($prop) { |
|
| 256 | - case '{http://sabredav.org/ns}email-address': |
|
| 257 | - $users = $this->userManager->getByEmail($value); |
|
| 258 | - |
|
| 259 | - $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
| 260 | - // is sharing restricted to groups only? |
|
| 261 | - if ($restrictGroups !== false) { |
|
| 262 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 263 | - if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 264 | - return $carry; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 269 | - return $carry; |
|
| 270 | - }, []); |
|
| 271 | - break; |
|
| 272 | - |
|
| 273 | - case '{DAV:}displayname': |
|
| 274 | - $users = $this->userManager->searchDisplayName($value); |
|
| 275 | - |
|
| 276 | - $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
| 277 | - // is sharing restricted to groups only? |
|
| 278 | - if ($restrictGroups !== false) { |
|
| 279 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 280 | - if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 281 | - return $carry; |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 286 | - return $carry; |
|
| 287 | - }, []); |
|
| 288 | - break; |
|
| 289 | - |
|
| 290 | - default: |
|
| 291 | - $results[] = []; |
|
| 292 | - break; |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // results is an array of arrays, so this is not the first search result |
|
| 297 | - // but the results of the first searchProperty |
|
| 298 | - if (count($results) === 1) { |
|
| 299 | - return $results[0]; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - switch ($test) { |
|
| 303 | - case 'anyof': |
|
| 304 | - return array_values(array_unique(array_merge(...$results))); |
|
| 305 | - |
|
| 306 | - case 'allof': |
|
| 307 | - default: |
|
| 308 | - return array_values(array_intersect(...$results)); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param string $prefixPath |
|
| 314 | - * @param array $searchProperties |
|
| 315 | - * @param string $test |
|
| 316 | - * @return array |
|
| 317 | - */ |
|
| 318 | - function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
| 319 | - if (count($searchProperties) === 0) { |
|
| 320 | - return []; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - switch ($prefixPath) { |
|
| 324 | - case 'principals/users': |
|
| 325 | - return $this->searchUserPrincipals($searchProperties, $test); |
|
| 326 | - |
|
| 327 | - default: |
|
| 328 | - return []; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @param string $uri |
|
| 334 | - * @param string $principalPrefix |
|
| 335 | - * @return string |
|
| 336 | - */ |
|
| 337 | - function findByUri($uri, $principalPrefix) { |
|
| 338 | - // If sharing is disabled, return the empty array |
|
| 339 | - $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
| 340 | - if (!$shareAPIEnabled) { |
|
| 341 | - return null; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - // If sharing is restricted to group members only, |
|
| 345 | - // return only members that have groups in common |
|
| 346 | - $restrictGroups = false; |
|
| 347 | - if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 348 | - $user = $this->userSession->getUser(); |
|
| 349 | - if (!$user) { |
|
| 350 | - return null; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - if (strpos($uri, 'mailto:') === 0) { |
|
| 357 | - if ($principalPrefix === 'principals/users') { |
|
| 358 | - $users = $this->userManager->getByEmail(substr($uri, 7)); |
|
| 359 | - if (count($users) !== 1) { |
|
| 360 | - return null; |
|
| 361 | - } |
|
| 362 | - $user = $users[0]; |
|
| 363 | - |
|
| 364 | - if ($restrictGroups !== false) { |
|
| 365 | - $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 366 | - if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 367 | - return null; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $this->principalPrefix . '/' . $user->getUID(); |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - if (substr($uri, 0, 10) === 'principal:') { |
|
| 375 | - $principal = substr($uri, 10); |
|
| 376 | - $principal = $this->getPrincipalByPath($principal); |
|
| 377 | - if ($principal !== null) { |
|
| 378 | - return $principal['uri']; |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return null; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * @param IUser $user |
|
| 387 | - * @return array |
|
| 388 | - */ |
|
| 389 | - protected function userToPrincipal($user) { |
|
| 390 | - $userId = $user->getUID(); |
|
| 391 | - $displayName = $user->getDisplayName(); |
|
| 392 | - $principal = [ |
|
| 393 | - 'uri' => $this->principalPrefix . '/' . $userId, |
|
| 394 | - '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
|
| 395 | - '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
|
| 396 | - ]; |
|
| 397 | - |
|
| 398 | - $email = $user->getEMailAddress(); |
|
| 399 | - if (!empty($email)) { |
|
| 400 | - $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - return $principal; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - public function getPrincipalPrefix() { |
|
| 407 | - return $this->principalPrefix; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * @param string $circleUniqueId |
|
| 412 | - * @return array|null |
|
| 413 | - * @throws \OCP\AppFramework\QueryException |
|
| 414 | - * @suppress PhanUndeclaredClassMethod |
|
| 415 | - * @suppress PhanUndeclaredClassCatch |
|
| 416 | - */ |
|
| 417 | - protected function circleToPrincipal($circleUniqueId) { |
|
| 418 | - if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 419 | - return null; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - try { |
|
| 423 | - $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($circleUniqueId, true); |
|
| 424 | - } catch(QueryException $ex) { |
|
| 425 | - return null; |
|
| 426 | - } catch(CircleDoesNotExistException $ex) { |
|
| 427 | - return null; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - if (!$circle) { |
|
| 431 | - return null; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $principal = [ |
|
| 435 | - 'uri' => 'principals/circles/' . $circleUniqueId, |
|
| 436 | - '{DAV:}displayname' => $circle->getName(), |
|
| 437 | - ]; |
|
| 438 | - |
|
| 439 | - return $principal; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Returns the list of circles a principal is a member of |
|
| 444 | - * |
|
| 445 | - * @param string $principal |
|
| 446 | - * @return array |
|
| 447 | - * @throws Exception |
|
| 448 | - * @throws \OCP\AppFramework\QueryException |
|
| 449 | - * @suppress PhanUndeclaredClassMethod |
|
| 450 | - */ |
|
| 451 | - public function getCircleMembership($principal):array { |
|
| 452 | - if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 453 | - return []; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - list($prefix, $name) = \Sabre\Uri\split($principal); |
|
| 457 | - if ($this->hasCircles && $prefix === $this->principalPrefix) { |
|
| 458 | - $user = $this->userManager->get($name); |
|
| 459 | - if (!$user) { |
|
| 460 | - throw new Exception('Principal not found'); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - $circles = \OCA\Circles\Api\v1\Circles::joinedCircles($name, true); |
|
| 464 | - |
|
| 465 | - $circles = array_map(function($circle) { |
|
| 466 | - /** @var \OCA\Circles\Model\Circle $circle */ |
|
| 467 | - return 'principals/circles/' . urlencode($circle->getUniqueId()); |
|
| 468 | - }, $circles); |
|
| 469 | - |
|
| 470 | - return $circles; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - return []; |
|
| 474 | - } |
|
| 53 | + /** @var IUserManager */ |
|
| 54 | + private $userManager; |
|
| 55 | + |
|
| 56 | + /** @var IGroupManager */ |
|
| 57 | + private $groupManager; |
|
| 58 | + |
|
| 59 | + /** @var IShareManager */ |
|
| 60 | + private $shareManager; |
|
| 61 | + |
|
| 62 | + /** @var IUserSession */ |
|
| 63 | + private $userSession; |
|
| 64 | + |
|
| 65 | + /** @var IConfig */ |
|
| 66 | + private $config; |
|
| 67 | + |
|
| 68 | + /** @var IAppManager */ |
|
| 69 | + private $appManager; |
|
| 70 | + |
|
| 71 | + /** @var string */ |
|
| 72 | + private $principalPrefix; |
|
| 73 | + |
|
| 74 | + /** @var bool */ |
|
| 75 | + private $hasGroups; |
|
| 76 | + |
|
| 77 | + /** @var bool */ |
|
| 78 | + private $hasCircles; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param IUserManager $userManager |
|
| 82 | + * @param IGroupManager $groupManager |
|
| 83 | + * @param IShareManager $shareManager |
|
| 84 | + * @param IUserSession $userSession |
|
| 85 | + * @param IConfig $config |
|
| 86 | + * @param string $principalPrefix |
|
| 87 | + */ |
|
| 88 | + public function __construct(IUserManager $userManager, |
|
| 89 | + IGroupManager $groupManager, |
|
| 90 | + IShareManager $shareManager, |
|
| 91 | + IUserSession $userSession, |
|
| 92 | + IConfig $config, |
|
| 93 | + IAppManager $appManager, |
|
| 94 | + $principalPrefix = 'principals/users/') { |
|
| 95 | + $this->userManager = $userManager; |
|
| 96 | + $this->groupManager = $groupManager; |
|
| 97 | + $this->shareManager = $shareManager; |
|
| 98 | + $this->userSession = $userSession; |
|
| 99 | + $this->config = $config; |
|
| 100 | + $this->appManager = $appManager; |
|
| 101 | + $this->principalPrefix = trim($principalPrefix, '/'); |
|
| 102 | + $this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/'); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Returns a list of principals based on a prefix. |
|
| 107 | + * |
|
| 108 | + * This prefix will often contain something like 'principals'. You are only |
|
| 109 | + * expected to return principals that are in this base path. |
|
| 110 | + * |
|
| 111 | + * You are expected to return at least a 'uri' for every user, you can |
|
| 112 | + * return any additional properties if you wish so. Common properties are: |
|
| 113 | + * {DAV:}displayname |
|
| 114 | + * |
|
| 115 | + * @param string $prefixPath |
|
| 116 | + * @return string[] |
|
| 117 | + */ |
|
| 118 | + public function getPrincipalsByPrefix($prefixPath) { |
|
| 119 | + $principals = []; |
|
| 120 | + |
|
| 121 | + if ($prefixPath === $this->principalPrefix) { |
|
| 122 | + foreach($this->userManager->search('') as $user) { |
|
| 123 | + $principals[] = $this->userToPrincipal($user); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $principals; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Returns a specific principal, specified by it's path. |
|
| 132 | + * The returned structure should be the exact same as from |
|
| 133 | + * getPrincipalsByPrefix. |
|
| 134 | + * |
|
| 135 | + * @param string $path |
|
| 136 | + * @return array |
|
| 137 | + */ |
|
| 138 | + public function getPrincipalByPath($path) { |
|
| 139 | + list($prefix, $name) = \Sabre\Uri\split($path); |
|
| 140 | + |
|
| 141 | + if ($prefix === $this->principalPrefix) { |
|
| 142 | + $user = $this->userManager->get($name); |
|
| 143 | + |
|
| 144 | + if ($user !== null) { |
|
| 145 | + return $this->userToPrincipal($user); |
|
| 146 | + } |
|
| 147 | + } else if ($prefix === 'principals/circles') { |
|
| 148 | + try { |
|
| 149 | + return $this->circleToPrincipal($name); |
|
| 150 | + } catch (QueryException $e) { |
|
| 151 | + return null; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + return null; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Returns the list of members for a group-principal |
|
| 159 | + * |
|
| 160 | + * @param string $principal |
|
| 161 | + * @return string[] |
|
| 162 | + * @throws Exception |
|
| 163 | + */ |
|
| 164 | + public function getGroupMemberSet($principal) { |
|
| 165 | + // TODO: for now the group principal has only one member, the user itself |
|
| 166 | + $principal = $this->getPrincipalByPath($principal); |
|
| 167 | + if (!$principal) { |
|
| 168 | + throw new Exception('Principal not found'); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return [$principal['uri']]; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Returns the list of groups a principal is a member of |
|
| 176 | + * |
|
| 177 | + * @param string $principal |
|
| 178 | + * @param bool $needGroups |
|
| 179 | + * @return array |
|
| 180 | + * @throws Exception |
|
| 181 | + */ |
|
| 182 | + public function getGroupMembership($principal, $needGroups = false) { |
|
| 183 | + list($prefix, $name) = \Sabre\Uri\split($principal); |
|
| 184 | + |
|
| 185 | + if ($prefix === $this->principalPrefix) { |
|
| 186 | + $user = $this->userManager->get($name); |
|
| 187 | + if (!$user) { |
|
| 188 | + throw new Exception('Principal not found'); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ($this->hasGroups || $needGroups) { |
|
| 192 | + $groups = $this->groupManager->getUserGroups($user); |
|
| 193 | + $groups = array_map(function($group) { |
|
| 194 | + /** @var IGroup $group */ |
|
| 195 | + return 'principals/groups/' . urlencode($group->getGID()); |
|
| 196 | + }, $groups); |
|
| 197 | + |
|
| 198 | + return $groups; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + return []; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Updates the list of group members for a group principal. |
|
| 206 | + * |
|
| 207 | + * The principals should be passed as a list of uri's. |
|
| 208 | + * |
|
| 209 | + * @param string $principal |
|
| 210 | + * @param string[] $members |
|
| 211 | + * @throws Exception |
|
| 212 | + */ |
|
| 213 | + public function setGroupMemberSet($principal, array $members) { |
|
| 214 | + throw new Exception('Setting members of the group is not supported yet'); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @param string $path |
|
| 219 | + * @param PropPatch $propPatch |
|
| 220 | + * @return int |
|
| 221 | + */ |
|
| 222 | + function updatePrincipal($path, PropPatch $propPatch) { |
|
| 223 | + return 0; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Search user principals |
|
| 228 | + * |
|
| 229 | + * @param array $searchProperties |
|
| 230 | + * @param string $test |
|
| 231 | + * @return array |
|
| 232 | + */ |
|
| 233 | + protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
|
| 234 | + $results = []; |
|
| 235 | + |
|
| 236 | + // If sharing is disabled, return the empty array |
|
| 237 | + $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
| 238 | + if (!$shareAPIEnabled) { |
|
| 239 | + return []; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // If sharing is restricted to group members only, |
|
| 243 | + // return only members that have groups in common |
|
| 244 | + $restrictGroups = false; |
|
| 245 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 246 | + $user = $this->userSession->getUser(); |
|
| 247 | + if (!$user) { |
|
| 248 | + return []; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + foreach ($searchProperties as $prop => $value) { |
|
| 255 | + switch ($prop) { |
|
| 256 | + case '{http://sabredav.org/ns}email-address': |
|
| 257 | + $users = $this->userManager->getByEmail($value); |
|
| 258 | + |
|
| 259 | + $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
| 260 | + // is sharing restricted to groups only? |
|
| 261 | + if ($restrictGroups !== false) { |
|
| 262 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 263 | + if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 264 | + return $carry; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 269 | + return $carry; |
|
| 270 | + }, []); |
|
| 271 | + break; |
|
| 272 | + |
|
| 273 | + case '{DAV:}displayname': |
|
| 274 | + $users = $this->userManager->searchDisplayName($value); |
|
| 275 | + |
|
| 276 | + $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
|
| 277 | + // is sharing restricted to groups only? |
|
| 278 | + if ($restrictGroups !== false) { |
|
| 279 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 280 | + if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 281 | + return $carry; |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 286 | + return $carry; |
|
| 287 | + }, []); |
|
| 288 | + break; |
|
| 289 | + |
|
| 290 | + default: |
|
| 291 | + $results[] = []; |
|
| 292 | + break; |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // results is an array of arrays, so this is not the first search result |
|
| 297 | + // but the results of the first searchProperty |
|
| 298 | + if (count($results) === 1) { |
|
| 299 | + return $results[0]; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + switch ($test) { |
|
| 303 | + case 'anyof': |
|
| 304 | + return array_values(array_unique(array_merge(...$results))); |
|
| 305 | + |
|
| 306 | + case 'allof': |
|
| 307 | + default: |
|
| 308 | + return array_values(array_intersect(...$results)); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param string $prefixPath |
|
| 314 | + * @param array $searchProperties |
|
| 315 | + * @param string $test |
|
| 316 | + * @return array |
|
| 317 | + */ |
|
| 318 | + function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
|
| 319 | + if (count($searchProperties) === 0) { |
|
| 320 | + return []; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + switch ($prefixPath) { |
|
| 324 | + case 'principals/users': |
|
| 325 | + return $this->searchUserPrincipals($searchProperties, $test); |
|
| 326 | + |
|
| 327 | + default: |
|
| 328 | + return []; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @param string $uri |
|
| 334 | + * @param string $principalPrefix |
|
| 335 | + * @return string |
|
| 336 | + */ |
|
| 337 | + function findByUri($uri, $principalPrefix) { |
|
| 338 | + // If sharing is disabled, return the empty array |
|
| 339 | + $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
|
| 340 | + if (!$shareAPIEnabled) { |
|
| 341 | + return null; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + // If sharing is restricted to group members only, |
|
| 345 | + // return only members that have groups in common |
|
| 346 | + $restrictGroups = false; |
|
| 347 | + if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 348 | + $user = $this->userSession->getUser(); |
|
| 349 | + if (!$user) { |
|
| 350 | + return null; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + $restrictGroups = $this->groupManager->getUserGroupIds($user); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + if (strpos($uri, 'mailto:') === 0) { |
|
| 357 | + if ($principalPrefix === 'principals/users') { |
|
| 358 | + $users = $this->userManager->getByEmail(substr($uri, 7)); |
|
| 359 | + if (count($users) !== 1) { |
|
| 360 | + return null; |
|
| 361 | + } |
|
| 362 | + $user = $users[0]; |
|
| 363 | + |
|
| 364 | + if ($restrictGroups !== false) { |
|
| 365 | + $userGroups = $this->groupManager->getUserGroupIds($user); |
|
| 366 | + if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
|
| 367 | + return null; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $this->principalPrefix . '/' . $user->getUID(); |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + if (substr($uri, 0, 10) === 'principal:') { |
|
| 375 | + $principal = substr($uri, 10); |
|
| 376 | + $principal = $this->getPrincipalByPath($principal); |
|
| 377 | + if ($principal !== null) { |
|
| 378 | + return $principal['uri']; |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return null; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * @param IUser $user |
|
| 387 | + * @return array |
|
| 388 | + */ |
|
| 389 | + protected function userToPrincipal($user) { |
|
| 390 | + $userId = $user->getUID(); |
|
| 391 | + $displayName = $user->getDisplayName(); |
|
| 392 | + $principal = [ |
|
| 393 | + 'uri' => $this->principalPrefix . '/' . $userId, |
|
| 394 | + '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
|
| 395 | + '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
|
| 396 | + ]; |
|
| 397 | + |
|
| 398 | + $email = $user->getEMailAddress(); |
|
| 399 | + if (!empty($email)) { |
|
| 400 | + $principal['{http://sabredav.org/ns}email-address'] = $email; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + return $principal; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + public function getPrincipalPrefix() { |
|
| 407 | + return $this->principalPrefix; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * @param string $circleUniqueId |
|
| 412 | + * @return array|null |
|
| 413 | + * @throws \OCP\AppFramework\QueryException |
|
| 414 | + * @suppress PhanUndeclaredClassMethod |
|
| 415 | + * @suppress PhanUndeclaredClassCatch |
|
| 416 | + */ |
|
| 417 | + protected function circleToPrincipal($circleUniqueId) { |
|
| 418 | + if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 419 | + return null; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + try { |
|
| 423 | + $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($circleUniqueId, true); |
|
| 424 | + } catch(QueryException $ex) { |
|
| 425 | + return null; |
|
| 426 | + } catch(CircleDoesNotExistException $ex) { |
|
| 427 | + return null; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + if (!$circle) { |
|
| 431 | + return null; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $principal = [ |
|
| 435 | + 'uri' => 'principals/circles/' . $circleUniqueId, |
|
| 436 | + '{DAV:}displayname' => $circle->getName(), |
|
| 437 | + ]; |
|
| 438 | + |
|
| 439 | + return $principal; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Returns the list of circles a principal is a member of |
|
| 444 | + * |
|
| 445 | + * @param string $principal |
|
| 446 | + * @return array |
|
| 447 | + * @throws Exception |
|
| 448 | + * @throws \OCP\AppFramework\QueryException |
|
| 449 | + * @suppress PhanUndeclaredClassMethod |
|
| 450 | + */ |
|
| 451 | + public function getCircleMembership($principal):array { |
|
| 452 | + if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 453 | + return []; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + list($prefix, $name) = \Sabre\Uri\split($principal); |
|
| 457 | + if ($this->hasCircles && $prefix === $this->principalPrefix) { |
|
| 458 | + $user = $this->userManager->get($name); |
|
| 459 | + if (!$user) { |
|
| 460 | + throw new Exception('Principal not found'); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + $circles = \OCA\Circles\Api\v1\Circles::joinedCircles($name, true); |
|
| 464 | + |
|
| 465 | + $circles = array_map(function($circle) { |
|
| 466 | + /** @var \OCA\Circles\Model\Circle $circle */ |
|
| 467 | + return 'principals/circles/' . urlencode($circle->getUniqueId()); |
|
| 468 | + }, $circles); |
|
| 469 | + |
|
| 470 | + return $circles; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + return []; |
|
| 474 | + } |
|
| 475 | 475 | |
| 476 | 476 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | $principals = []; |
| 120 | 120 | |
| 121 | 121 | if ($prefixPath === $this->principalPrefix) { |
| 122 | - foreach($this->userManager->search('') as $user) { |
|
| 122 | + foreach ($this->userManager->search('') as $user) { |
|
| 123 | 123 | $principals[] = $this->userToPrincipal($user); |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | $groups = $this->groupManager->getUserGroups($user); |
| 193 | 193 | $groups = array_map(function($group) { |
| 194 | 194 | /** @var IGroup $group */ |
| 195 | - return 'principals/groups/' . urlencode($group->getGID()); |
|
| 195 | + return 'principals/groups/'.urlencode($group->getGID()); |
|
| 196 | 196 | }, $groups); |
| 197 | 197 | |
| 198 | 198 | return $groups; |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 268 | + $carry[] = $this->principalPrefix.'/'.$user->getUID(); |
|
| 269 | 269 | return $carry; |
| 270 | 270 | }, []); |
| 271 | 271 | break; |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
|
| 285 | + $carry[] = $this->principalPrefix.'/'.$user->getUID(); |
|
| 286 | 286 | return $carry; |
| 287 | 287 | }, []); |
| 288 | 288 | break; |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | } |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - return $this->principalPrefix . '/' . $user->getUID(); |
|
| 371 | + return $this->principalPrefix.'/'.$user->getUID(); |
|
| 372 | 372 | } |
| 373 | 373 | } |
| 374 | 374 | if (substr($uri, 0, 10) === 'principal:') { |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | $userId = $user->getUID(); |
| 391 | 391 | $displayName = $user->getDisplayName(); |
| 392 | 392 | $principal = [ |
| 393 | - 'uri' => $this->principalPrefix . '/' . $userId, |
|
| 393 | + 'uri' => $this->principalPrefix.'/'.$userId, |
|
| 394 | 394 | '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
| 395 | 395 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
| 396 | 396 | ]; |
@@ -421,9 +421,9 @@ discard block |
||
| 421 | 421 | |
| 422 | 422 | try { |
| 423 | 423 | $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($circleUniqueId, true); |
| 424 | - } catch(QueryException $ex) { |
|
| 424 | + } catch (QueryException $ex) { |
|
| 425 | 425 | return null; |
| 426 | - } catch(CircleDoesNotExistException $ex) { |
|
| 426 | + } catch (CircleDoesNotExistException $ex) { |
|
| 427 | 427 | return null; |
| 428 | 428 | } |
| 429 | 429 | |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | $principal = [ |
| 435 | - 'uri' => 'principals/circles/' . $circleUniqueId, |
|
| 435 | + 'uri' => 'principals/circles/'.$circleUniqueId, |
|
| 436 | 436 | '{DAV:}displayname' => $circle->getName(), |
| 437 | 437 | ]; |
| 438 | 438 | |
@@ -464,7 +464,7 @@ discard block |
||
| 464 | 464 | |
| 465 | 465 | $circles = array_map(function($circle) { |
| 466 | 466 | /** @var \OCA\Circles\Model\Circle $circle */ |
| 467 | - return 'principals/circles/' . urlencode($circle->getUniqueId()); |
|
| 467 | + return 'principals/circles/'.urlencode($circle->getUniqueId()); |
|
| 468 | 468 | }, $circles); |
| 469 | 469 | |
| 470 | 470 | return $circles; |
@@ -42,160 +42,160 @@ |
||
| 42 | 42 | use Sabre\DAV\Auth\Plugin; |
| 43 | 43 | |
| 44 | 44 | class ServerFactory { |
| 45 | - /** @var IConfig */ |
|
| 46 | - private $config; |
|
| 47 | - /** @var ILogger */ |
|
| 48 | - private $logger; |
|
| 49 | - /** @var IDBConnection */ |
|
| 50 | - private $databaseConnection; |
|
| 51 | - /** @var IUserSession */ |
|
| 52 | - private $userSession; |
|
| 53 | - /** @var IMountManager */ |
|
| 54 | - private $mountManager; |
|
| 55 | - /** @var ITagManager */ |
|
| 56 | - private $tagManager; |
|
| 57 | - /** @var IRequest */ |
|
| 58 | - private $request; |
|
| 59 | - /** @var IPreview */ |
|
| 60 | - private $previewManager; |
|
| 45 | + /** @var IConfig */ |
|
| 46 | + private $config; |
|
| 47 | + /** @var ILogger */ |
|
| 48 | + private $logger; |
|
| 49 | + /** @var IDBConnection */ |
|
| 50 | + private $databaseConnection; |
|
| 51 | + /** @var IUserSession */ |
|
| 52 | + private $userSession; |
|
| 53 | + /** @var IMountManager */ |
|
| 54 | + private $mountManager; |
|
| 55 | + /** @var ITagManager */ |
|
| 56 | + private $tagManager; |
|
| 57 | + /** @var IRequest */ |
|
| 58 | + private $request; |
|
| 59 | + /** @var IPreview */ |
|
| 60 | + private $previewManager; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @param IConfig $config |
|
| 64 | - * @param ILogger $logger |
|
| 65 | - * @param IDBConnection $databaseConnection |
|
| 66 | - * @param IUserSession $userSession |
|
| 67 | - * @param IMountManager $mountManager |
|
| 68 | - * @param ITagManager $tagManager |
|
| 69 | - * @param IRequest $request |
|
| 70 | - * @param IPreview $previewManager |
|
| 71 | - */ |
|
| 72 | - public function __construct( |
|
| 73 | - IConfig $config, |
|
| 74 | - ILogger $logger, |
|
| 75 | - IDBConnection $databaseConnection, |
|
| 76 | - IUserSession $userSession, |
|
| 77 | - IMountManager $mountManager, |
|
| 78 | - ITagManager $tagManager, |
|
| 79 | - IRequest $request, |
|
| 80 | - IPreview $previewManager |
|
| 81 | - ) { |
|
| 82 | - $this->config = $config; |
|
| 83 | - $this->logger = $logger; |
|
| 84 | - $this->databaseConnection = $databaseConnection; |
|
| 85 | - $this->userSession = $userSession; |
|
| 86 | - $this->mountManager = $mountManager; |
|
| 87 | - $this->tagManager = $tagManager; |
|
| 88 | - $this->request = $request; |
|
| 89 | - $this->previewManager = $previewManager; |
|
| 90 | - } |
|
| 62 | + /** |
|
| 63 | + * @param IConfig $config |
|
| 64 | + * @param ILogger $logger |
|
| 65 | + * @param IDBConnection $databaseConnection |
|
| 66 | + * @param IUserSession $userSession |
|
| 67 | + * @param IMountManager $mountManager |
|
| 68 | + * @param ITagManager $tagManager |
|
| 69 | + * @param IRequest $request |
|
| 70 | + * @param IPreview $previewManager |
|
| 71 | + */ |
|
| 72 | + public function __construct( |
|
| 73 | + IConfig $config, |
|
| 74 | + ILogger $logger, |
|
| 75 | + IDBConnection $databaseConnection, |
|
| 76 | + IUserSession $userSession, |
|
| 77 | + IMountManager $mountManager, |
|
| 78 | + ITagManager $tagManager, |
|
| 79 | + IRequest $request, |
|
| 80 | + IPreview $previewManager |
|
| 81 | + ) { |
|
| 82 | + $this->config = $config; |
|
| 83 | + $this->logger = $logger; |
|
| 84 | + $this->databaseConnection = $databaseConnection; |
|
| 85 | + $this->userSession = $userSession; |
|
| 86 | + $this->mountManager = $mountManager; |
|
| 87 | + $this->tagManager = $tagManager; |
|
| 88 | + $this->request = $request; |
|
| 89 | + $this->previewManager = $previewManager; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @param string $baseUri |
|
| 94 | - * @param string $requestUri |
|
| 95 | - * @param Plugin $authPlugin |
|
| 96 | - * @param callable $viewCallBack callback that should return the view for the dav endpoint |
|
| 97 | - * @return Server |
|
| 98 | - */ |
|
| 99 | - public function createServer($baseUri, |
|
| 100 | - $requestUri, |
|
| 101 | - Plugin $authPlugin, |
|
| 102 | - callable $viewCallBack) { |
|
| 103 | - // Fire up server |
|
| 104 | - $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree(); |
|
| 105 | - $server = new \OCA\DAV\Connector\Sabre\Server($objectTree); |
|
| 106 | - // Set URL explicitly due to reverse-proxy situations |
|
| 107 | - $server->httpRequest->setUrl($requestUri); |
|
| 108 | - $server->setBaseUri($baseUri); |
|
| 92 | + /** |
|
| 93 | + * @param string $baseUri |
|
| 94 | + * @param string $requestUri |
|
| 95 | + * @param Plugin $authPlugin |
|
| 96 | + * @param callable $viewCallBack callback that should return the view for the dav endpoint |
|
| 97 | + * @return Server |
|
| 98 | + */ |
|
| 99 | + public function createServer($baseUri, |
|
| 100 | + $requestUri, |
|
| 101 | + Plugin $authPlugin, |
|
| 102 | + callable $viewCallBack) { |
|
| 103 | + // Fire up server |
|
| 104 | + $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree(); |
|
| 105 | + $server = new \OCA\DAV\Connector\Sabre\Server($objectTree); |
|
| 106 | + // Set URL explicitly due to reverse-proxy situations |
|
| 107 | + $server->httpRequest->setUrl($requestUri); |
|
| 108 | + $server->setBaseUri($baseUri); |
|
| 109 | 109 | |
| 110 | - // Load plugins |
|
| 111 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config)); |
|
| 112 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config)); |
|
| 113 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin()); |
|
| 114 | - $server->addPlugin($authPlugin); |
|
| 115 | - // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to / |
|
| 116 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); |
|
| 117 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger)); |
|
| 118 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 119 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 120 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
| 121 | - if($this->request->isUserAgent([ |
|
| 122 | - '/WebDAVFS/', |
|
| 123 | - '/OneNote/', |
|
| 124 | - '/Microsoft-WebDAV-MiniRedir/', |
|
| 125 | - ])) { |
|
| 126 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin()); |
|
| 127 | - } |
|
| 110 | + // Load plugins |
|
| 111 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config)); |
|
| 112 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config)); |
|
| 113 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin()); |
|
| 114 | + $server->addPlugin($authPlugin); |
|
| 115 | + // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to / |
|
| 116 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); |
|
| 117 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger)); |
|
| 118 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 119 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 120 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
| 121 | + if($this->request->isUserAgent([ |
|
| 122 | + '/WebDAVFS/', |
|
| 123 | + '/OneNote/', |
|
| 124 | + '/Microsoft-WebDAV-MiniRedir/', |
|
| 125 | + ])) { |
|
| 126 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin()); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) { |
|
| 130 | - $server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 131 | - } |
|
| 129 | + if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) { |
|
| 130 | + $server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - // wait with registering these until auth is handled and the filesystem is setup |
|
| 134 | - $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) { |
|
| 135 | - // ensure the skeleton is copied |
|
| 136 | - $userFolder = \OC::$server->getUserFolder(); |
|
| 133 | + // wait with registering these until auth is handled and the filesystem is setup |
|
| 134 | + $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) { |
|
| 135 | + // ensure the skeleton is copied |
|
| 136 | + $userFolder = \OC::$server->getUserFolder(); |
|
| 137 | 137 | |
| 138 | - /** @var \OC\Files\View $view */ |
|
| 139 | - $view = $viewCallBack($server); |
|
| 140 | - if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) { |
|
| 141 | - $rootInfo = $userFolder; |
|
| 142 | - } else { |
|
| 143 | - $rootInfo = $view->getFileInfo(''); |
|
| 144 | - } |
|
| 138 | + /** @var \OC\Files\View $view */ |
|
| 139 | + $view = $viewCallBack($server); |
|
| 140 | + if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) { |
|
| 141 | + $rootInfo = $userFolder; |
|
| 142 | + } else { |
|
| 143 | + $rootInfo = $view->getFileInfo(''); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - // Create Nextcloud Dir |
|
| 147 | - if ($rootInfo->getType() === 'dir') { |
|
| 148 | - $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree); |
|
| 149 | - } else { |
|
| 150 | - $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo); |
|
| 151 | - } |
|
| 152 | - $objectTree->init($root, $view, $this->mountManager); |
|
| 146 | + // Create Nextcloud Dir |
|
| 147 | + if ($rootInfo->getType() === 'dir') { |
|
| 148 | + $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree); |
|
| 149 | + } else { |
|
| 150 | + $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo); |
|
| 151 | + } |
|
| 152 | + $objectTree->init($root, $view, $this->mountManager); |
|
| 153 | 153 | |
| 154 | - $server->addPlugin( |
|
| 155 | - new \OCA\DAV\Connector\Sabre\FilesPlugin( |
|
| 156 | - $objectTree, |
|
| 157 | - $this->config, |
|
| 158 | - $this->request, |
|
| 159 | - $this->previewManager, |
|
| 160 | - false, |
|
| 161 | - !$this->config->getSystemValue('debug', false) |
|
| 162 | - ) |
|
| 163 | - ); |
|
| 164 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true)); |
|
| 154 | + $server->addPlugin( |
|
| 155 | + new \OCA\DAV\Connector\Sabre\FilesPlugin( |
|
| 156 | + $objectTree, |
|
| 157 | + $this->config, |
|
| 158 | + $this->request, |
|
| 159 | + $this->previewManager, |
|
| 160 | + false, |
|
| 161 | + !$this->config->getSystemValue('debug', false) |
|
| 162 | + ) |
|
| 163 | + ); |
|
| 164 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true)); |
|
| 165 | 165 | |
| 166 | - if($this->userSession->isLoggedIn()) { |
|
| 167 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager)); |
|
| 168 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin( |
|
| 169 | - $objectTree, |
|
| 170 | - $this->userSession, |
|
| 171 | - $userFolder, |
|
| 172 | - \OC::$server->getShareManager() |
|
| 173 | - )); |
|
| 174 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession)); |
|
| 175 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin( |
|
| 176 | - $objectTree, |
|
| 177 | - $view, |
|
| 178 | - \OC::$server->getSystemTagManager(), |
|
| 179 | - \OC::$server->getSystemTagObjectMapper(), |
|
| 180 | - \OC::$server->getTagManager(), |
|
| 181 | - $this->userSession, |
|
| 182 | - \OC::$server->getGroupManager(), |
|
| 183 | - $userFolder, |
|
| 184 | - \OC::$server->getAppManager() |
|
| 185 | - )); |
|
| 186 | - // custom properties plugin must be the last one |
|
| 187 | - $server->addPlugin( |
|
| 188 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 189 | - new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend( |
|
| 190 | - $objectTree, |
|
| 191 | - $this->databaseConnection, |
|
| 192 | - $this->userSession->getUser() |
|
| 193 | - ) |
|
| 194 | - ) |
|
| 195 | - ); |
|
| 196 | - } |
|
| 197 | - $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin()); |
|
| 198 | - }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request |
|
| 199 | - return $server; |
|
| 200 | - } |
|
| 166 | + if($this->userSession->isLoggedIn()) { |
|
| 167 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager)); |
|
| 168 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin( |
|
| 169 | + $objectTree, |
|
| 170 | + $this->userSession, |
|
| 171 | + $userFolder, |
|
| 172 | + \OC::$server->getShareManager() |
|
| 173 | + )); |
|
| 174 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession)); |
|
| 175 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin( |
|
| 176 | + $objectTree, |
|
| 177 | + $view, |
|
| 178 | + \OC::$server->getSystemTagManager(), |
|
| 179 | + \OC::$server->getSystemTagObjectMapper(), |
|
| 180 | + \OC::$server->getTagManager(), |
|
| 181 | + $this->userSession, |
|
| 182 | + \OC::$server->getGroupManager(), |
|
| 183 | + $userFolder, |
|
| 184 | + \OC::$server->getAppManager() |
|
| 185 | + )); |
|
| 186 | + // custom properties plugin must be the last one |
|
| 187 | + $server->addPlugin( |
|
| 188 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 189 | + new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend( |
|
| 190 | + $objectTree, |
|
| 191 | + $this->databaseConnection, |
|
| 192 | + $this->userSession->getUser() |
|
| 193 | + ) |
|
| 194 | + ) |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | + $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin()); |
|
| 198 | + }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request |
|
| 199 | + return $server; |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -70,259 +70,259 @@ |
||
| 70 | 70 | |
| 71 | 71 | class Server { |
| 72 | 72 | |
| 73 | - /** @var IRequest */ |
|
| 74 | - private $request; |
|
| 75 | - |
|
| 76 | - /** @var string */ |
|
| 77 | - private $baseUri; |
|
| 78 | - |
|
| 79 | - /** @var Connector\Sabre\Server */ |
|
| 80 | - public $server; |
|
| 81 | - |
|
| 82 | - public function __construct(IRequest $request, $baseUri) { |
|
| 83 | - $this->request = $request; |
|
| 84 | - $this->baseUri = $baseUri; |
|
| 85 | - $logger = \OC::$server->getLogger(); |
|
| 86 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 87 | - |
|
| 88 | - $root = new RootCollection(); |
|
| 89 | - $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
| 90 | - |
|
| 91 | - // Add maintenance plugin |
|
| 92 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
| 93 | - |
|
| 94 | - // Backends |
|
| 95 | - $authBackend = new Auth( |
|
| 96 | - \OC::$server->getSession(), |
|
| 97 | - \OC::$server->getUserSession(), |
|
| 98 | - \OC::$server->getRequest(), |
|
| 99 | - \OC::$server->getTwoFactorAuthManager(), |
|
| 100 | - \OC::$server->getBruteForceThrottler() |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - // Set URL explicitly due to reverse-proxy situations |
|
| 104 | - $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
| 105 | - $this->server->setBaseUri($this->baseUri); |
|
| 106 | - |
|
| 107 | - $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
| 108 | - $this->server->addPlugin(new AnonymousOptionsPlugin()); |
|
| 109 | - $authPlugin = new Plugin(); |
|
| 110 | - $authPlugin->addBackend(new PublicAuth()); |
|
| 111 | - $this->server->addPlugin($authPlugin); |
|
| 112 | - |
|
| 113 | - // allow setup of additional auth backends |
|
| 114 | - $event = new SabrePluginEvent($this->server); |
|
| 115 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
| 116 | - |
|
| 117 | - $bearerAuthBackend = new BearerAuth( |
|
| 118 | - \OC::$server->getUserSession(), |
|
| 119 | - \OC::$server->getSession(), |
|
| 120 | - \OC::$server->getRequest() |
|
| 121 | - ); |
|
| 122 | - $authPlugin->addBackend($bearerAuthBackend); |
|
| 123 | - // because we are throwing exceptions this plugin has to be the last one |
|
| 124 | - $authPlugin->addBackend($authBackend); |
|
| 125 | - |
|
| 126 | - // debugging |
|
| 127 | - if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
| 128 | - $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
| 129 | - } else { |
|
| 130 | - $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
| 134 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 135 | - $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
| 136 | - |
|
| 137 | - // acl |
|
| 138 | - $acl = new DavAclPlugin(); |
|
| 139 | - $acl->principalCollectionSet = [ |
|
| 140 | - 'principals/users', 'principals/groups', |
|
| 141 | - 'principals/calendar-resources', |
|
| 142 | - 'principals/calendar-rooms', |
|
| 143 | - ]; |
|
| 144 | - $acl->defaultUsernamePath = 'principals/users'; |
|
| 145 | - $this->server->addPlugin($acl); |
|
| 146 | - |
|
| 147 | - // calendar plugins |
|
| 148 | - if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) { |
|
| 149 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
| 150 | - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
| 151 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
| 152 | - if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { |
|
| 153 | - $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $this->server->addPlugin(new CalDAV\WebcalCaching\Plugin($request)); |
|
| 157 | - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
| 158 | - |
|
| 159 | - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
| 160 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 161 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
| 162 | - \OC::$server->getConfig(), |
|
| 163 | - \OC::$server->getURLGenerator() |
|
| 164 | - )); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // addressbook plugins |
|
| 168 | - if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { |
|
| 169 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 170 | - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
| 171 | - $this->server->addPlugin(new VCFExportPlugin()); |
|
| 172 | - $this->server->addPlugin(new MultiGetExportPlugin()); |
|
| 173 | - $this->server->addPlugin(new HasPhotoPlugin()); |
|
| 174 | - $this->server->addPlugin(new ImageExportPlugin(new PhotoCache( |
|
| 175 | - \OC::$server->getAppDataDir('dav-photocache'), |
|
| 176 | - \OC::$server->getLogger()) |
|
| 177 | - )); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // system tags plugins |
|
| 181 | - $this->server->addPlugin(new SystemTagPlugin( |
|
| 182 | - \OC::$server->getSystemTagManager(), |
|
| 183 | - \OC::$server->getGroupManager(), |
|
| 184 | - \OC::$server->getUserSession() |
|
| 185 | - )); |
|
| 186 | - |
|
| 187 | - // comments plugin |
|
| 188 | - $this->server->addPlugin(new CommentsPlugin( |
|
| 189 | - \OC::$server->getCommentsManager(), |
|
| 190 | - \OC::$server->getUserSession() |
|
| 191 | - )); |
|
| 192 | - |
|
| 193 | - $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
| 194 | - $this->server->addPlugin(new ChunkingPlugin()); |
|
| 195 | - |
|
| 196 | - // allow setup of additional plugins |
|
| 197 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
| 198 | - |
|
| 199 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 200 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
| 201 | - if($request->isUserAgent([ |
|
| 202 | - '/WebDAVFS/', |
|
| 203 | - '/OneNote/', |
|
| 204 | - '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
| 205 | - ])) { |
|
| 206 | - $this->server->addPlugin(new FakeLockerPlugin()); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
| 210 | - $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $lazySearchBackend = new LazySearchBackend(); |
|
| 214 | - $this->server->addPlugin(new SearchPlugin($lazySearchBackend)); |
|
| 215 | - |
|
| 216 | - // wait with registering these until auth is handled and the filesystem is setup |
|
| 217 | - $this->server->on('beforeMethod', function () use ($root, $lazySearchBackend) { |
|
| 218 | - // custom properties plugin must be the last one |
|
| 219 | - $userSession = \OC::$server->getUserSession(); |
|
| 220 | - $user = $userSession->getUser(); |
|
| 221 | - if ($user !== null) { |
|
| 222 | - $view = \OC\Files\Filesystem::getView(); |
|
| 223 | - $this->server->addPlugin( |
|
| 224 | - new FilesPlugin( |
|
| 225 | - $this->server->tree, |
|
| 226 | - \OC::$server->getConfig(), |
|
| 227 | - $this->request, |
|
| 228 | - \OC::$server->getPreviewManager(), |
|
| 229 | - false, |
|
| 230 | - !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
| 231 | - ) |
|
| 232 | - ); |
|
| 233 | - |
|
| 234 | - $this->server->addPlugin( |
|
| 235 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 236 | - new CustomPropertiesBackend( |
|
| 237 | - $this->server->tree, |
|
| 238 | - \OC::$server->getDatabaseConnection(), |
|
| 239 | - \OC::$server->getUserSession()->getUser() |
|
| 240 | - ) |
|
| 241 | - ) |
|
| 242 | - ); |
|
| 243 | - if ($view !== null) { |
|
| 244 | - $this->server->addPlugin( |
|
| 245 | - new QuotaPlugin($view, false)); |
|
| 246 | - } |
|
| 247 | - $this->server->addPlugin( |
|
| 248 | - new TagsPlugin( |
|
| 249 | - $this->server->tree, \OC::$server->getTagManager() |
|
| 250 | - ) |
|
| 251 | - ); |
|
| 252 | - // TODO: switch to LazyUserFolder |
|
| 253 | - $userFolder = \OC::$server->getUserFolder(); |
|
| 254 | - $this->server->addPlugin(new SharesPlugin( |
|
| 255 | - $this->server->tree, |
|
| 256 | - $userSession, |
|
| 257 | - $userFolder, |
|
| 258 | - \OC::$server->getShareManager() |
|
| 259 | - )); |
|
| 260 | - $this->server->addPlugin(new CommentPropertiesPlugin( |
|
| 261 | - \OC::$server->getCommentsManager(), |
|
| 262 | - $userSession |
|
| 263 | - )); |
|
| 264 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
| 265 | - if ($view !== null) { |
|
| 266 | - $this->server->addPlugin(new FilesReportPlugin( |
|
| 267 | - $this->server->tree, |
|
| 268 | - $view, |
|
| 269 | - \OC::$server->getSystemTagManager(), |
|
| 270 | - \OC::$server->getSystemTagObjectMapper(), |
|
| 271 | - \OC::$server->getTagManager(), |
|
| 272 | - $userSession, |
|
| 273 | - \OC::$server->getGroupManager(), |
|
| 274 | - $userFolder, |
|
| 275 | - \OC::$server->getAppManager() |
|
| 276 | - )); |
|
| 277 | - $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend( |
|
| 278 | - $this->server->tree, |
|
| 279 | - $user, |
|
| 280 | - \OC::$server->getRootFolder(), |
|
| 281 | - \OC::$server->getShareManager(), |
|
| 282 | - $view |
|
| 283 | - )); |
|
| 284 | - } |
|
| 285 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin( |
|
| 286 | - \OC::$server->getConfig(), |
|
| 287 | - \OC::$server->query(BirthdayService::class) |
|
| 288 | - )); |
|
| 289 | - $this->server->addPlugin(new AppleProvisioningPlugin( |
|
| 290 | - \OC::$server->getUserSession(), |
|
| 291 | - \OC::$server->getURLGenerator(), |
|
| 292 | - \OC::$server->getThemingDefaults(), |
|
| 293 | - \OC::$server->getRequest(), |
|
| 294 | - \OC::$server->getL10N('dav'), |
|
| 295 | - function() { |
|
| 296 | - return UUIDUtil::getUUID(); |
|
| 297 | - } |
|
| 298 | - )); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - // register plugins from apps |
|
| 302 | - $pluginManager = new PluginManager( |
|
| 303 | - \OC::$server, |
|
| 304 | - \OC::$server->getAppManager() |
|
| 305 | - ); |
|
| 306 | - foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
| 307 | - $this->server->addPlugin($appPlugin); |
|
| 308 | - } |
|
| 309 | - foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
| 310 | - $root->addChild($appCollection); |
|
| 311 | - } |
|
| 312 | - }); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - public function exec() { |
|
| 316 | - $this->server->exec(); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - private function requestIsForSubtree(array $subTrees): bool { |
|
| 320 | - foreach ($subTrees as $subTree) { |
|
| 321 | - $subTree = trim($subTree, ' /'); |
|
| 322 | - if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) { |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 73 | + /** @var IRequest */ |
|
| 74 | + private $request; |
|
| 75 | + |
|
| 76 | + /** @var string */ |
|
| 77 | + private $baseUri; |
|
| 78 | + |
|
| 79 | + /** @var Connector\Sabre\Server */ |
|
| 80 | + public $server; |
|
| 81 | + |
|
| 82 | + public function __construct(IRequest $request, $baseUri) { |
|
| 83 | + $this->request = $request; |
|
| 84 | + $this->baseUri = $baseUri; |
|
| 85 | + $logger = \OC::$server->getLogger(); |
|
| 86 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 87 | + |
|
| 88 | + $root = new RootCollection(); |
|
| 89 | + $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
| 90 | + |
|
| 91 | + // Add maintenance plugin |
|
| 92 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
| 93 | + |
|
| 94 | + // Backends |
|
| 95 | + $authBackend = new Auth( |
|
| 96 | + \OC::$server->getSession(), |
|
| 97 | + \OC::$server->getUserSession(), |
|
| 98 | + \OC::$server->getRequest(), |
|
| 99 | + \OC::$server->getTwoFactorAuthManager(), |
|
| 100 | + \OC::$server->getBruteForceThrottler() |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + // Set URL explicitly due to reverse-proxy situations |
|
| 104 | + $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
| 105 | + $this->server->setBaseUri($this->baseUri); |
|
| 106 | + |
|
| 107 | + $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
| 108 | + $this->server->addPlugin(new AnonymousOptionsPlugin()); |
|
| 109 | + $authPlugin = new Plugin(); |
|
| 110 | + $authPlugin->addBackend(new PublicAuth()); |
|
| 111 | + $this->server->addPlugin($authPlugin); |
|
| 112 | + |
|
| 113 | + // allow setup of additional auth backends |
|
| 114 | + $event = new SabrePluginEvent($this->server); |
|
| 115 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
| 116 | + |
|
| 117 | + $bearerAuthBackend = new BearerAuth( |
|
| 118 | + \OC::$server->getUserSession(), |
|
| 119 | + \OC::$server->getSession(), |
|
| 120 | + \OC::$server->getRequest() |
|
| 121 | + ); |
|
| 122 | + $authPlugin->addBackend($bearerAuthBackend); |
|
| 123 | + // because we are throwing exceptions this plugin has to be the last one |
|
| 124 | + $authPlugin->addBackend($authBackend); |
|
| 125 | + |
|
| 126 | + // debugging |
|
| 127 | + if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
| 128 | + $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
| 129 | + } else { |
|
| 130 | + $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
| 134 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
| 135 | + $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
| 136 | + |
|
| 137 | + // acl |
|
| 138 | + $acl = new DavAclPlugin(); |
|
| 139 | + $acl->principalCollectionSet = [ |
|
| 140 | + 'principals/users', 'principals/groups', |
|
| 141 | + 'principals/calendar-resources', |
|
| 142 | + 'principals/calendar-rooms', |
|
| 143 | + ]; |
|
| 144 | + $acl->defaultUsernamePath = 'principals/users'; |
|
| 145 | + $this->server->addPlugin($acl); |
|
| 146 | + |
|
| 147 | + // calendar plugins |
|
| 148 | + if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) { |
|
| 149 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
| 150 | + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
| 151 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
| 152 | + if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { |
|
| 153 | + $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $this->server->addPlugin(new CalDAV\WebcalCaching\Plugin($request)); |
|
| 157 | + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
| 158 | + |
|
| 159 | + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
| 160 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 161 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
| 162 | + \OC::$server->getConfig(), |
|
| 163 | + \OC::$server->getURLGenerator() |
|
| 164 | + )); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // addressbook plugins |
|
| 168 | + if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { |
|
| 169 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
| 170 | + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
| 171 | + $this->server->addPlugin(new VCFExportPlugin()); |
|
| 172 | + $this->server->addPlugin(new MultiGetExportPlugin()); |
|
| 173 | + $this->server->addPlugin(new HasPhotoPlugin()); |
|
| 174 | + $this->server->addPlugin(new ImageExportPlugin(new PhotoCache( |
|
| 175 | + \OC::$server->getAppDataDir('dav-photocache'), |
|
| 176 | + \OC::$server->getLogger()) |
|
| 177 | + )); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // system tags plugins |
|
| 181 | + $this->server->addPlugin(new SystemTagPlugin( |
|
| 182 | + \OC::$server->getSystemTagManager(), |
|
| 183 | + \OC::$server->getGroupManager(), |
|
| 184 | + \OC::$server->getUserSession() |
|
| 185 | + )); |
|
| 186 | + |
|
| 187 | + // comments plugin |
|
| 188 | + $this->server->addPlugin(new CommentsPlugin( |
|
| 189 | + \OC::$server->getCommentsManager(), |
|
| 190 | + \OC::$server->getUserSession() |
|
| 191 | + )); |
|
| 192 | + |
|
| 193 | + $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
| 194 | + $this->server->addPlugin(new ChunkingPlugin()); |
|
| 195 | + |
|
| 196 | + // allow setup of additional plugins |
|
| 197 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
| 198 | + |
|
| 199 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
| 200 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
| 201 | + if($request->isUserAgent([ |
|
| 202 | + '/WebDAVFS/', |
|
| 203 | + '/OneNote/', |
|
| 204 | + '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
| 205 | + ])) { |
|
| 206 | + $this->server->addPlugin(new FakeLockerPlugin()); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
| 210 | + $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $lazySearchBackend = new LazySearchBackend(); |
|
| 214 | + $this->server->addPlugin(new SearchPlugin($lazySearchBackend)); |
|
| 215 | + |
|
| 216 | + // wait with registering these until auth is handled and the filesystem is setup |
|
| 217 | + $this->server->on('beforeMethod', function () use ($root, $lazySearchBackend) { |
|
| 218 | + // custom properties plugin must be the last one |
|
| 219 | + $userSession = \OC::$server->getUserSession(); |
|
| 220 | + $user = $userSession->getUser(); |
|
| 221 | + if ($user !== null) { |
|
| 222 | + $view = \OC\Files\Filesystem::getView(); |
|
| 223 | + $this->server->addPlugin( |
|
| 224 | + new FilesPlugin( |
|
| 225 | + $this->server->tree, |
|
| 226 | + \OC::$server->getConfig(), |
|
| 227 | + $this->request, |
|
| 228 | + \OC::$server->getPreviewManager(), |
|
| 229 | + false, |
|
| 230 | + !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
| 231 | + ) |
|
| 232 | + ); |
|
| 233 | + |
|
| 234 | + $this->server->addPlugin( |
|
| 235 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
| 236 | + new CustomPropertiesBackend( |
|
| 237 | + $this->server->tree, |
|
| 238 | + \OC::$server->getDatabaseConnection(), |
|
| 239 | + \OC::$server->getUserSession()->getUser() |
|
| 240 | + ) |
|
| 241 | + ) |
|
| 242 | + ); |
|
| 243 | + if ($view !== null) { |
|
| 244 | + $this->server->addPlugin( |
|
| 245 | + new QuotaPlugin($view, false)); |
|
| 246 | + } |
|
| 247 | + $this->server->addPlugin( |
|
| 248 | + new TagsPlugin( |
|
| 249 | + $this->server->tree, \OC::$server->getTagManager() |
|
| 250 | + ) |
|
| 251 | + ); |
|
| 252 | + // TODO: switch to LazyUserFolder |
|
| 253 | + $userFolder = \OC::$server->getUserFolder(); |
|
| 254 | + $this->server->addPlugin(new SharesPlugin( |
|
| 255 | + $this->server->tree, |
|
| 256 | + $userSession, |
|
| 257 | + $userFolder, |
|
| 258 | + \OC::$server->getShareManager() |
|
| 259 | + )); |
|
| 260 | + $this->server->addPlugin(new CommentPropertiesPlugin( |
|
| 261 | + \OC::$server->getCommentsManager(), |
|
| 262 | + $userSession |
|
| 263 | + )); |
|
| 264 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
| 265 | + if ($view !== null) { |
|
| 266 | + $this->server->addPlugin(new FilesReportPlugin( |
|
| 267 | + $this->server->tree, |
|
| 268 | + $view, |
|
| 269 | + \OC::$server->getSystemTagManager(), |
|
| 270 | + \OC::$server->getSystemTagObjectMapper(), |
|
| 271 | + \OC::$server->getTagManager(), |
|
| 272 | + $userSession, |
|
| 273 | + \OC::$server->getGroupManager(), |
|
| 274 | + $userFolder, |
|
| 275 | + \OC::$server->getAppManager() |
|
| 276 | + )); |
|
| 277 | + $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend( |
|
| 278 | + $this->server->tree, |
|
| 279 | + $user, |
|
| 280 | + \OC::$server->getRootFolder(), |
|
| 281 | + \OC::$server->getShareManager(), |
|
| 282 | + $view |
|
| 283 | + )); |
|
| 284 | + } |
|
| 285 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin( |
|
| 286 | + \OC::$server->getConfig(), |
|
| 287 | + \OC::$server->query(BirthdayService::class) |
|
| 288 | + )); |
|
| 289 | + $this->server->addPlugin(new AppleProvisioningPlugin( |
|
| 290 | + \OC::$server->getUserSession(), |
|
| 291 | + \OC::$server->getURLGenerator(), |
|
| 292 | + \OC::$server->getThemingDefaults(), |
|
| 293 | + \OC::$server->getRequest(), |
|
| 294 | + \OC::$server->getL10N('dav'), |
|
| 295 | + function() { |
|
| 296 | + return UUIDUtil::getUUID(); |
|
| 297 | + } |
|
| 298 | + )); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + // register plugins from apps |
|
| 302 | + $pluginManager = new PluginManager( |
|
| 303 | + \OC::$server, |
|
| 304 | + \OC::$server->getAppManager() |
|
| 305 | + ); |
|
| 306 | + foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
| 307 | + $this->server->addPlugin($appPlugin); |
|
| 308 | + } |
|
| 309 | + foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
| 310 | + $root->addChild($appCollection); |
|
| 311 | + } |
|
| 312 | + }); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + public function exec() { |
|
| 316 | + $this->server->exec(); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + private function requestIsForSubtree(array $subTrees): bool { |
|
| 320 | + foreach ($subTrees as $subTree) { |
|
| 321 | + $subTree = trim($subTree, ' /'); |
|
| 322 | + if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) { |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | 328 | } |