Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Update check for filtering column from include #70

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ Data filtering is very easy with at `Larapi` see the examples below.

By default, all filters have to be explicitly allowed using `$whiteListFilter` property in specified Model.

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
use HasFactory;

public function author()
{
return $this->belongsTo(Author::class);
}

// List of all valid syntax for $whiteListFilter
//public static $whiteListFilter = ['*'];
//public static $whiteListFilter = ['id', 'title', 'author'];
//public static $whiteListFilter = ['id', 'title', 'author.*'];

}
```

If the filter is `['*']` then all properties and sub-properties can be used for filtering.
If the filter is `a list of model properties` then only the selected properties can be filtered.
If some of the filter are a relationship then only the `$whiteListFilter` properties of the sub-property's model can be filtered.
If some of the filter contains a `.*` the all sub-properties of the relationship model can be filtered (the `$whiteListFilter` is not used).

Comment on lines +7 to +36
Copy link
Collaborator

@gentritabazi gentritabazi Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To much code here,

Can you update like this:


List of all valid syntax for $whiteListFilter:

public static $whiteListFilter = ['*'];
public static $whiteListFilter = ['id', 'title', 'author'];
public static $whiteListFilter = ['id', 'title', 'author.*'];  

If the filter is ['*'] then all properties and sub-properties can be used for filtering.
If the filter is a list of model properties then only the selected properties can be filtered.
If some of the filter are a relationship then only the $whiteListFilter properties of the sub-property's model can be filtered.
If some of the filter contains a .* the all sub-properties of the relationship model can be filtered.

For more advanced use cases, [custom filter](advanced_usage?id=custom-filter) can be used.

#### Operators
Expand Down
50 changes: 45 additions & 5 deletions src/Database/EloquentBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,14 @@ protected function applyFilter(Builder $queryBuilder, array $filter, $or = false
$operator = $filter['operator'] ?? 'eq';
$value = $filter['value'];
$not = $filter['not'] ?? false;
$whiteListFilter = (get_class_vars(get_class($queryBuilder->getModel()))['whiteListFilter']) ?? [];
$wantsRelationship = stripos($column, '.');
$clauseOperator = true;
$lastColumn = explode('.', $column);
$lastColumn = end($lastColumn);
$relationName = str_replace('.'. $lastColumn, '', $column);
$filterRawJoinColumns = isset($this->filterRawJoinColumns) ? $this->filterRawJoinColumns : [];

// Check if column can filered.
if (!in_array($column, $whiteListFilter)) {
throw new LarapiException('Oops! You cannot filter column '. $column. '.');
}
$this->checkFilterColumn($column, get_class($queryBuilder->getModel()));

// Check operator.
switch ($operator) {
Expand Down Expand Up @@ -284,6 +280,50 @@ protected function applyFilter(Builder $queryBuilder, array $filter, $or = false
}
}

private function checkFilterColumn(String $column, String $baseClassName, array $overrideWhiteListFilter = null)
{
if (empty($column) || empty($baseClassName)) {
return;
}

// Retrieve the whiteListFilter
$whiteListFilter = $overrideWhiteListFilter ?? ((array)(get_class_vars($baseClassName)['whiteListFilter']) ?? []);

// Check if the whitelist filter is a star
if (in_array('*', $whiteListFilter)) {
if (count($whiteListFilter) > 1) {
throw new LarapiException('Oops! If you use "*" for the whiteListFilter, you cannot specify another column on ' . $baseClassName . ' class.');
}
return;
}

// Check if full column can filered.
if (in_array($column, $whiteListFilter)) {
return;
}

$parts = explode('.', $column);
$firstPart = $parts[0];

$simpleColumnCheckInListFilter = in_array($firstPart, $whiteListFilter);
$complexColumnCheckInListFilter = in_array($firstPart . '.*', $whiteListFilter);

// Check if splitted column can filered.
if (!$simpleColumnCheckInListFilter && !$complexColumnCheckInListFilter) {
throw new LarapiException('Oops! You cannot filter column ' . $column . ' on ' . $baseClassName . ' class.');
}

// Get next part and next class
$nextColums = join('.', array_slice($parts, 1));
$baseClass = new $baseClassName();
$nextClass = method_exists($baseClass, $firstPart) ? get_class($baseClass->$firstPart()->getRelated()) : '';
// If the whiteListFilter contains a column with a star we want to bypass the check for the next part
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put extra line here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before line 320.

$nextOverrideWhiteListFilter = $complexColumnCheckInListFilter ? ['*'] : null;

// Recursive call to check sub parts
$this->checkFilterColumn($nextColums, $nextClass, $nextOverrideWhiteListFilter);
}

private function hasCustomMethod($type, $key)
{
$methodName = sprintf('%s%s', $type, Str::studly($key));
Expand Down