Skip to content

Commit da018af

Browse files
authored
Merge pull request #124 from Lomkit/fix/search-query-on-scout-request
🐛 search query limit applied on scout fetches
2 parents 095d0f7 + 93bb2af commit da018af

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

src/Http/Requests/RestRequest.php

+10
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ class RestRequest extends FormRequest
1010
{
1111
use InteractsWithRules;
1212
use Resourcable;
13+
14+
/**
15+
* Determine if scout mode is asked for the given request.
16+
*
17+
* @var bool
18+
*/
19+
public function isScoutMode()
20+
{
21+
return $this->has('search.text.value');
22+
}
1323
}

src/Query/Builder.php

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public function __construct(Resource $resource, \Illuminate\Database\Eloquent\Bu
3737
*/
3838
protected $resource;
3939

40+
/**
41+
* Determine if security should be disabled in case we don't want it.
42+
*
43+
* @var bool
44+
*/
45+
protected bool $disableSecurity = false;
46+
4047
/**
4148
* The query builder instance.
4249
*
@@ -49,6 +56,13 @@ public function newQueryBuilder($parameters)
4956
return app()->make(QueryBuilder::class, $parameters);
5057
}
5158

59+
public function disableSecurity($disable = true)
60+
{
61+
$this->disableSecurity = $disable;
62+
63+
return $this;
64+
}
65+
5266
/**
5367
* Convert the query builder to an Eloquent query builder.
5468
*

src/Query/ScoutBuilder.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ public function search(array $parameters = [])
3737
$this->applyInstructions($parameters['instructions']);
3838
});
3939

40-
// @TODO: instructions scout side ????
41-
4240
$this->queryBuilder
4341
->query(function (Builder $query) use ($parameters) {
4442
app()->make(QueryBuilder::class, ['query' => $query, 'resource' => $this->resource])
43+
->disableSecurity()
4544
->search(
4645
collect($parameters)
4746
->except([

src/Query/Traits/PerformSearch.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function search(array $parameters = [])
2020
{
2121
$this->resource->authorizeTo('viewAny', $this->resource::$model);
2222

23-
$this->resource->searchQuery(app()->make(RestRequest::class), $this->queryBuilder);
23+
$this->when(!$this->disableSecurity, function () {
24+
$this->resource->searchQuery(app()->make(RestRequest::class), $this->queryBuilder);
25+
});
2426

2527
// Here we run the filters in a subquery to avoid conflicts
2628
$this->when(isset($parameters['filters']), function () use ($parameters) {
@@ -57,8 +59,8 @@ public function search(array $parameters = [])
5759
$this->applyAggregates($parameters['aggregates']);
5860
});
5961

60-
// In case we are in a relation we don't apply the limits since we dont know how much records will be related.
61-
if (!$this->queryBuilder instanceof Relation) {
62+
// In case we are in a relation we don't apply the limits since we don't know how much records will be related.
63+
if (!$this->queryBuilder instanceof Relation && !$this->disableSecurity) {
6264
$this->queryBuilder->limit($parameters['limit'] ?? 50);
6365
}
6466

src/Rules/SearchRules.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ class SearchRules implements ValidationRule, ValidatorAwareRule
3535
*/
3636
protected RestRequest $request;
3737

38-
/**
39-
* Determine if scout mode is asked for the given request.
40-
*
41-
* @var bool
42-
*/
43-
public function isScoutMode()
44-
{
45-
return $this->request->has('search.text.value');
46-
}
47-
4838
/**
4939
* If the rules is specified at root level.
5040
*
@@ -137,7 +127,7 @@ public function textRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
137127
*/
138128
public function filtersRules(\Lomkit\Rest\Http\Resource $resource, string $prefix, bool $isMaxDepth = false)
139129
{
140-
$isScoutMode = $this->isScoutMode();
130+
$isScoutMode = $this->request->isScoutMode();
141131

142132
$operatorRules = $isScoutMode ?
143133
['=', 'in', 'not in'] :
@@ -193,7 +183,7 @@ public function filtersRules(\Lomkit\Rest\Http\Resource $resource, string $prefi
193183
*/
194184
protected function scopesRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
195185
{
196-
if ($this->isScoutMode()) {
186+
if ($this->request->isScoutMode()) {
197187
return [
198188
$prefix => 'prohibited',
199189
];
@@ -226,7 +216,7 @@ protected function instructionsRules(\Lomkit\Rest\Http\Resource $resource, strin
226216
{
227217
$instructionNames = Rule::in(
228218
collect(
229-
$this->isScoutMode() ?
219+
$this->request->isScoutMode() ?
230220
$resource->getScoutInstructions($this->request) :
231221
$resource->getInstructions($this->request)
232222
)
@@ -263,7 +253,7 @@ protected function instructionsRules(\Lomkit\Rest\Http\Resource $resource, strin
263253
*/
264254
protected function sortsRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
265255
{
266-
$fields = $this->isScoutMode() ?
256+
$fields = $this->request->isScoutMode() ?
267257
Rule::in($resource->getScoutFields($this->request)) :
268258
Rule::in($resource->getFields($this->request));
269259

0 commit comments

Comments
 (0)