-
Notifications
You must be signed in to change notification settings - Fork 11
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e4ea56
Merge pull request #69 from one2tek/2.x
gentritabazi c343c15
update check for filtering column from include
Angelinsky7 0b005b9
Directly use the relationship info from eloquent and support old syntax
Angelinsky7 0cc708c
Merge pull request #73 from one2tek/2.x
gentritabazi 9deb770
Merge branch 'master' of github.com:one2tek/larapi
Angelinsky7 3f45cd1
complete filtering and documenation
Angelinsky7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put extra line here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before line |
||
$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)); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
: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.