From 92fddf5b5e489fccb84b8b83f658082a4bf2079a Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 4 Jul 2023 10:10:53 +0000
Subject: [PATCH] unify checking methods, fixes #2815

Closes #2815

Merge request studip/studip!1897
---
 .../JsonApiIntegration/QueryChecker.php       | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/lib/classes/JsonApi/JsonApiIntegration/QueryChecker.php b/lib/classes/JsonApi/JsonApiIntegration/QueryChecker.php
index 045598b510f..d1b209a4a3f 100644
--- a/lib/classes/JsonApi/JsonApiIntegration/QueryChecker.php
+++ b/lib/classes/JsonApi/JsonApiIntegration/QueryChecker.php
@@ -104,16 +104,15 @@ class QueryChecker
 
     protected function checkSorting(ErrorCollection $errors, QueryParserInterface $queryParser): void
     {
-        $sorts = iterator_to_array($queryParser->getSorts());
-        if (null !== $sorts && null !== $this->sortParameters) {
-            foreach (array_keys($sorts) as $sortParameter) {
-                if (!array_key_exists($sortParameter, $this->sortParameters)) {
-                    $errors->addQueryParameterError(
-                        QueryParser::PARAM_SORT,
-                        sprintf('Sort parameter %s is not allowed.', $sortParameter)
-                    );
-                }
-            }
+        $withinAllowed = $this->keysWithinAllowed(
+            iterator_to_array($queryParser->getSorts()),
+            $this->sortParameters
+        );
+        if (!$withinAllowed) {
+            $errors->addQueryParameterError(
+                QueryParser::PARAM_SORT,
+                'Sort parameter should contain only allowed values.'
+            );
         }
     }
 
-- 
GitLab