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 $additionalFilter; |
16
|
|
|
|
17
|
|
|
private $filterKey; |
18
|
|
|
|
19
|
|
|
private $finalFilterIds; |
20
|
|
|
|
21
|
|
|
private $logger; |
22
|
|
|
|
23
|
5 |
|
public function __construct() |
24
|
|
|
{ |
25
|
5 |
|
$this->idsMustBeSubset = true; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
5 |
|
public function setFiltering($filtering) |
29
|
|
|
{ |
30
|
5 |
|
$this->filtering = $filtering; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
5 |
|
public function setObjectFilter(Filter $additionalFilter) |
34
|
|
|
{ |
35
|
5 |
|
$this->additionalFilter = $additionalFilter; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function setFilterKey($filterKey) |
39
|
|
|
{ |
40
|
1 |
|
$this->filterKey = $filterKey; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
5 |
|
public function validateIds() |
44
|
|
|
{ |
45
|
5 |
|
$rawFilteredIds = $this->additionalFilter->getIds(); |
46
|
|
|
|
47
|
|
|
// guard |
48
|
5 |
|
$chiave = key($this->filtering); |
49
|
5 |
|
$chiaveEsplosa = explode('|' , $chiave); |
50
|
5 |
|
if (!isset($chiaveEsplosa[1]) || !in_array($chiaveEsplosa[1], ['list', 'nlist'])) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// check |
55
|
5 |
|
foreach ($this->filtering as $key => $queryStringIds) { |
56
|
5 |
|
$querystringIds = explode(',', $queryStringIds); |
57
|
5 |
|
$additionalFiltersIds = explode(',', $this->additionalFilter->getIds()); |
58
|
5 |
|
foreach ($querystringIds as $requestedId) { |
59
|
|
|
|
60
|
|
|
// list + list |
61
|
5 |
|
if ($this->additionalFilter->getOperator() == 'list') { |
62
|
2 |
|
if (!in_array($requestedId, $additionalFiltersIds)) { |
63
|
1 |
|
throw new ForbiddenContentException( |
64
|
1 |
|
'Oops! Forbidden requested id ' . $requestedId |
65
|
1 |
|
. ' is not available. ' |
66
|
1 |
|
. 'Available are ' . join(', ', $additionalFiltersIds) . '' |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// list + nlist |
72
|
4 |
|
if ($this->additionalFilter->getOperator() == 'nlist') { |
73
|
3 |
|
$queryStringOperator = explode('|', key($this->filtering)); |
|
|
|
|
74
|
3 |
|
if (array_intersect($querystringIds, $additionalFiltersIds) == []) { |
75
|
3 |
|
$this->filterKey = str_replace('nlist', 'list', $this->additionalFilter->getRawFilter()); |
76
|
3 |
|
$rawFilteredIds = join(',', $querystringIds); |
77
|
4 |
|
$this->idsMustBeSubset = false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
// nlist + list |
83
|
|
|
|
84
|
|
|
// nlist + nlist |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
$this->finalFilterIds = $rawFilteredIds; |
90
|
|
|
|
91
|
4 |
|
if (true == $this->idsMustBeSubset) { |
|
|
|
|
92
|
1 |
|
foreach ($this->filtering as $key => $queryStringIds) { |
93
|
1 |
|
$querystringIds = explode(',', $queryStringIds); |
94
|
1 |
|
$additionalFiltersIds = explode(',', $this->additionalFilter->getIds()); |
95
|
1 |
|
$this->finalFilterIds = join(',', array_intersect($querystringIds, $additionalFiltersIds)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
4 |
|
} |
99
|
|
|
|
100
|
2 |
|
public function getFilterKey() |
101
|
|
|
{ |
102
|
2 |
|
return $this->filterKey; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function getFinalFilterIds() |
106
|
|
|
{ |
107
|
2 |
|
return $this->finalFilterIds; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setLogger(LoggerInterface $logger) |
111
|
|
|
{ |
112
|
|
|
$this->logger = $logger; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function log($message) |
116
|
|
|
{ |
117
|
|
|
if ($this->logger) { |
118
|
|
|
$this->logger->critical($message); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|