Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| JsonParameterFilter | |
95.00% |
19 / 20 |
|
50.00% |
1 / 2 |
8 | |
0.00% |
0 / 1 |
| filter | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| getDecodedUri | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\accessControl\filter; |
| 25 | |
| 26 | use common_Utils; |
| 27 | use tao_helpers_Uri; |
| 28 | |
| 29 | class JsonParameterFilter implements ParameterFilterInterface |
| 30 | { |
| 31 | public function filter(array $requestParameters, array $filterNames): array |
| 32 | { |
| 33 | if (empty($filterNames)) { |
| 34 | return []; |
| 35 | } |
| 36 | |
| 37 | $groupedUris = []; |
| 38 | |
| 39 | $json = (array)json_decode( |
| 40 | (string)(array_keys($requestParameters)[0] ?? ''), |
| 41 | true |
| 42 | ); |
| 43 | |
| 44 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 45 | return []; |
| 46 | } |
| 47 | |
| 48 | foreach ($json as $key => $value) { |
| 49 | if (in_array($key, $filterNames, true)) { |
| 50 | $encodedUri = $this->getDecodedUri((string)$value); |
| 51 | |
| 52 | if (common_Utils::isUri($encodedUri)) { |
| 53 | $groupedUris[$key][] = $encodedUri; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return $groupedUris; |
| 59 | } |
| 60 | |
| 61 | private function getDecodedUri(string $uri): string |
| 62 | { |
| 63 | // This is necessary cause JSON request might have converted '.' to '_' |
| 64 | $decodedUri = str_replace('_', '.', $uri); |
| 65 | |
| 66 | $decodedUri = tao_helpers_Uri::isUriEncoded($decodedUri) |
| 67 | ? tao_helpers_Uri::decode($decodedUri) |
| 68 | : $decodedUri; |
| 69 | |
| 70 | return $decodedUri; |
| 71 | } |
| 72 | } |