@@ -119,14 +119,14 @@ public function getPropertyDefinitionsForScope($href, $path) {
119119 new SearchPropertyDefinition (FilesPlugin::SIZE_PROPERTYNAME , true , true , true , SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER ),
120120 new SearchPropertyDefinition (TagsPlugin::FAVORITE_PROPERTYNAME , true , true , true , SearchPropertyDefinition::DATATYPE_BOOLEAN ),
121121 new SearchPropertyDefinition (FilesPlugin::INTERNAL_FILEID_PROPERTYNAME , true , true , false , SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER ),
122+ new SearchPropertyDefinition (FilesPlugin::OWNER_ID_PROPERTYNAME , true , true , false ),
122123
123124 // select only properties
124125 new SearchPropertyDefinition ('{DAV:}resourcetype ' , false , true , false ),
125126 new SearchPropertyDefinition ('{DAV:}getcontentlength ' , false , true , false ),
126127 new SearchPropertyDefinition (FilesPlugin::CHECKSUMS_PROPERTYNAME , false , true , false ),
127128 new SearchPropertyDefinition (FilesPlugin::PERMISSIONS_PROPERTYNAME , false , true , false ),
128129 new SearchPropertyDefinition (FilesPlugin::GETETAG_PROPERTYNAME , false , true , false ),
129- new SearchPropertyDefinition (FilesPlugin::OWNER_ID_PROPERTYNAME , false , true , false ),
130130 new SearchPropertyDefinition (FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME , false , true , false ),
131131 new SearchPropertyDefinition (FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME , false , true , false ),
132132 new SearchPropertyDefinition (FilesPlugin::HAS_PREVIEW_PROPERTYNAME , false , true , false , SearchPropertyDefinition::DATATYPE_BOOLEAN ),
@@ -169,10 +169,12 @@ public function search(Query $search) {
169169 return new SearchResult ($ davNode , $ path );
170170 }, $ results );
171171
172- // Sort again, since the result from multiple storages is appended and not sorted
173- usort ($ nodes , function (SearchResult $ a , SearchResult $ b ) use ($ search ) {
174- return $ this ->sort ($ a , $ b , $ search ->orderBy );
175- });
172+ if (!$ query ->limitToHome ()) {
173+ // Sort again, since the result from multiple storages is appended and not sorted
174+ usort ($ nodes , function (SearchResult $ a , SearchResult $ b ) use ($ search ) {
175+ return $ this ->sort ($ a , $ b , $ search ->orderBy );
176+ });
177+ }
176178
177179 // If a limit is provided use only return that number of files
178180 if ($ search ->limit ->maxResults !== 0 ) {
@@ -267,11 +269,29 @@ private function getHrefForNode(Node $node) {
267269 * @param Query $query
268270 * @return ISearchQuery
269271 */
270- private function transformQuery (Query $ query ) {
272+ private function transformQuery (Query $ query ): ISearchQuery {
271273 // TODO offset
272274 $ limit = $ query ->limit ;
273275 $ orders = array_map ([$ this , 'mapSearchOrder ' ], $ query ->orderBy );
274- return new SearchQuery ($ this ->transformSearchOperation ($ query ->where ), (int )$ limit ->maxResults , 0 , $ orders , $ this ->user );
276+
277+ $ limitHome = false ;
278+ $ ownerProp = $ this ->extractWhereValue ($ query ->where , FilesPlugin::OWNER_ID_PROPERTYNAME , Operator::OPERATION_EQUAL );
279+ if ($ ownerProp !== null ) {
280+ if ($ ownerProp === $ this ->user ->getUID ()) {
281+ $ limitHome = true ;
282+ } else {
283+ throw new \InvalidArgumentException ("Invalid search value for '{http://owncloud.org/ns}owner-id', only the current user id is allowed " );
284+ }
285+ }
286+
287+ return new SearchQuery (
288+ $ this ->transformSearchOperation ($ query ->where ),
289+ (int )$ limit ->maxResults ,
290+ 0 ,
291+ $ orders ,
292+ $ this ->user ,
293+ $ limitHome
294+ );
275295 }
276296
277297 /**
@@ -360,4 +380,52 @@ private function castValue(SearchPropertyDefinition $property, $value) {
360380 return $ value ;
361381 }
362382 }
383+
384+ /**
385+ * Get a specific property from the were clause
386+ */
387+ private function extractWhereValue (Operator &$ operator , string $ propertyName , string $ comparison , bool $ acceptableLocation = true ): ?string {
388+ switch ($ operator ->type ) {
389+ case Operator::OPERATION_AND :
390+ case Operator::OPERATION_OR :
391+ case Operator::OPERATION_NOT :
392+ foreach ($ operator ->arguments as &$ argument ) {
393+ $ value = $ this ->extractWhereValue ($ argument , $ propertyName , $ comparison , $ acceptableLocation && $ operator ->type === Operator::OPERATION_AND );
394+ if ($ value !== null ) {
395+ return $ value ;
396+ }
397+ }
398+ return null ;
399+ case Operator::OPERATION_EQUAL :
400+ case Operator::OPERATION_GREATER_OR_EQUAL_THAN :
401+ case Operator::OPERATION_GREATER_THAN :
402+ case Operator::OPERATION_LESS_OR_EQUAL_THAN :
403+ case Operator::OPERATION_LESS_THAN :
404+ case Operator::OPERATION_IS_LIKE :
405+ if ($ operator ->arguments [0 ]->name === $ propertyName ) {
406+ if ($ operator ->type === $ comparison ) {
407+ if ($ acceptableLocation ) {
408+ if ($ operator ->arguments [1 ] instanceof Literal) {
409+ $ value = $ operator ->arguments [1 ]->value ;
410+
411+ // to remove the comparison from the query, we replace it with an empty AND
412+ $ operator = new Operator (Operator::OPERATION_AND );
413+
414+ return $ value ;
415+ } else {
416+ throw new \InvalidArgumentException ("searching by ' $ propertyName' is only allowed with a literal value " );
417+ }
418+ } else {
419+ throw new \InvalidArgumentException ("searching by ' $ propertyName' is not allowed inside a '{DAV:}or' or '{DAV:}not' " );
420+ }
421+ } else {
422+ throw new \InvalidArgumentException ("searching by ' $ propertyName' is only allowed inside a ' $ comparison' " );
423+ }
424+ } else {
425+ return null ;
426+ }
427+ default :
428+ return null ;
429+ }
430+ }
363431}
0 commit comments