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
|
6 |
|
public function __construct() |
26
|
|
|
{ |
27
|
6 |
|
$this->idsMustBeSubset = true; |
28
|
6 |
|
} |
29
|
|
|
|
30
|
|
|
/** @codeCoverageIgnore */ |
31
|
|
|
public function setLogger(LoggerInterface $logger) |
32
|
|
|
{ |
33
|
|
|
$this->logger = $logger; |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
public function setFiltering($filtering) |
37
|
|
|
{ |
38
|
4 |
|
$this->filtering = $filtering; |
39
|
4 |
|
} |
40
|
|
|
|
41
|
3 |
|
public function setAdditionalFiltersIds($additionalFiltersIds) |
42
|
|
|
{ |
43
|
3 |
|
$this->additionalFiltersIds = $additionalFiltersIds; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
5 |
|
public function setObjectFilter(Filter $objectFilter) |
47
|
|
|
{ |
48
|
5 |
|
$this->objectFilter = $objectFilter; |
49
|
5 |
|
} |
50
|
|
|
|
51
|
4 |
|
public function setFilterKey($filterKey) |
52
|
|
|
{ |
53
|
4 |
|
$this->filterKey = $filterKey; |
54
|
4 |
|
} |
55
|
|
|
|
56
|
6 |
|
public function validateIds() |
57
|
|
|
{ |
58
|
|
|
//$this->log('Start to check IDS'); |
59
|
|
|
|
60
|
6 |
|
if (!$this->objectFilter) { |
61
|
1 |
|
throw new \RuntimeException( |
62
|
1 |
|
'Oops! Missing GenericFilter object!!!' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
if (!$this->filtering) { |
67
|
1 |
|
throw new \RuntimeException( |
68
|
1 |
|
'Oops! Filtering is missing!!!' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
$rawFilteredIds = $this->objectFilter->getIds(); |
73
|
|
|
|
74
|
4 |
|
foreach ($this->filtering as $key => $queryStringIds) { |
75
|
4 |
|
$qsIds = explode(',', $queryStringIds); |
76
|
4 |
|
$addFilIds = explode(',', $this->additionalFiltersIds); |
77
|
4 |
|
foreach ($qsIds as $requestedId) { |
78
|
4 |
|
if ($this->objectFilter->getOperator() == 'list') { |
79
|
1 |
|
if (!in_array($requestedId, $addFilIds)) { |
80
|
1 |
|
throw new ForbiddenContentException( |
81
|
1 |
|
'Oops! Forbidden requested id ' . $requestedId |
82
|
1 |
|
. ' is not available. ' |
83
|
1 |
|
. 'Available are "' . join(', ', $addFilIds) . '"' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
if ($this->objectFilter->getOperator() == 'nlist') { |
89
|
1 |
|
$queryStringOperator = explode('|', key($this->filtering)); |
|
|
|
|
90
|
1 |
|
if (array_intersect($qsIds, $addFilIds) == []) { |
91
|
1 |
|
$this->filterKey = str_replace('nlist', 'list', $this->objectFilter->getRawFilter()); |
92
|
1 |
|
$rawFilteredIds = join(',', $qsIds); |
93
|
3 |
|
$this->idsMustBeSubset = false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
$this->finalFilterIds = $rawFilteredIds; |
100
|
|
|
|
101
|
3 |
|
if (true == $this->idsMustBeSubset) { |
|
|
|
|
102
|
2 |
|
foreach ($this->filtering as $key => $queryStringIds) { |
103
|
2 |
|
$qsIds = explode(',', $queryStringIds); |
104
|
2 |
|
$addFilIds = explode(',', $this->additionalFiltersIds); |
105
|
2 |
|
$this->finalFilterIds = join(',', array_intersect($qsIds, $addFilIds)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
$this->log(var_export([ |
110
|
3 |
|
'filterKey' => $this->getFilterKey(), |
111
|
3 |
|
'finalFilterIds' => $this->getFinalFilterIds(), |
112
|
3 |
|
], true)); |
113
|
3 |
|
} |
114
|
|
|
|
115
|
1 |
|
public function idsAreSubset() |
116
|
|
|
{ |
117
|
1 |
|
return $this->idsMustBeSubset; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
public function getFilterKey() |
121
|
|
|
{ |
122
|
3 |
|
return $this->filterKey; |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
public function getFinalFilterIds() |
126
|
|
|
{ |
127
|
3 |
|
return $this->finalFilterIds; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @codeCoverageIgnore */ |
131
|
|
|
public function log($message) |
132
|
|
|
{ |
133
|
|
|
if ($this->logger) { |
134
|
|
|
$this->logger->debug('[IdsChecker] - ' . $message); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|