1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Services; |
4
|
|
|
|
5
|
|
|
use Mado\QueryBundle\Exceptions\ForbiddenContentException; |
6
|
|
|
use Mado\QueryBundle\Objects\Filter; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class IdsChecker |
10
|
|
|
{ |
11
|
|
|
private $idsMustBeSubset = true; |
12
|
|
|
|
13
|
|
|
private $filtering; |
14
|
|
|
|
15
|
|
|
private $additionalFiltersIds; |
16
|
|
|
|
17
|
|
|
private $objectFilter; |
18
|
|
|
|
19
|
|
|
private $filterKey; |
20
|
|
|
|
21
|
|
|
private $finalFilterIds; |
22
|
|
|
|
23
|
|
|
private $logger; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->idsMustBeSubset = true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setLogger(LoggerInterface $logger) |
31
|
|
|
{ |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setFiltering($filtering) |
36
|
|
|
{ |
37
|
|
|
$this->filtering = $filtering; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setAdditionalFiltersIds($additionalFiltersIds) |
41
|
|
|
{ |
42
|
|
|
$this->additionalFiltersIds = $additionalFiltersIds; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setObjectFilter(Filter $objectFilter) |
46
|
|
|
{ |
47
|
|
|
$this->objectFilter = $objectFilter; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setFilterKey($filterKey) |
51
|
|
|
{ |
52
|
|
|
$this->filterKey = $filterKey; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function validateIds() |
56
|
|
|
{ |
57
|
|
|
$this->log('Start to check IDS'); |
58
|
|
|
|
59
|
|
|
$rawFilteredIds = $this->objectFilter->getIds(); |
60
|
|
|
|
61
|
|
|
foreach ($this->filtering as $key => $queryStringIds) { |
62
|
|
|
$qsIds = explode(',', $queryStringIds); |
63
|
|
|
$addFilIds = explode(',', $this->additionalFiltersIds); |
64
|
|
|
foreach ($qsIds as $requestedId) { |
65
|
|
|
if ($this->objectFilter->getOperator() == 'list') { |
66
|
|
|
if (!in_array($requestedId, $addFilIds)) { |
67
|
|
|
throw new ForbiddenContentException( |
68
|
|
|
'Oops! Forbidden requested id ' . $requestedId |
69
|
|
|
. ' is not available. ' |
70
|
|
|
. 'Available are "' . join(', ', $addFilIds) . '"' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($this->objectFilter->getOperator() == 'nlist') { |
76
|
|
|
$queryStringOperator = explode('|', key($this->filtering)); |
|
|
|
|
77
|
|
|
if (array_intersect($qsIds, $addFilIds) == []) { |
78
|
|
|
$this->filterKey = str_replace('nlist', 'list', $this->objectFilter->getRawFilter()); |
79
|
|
|
$rawFilteredIds = join(',', $qsIds); |
80
|
|
|
$this->idsMustBeSubset = false; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->finalFilterIds = $rawFilteredIds; |
87
|
|
|
|
88
|
|
|
if (true == $this->idsMustBeSubset) { |
|
|
|
|
89
|
|
|
foreach ($this->filtering as $key => $queryStringIds) { |
90
|
|
|
$qsIds = explode(',', $queryStringIds); |
91
|
|
|
$addFilIds = explode(',', $this->additionalFiltersIds); |
92
|
|
|
$this->finalFilterIds = join(',', array_intersect($qsIds, $addFilIds)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->log(var_export([ |
97
|
|
|
'filterKey' => $this->getFilterKey(), |
98
|
|
|
'finalFilterIds' => $this->getFinalFilterIds(), |
99
|
|
|
], true)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getFilterKey() |
103
|
|
|
{ |
104
|
|
|
return $this->filterKey; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getFinalFilterIds() |
108
|
|
|
{ |
109
|
|
|
return $this->finalFilterIds; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function log($message) |
113
|
|
|
{ |
114
|
|
|
if ($this->logger) { |
115
|
|
|
$this->logger->debug('[IdsChecker] - ' . $message); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|