Skip to content

Commit

Permalink
Merge pull request #124 from Lomkit/fix/search-query-on-scout-request
Browse files Browse the repository at this point in the history
🐛 search query limit applied on scout fetches
  • Loading branch information
GautierDele authored Jul 1, 2024
2 parents 095d0f7 + 93bb2af commit da018af
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/Http/Requests/RestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ class RestRequest extends FormRequest
{
use InteractsWithRules;
use Resourcable;

/**
* Determine if scout mode is asked for the given request.
*
* @var bool
*/
public function isScoutMode()
{
return $this->has('search.text.value');
}
}
14 changes: 14 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function __construct(Resource $resource, \Illuminate\Database\Eloquent\Bu
*/
protected $resource;

/**
* Determine if security should be disabled in case we don't want it.
*
* @var bool
*/
protected bool $disableSecurity = false;

/**
* The query builder instance.
*
Expand All @@ -49,6 +56,13 @@ public function newQueryBuilder($parameters)
return app()->make(QueryBuilder::class, $parameters);
}

public function disableSecurity($disable = true)
{
$this->disableSecurity = $disable;

return $this;
}

/**
* Convert the query builder to an Eloquent query builder.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Query/ScoutBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public function search(array $parameters = [])
$this->applyInstructions($parameters['instructions']);
});

// @TODO: instructions scout side ????

$this->queryBuilder
->query(function (Builder $query) use ($parameters) {
app()->make(QueryBuilder::class, ['query' => $query, 'resource' => $this->resource])
->disableSecurity()
->search(
collect($parameters)
->except([
Expand Down
8 changes: 5 additions & 3 deletions src/Query/Traits/PerformSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function search(array $parameters = [])
{
$this->resource->authorizeTo('viewAny', $this->resource::$model);

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

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

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

Expand Down
18 changes: 4 additions & 14 deletions src/Rules/SearchRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ class SearchRules implements ValidationRule, ValidatorAwareRule
*/
protected RestRequest $request;

/**
* Determine if scout mode is asked for the given request.
*
* @var bool
*/
public function isScoutMode()
{
return $this->request->has('search.text.value');
}

/**
* If the rules is specified at root level.
*
Expand Down Expand Up @@ -137,7 +127,7 @@ public function textRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
*/
public function filtersRules(\Lomkit\Rest\Http\Resource $resource, string $prefix, bool $isMaxDepth = false)
{
$isScoutMode = $this->isScoutMode();
$isScoutMode = $this->request->isScoutMode();

$operatorRules = $isScoutMode ?
['=', 'in', 'not in'] :
Expand Down Expand Up @@ -193,7 +183,7 @@ public function filtersRules(\Lomkit\Rest\Http\Resource $resource, string $prefi
*/
protected function scopesRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
{
if ($this->isScoutMode()) {
if ($this->request->isScoutMode()) {
return [
$prefix => 'prohibited',
];
Expand Down Expand Up @@ -226,7 +216,7 @@ protected function instructionsRules(\Lomkit\Rest\Http\Resource $resource, strin
{
$instructionNames = Rule::in(
collect(
$this->isScoutMode() ?
$this->request->isScoutMode() ?
$resource->getScoutInstructions($this->request) :
$resource->getInstructions($this->request)
)
Expand Down Expand Up @@ -263,7 +253,7 @@ protected function instructionsRules(\Lomkit\Rest\Http\Resource $resource, strin
*/
protected function sortsRules(\Lomkit\Rest\Http\Resource $resource, string $prefix)
{
$fields = $this->isScoutMode() ?
$fields = $this->request->isScoutMode() ?
Rule::in($resource->getScoutFields($this->request)) :
Rule::in($resource->getFields($this->request));

Expand Down

0 comments on commit da018af

Please sign in to comment.