-
-
Notifications
You must be signed in to change notification settings - Fork 835
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
fix: filter values are not validated #3795
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
31eb004
fix: filter values are not validated
SychO9 a97729b
fix(regression): group gambit broken
SychO9 00fad13
chore(review): improve code
SychO9 cfb5c66
chore(review): return a `0` offset rather than quitting
SychO9 1ae4ea2
chore: phpstan return types
SychO9 c0b4add
feat: filtering posts per many tags
SychO9 b97ae96
chore: avoid nesting
SychO9 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
SychO9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* | ||
* This file is part of Flarum. | ||
* | ||
* For detailed copyright and license information, please view the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Flarum\Filter; | ||
|
||
use Flarum\Foundation\ValidationException as FlarumValidationException; | ||
use Flarum\Locale\Translator; | ||
|
||
trait ValidateFilterTrait | ||
{ | ||
/** | ||
* @throws FlarumValidationException | ||
* @return array<string>|array<array> | ||
*/ | ||
protected function asStringArray($filterValue, bool $multidimensional = false): array | ||
{ | ||
if (is_array($filterValue)) { | ||
$value = array_map(function ($subValue) use ($multidimensional) { | ||
if (is_array($subValue) && ! $multidimensional) { | ||
SychO9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->throwValidationException('core.api.invalid_filter_type.must_not_be_multidimensional_array_message'); | ||
} elseif (is_array($subValue)) { | ||
return $this->asStringArray($subValue, true); | ||
} else { | ||
return $this->asString($subValue); | ||
} | ||
}, $filterValue); | ||
} else { | ||
$value = explode(',', $this->asString($filterValue)); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asString($filterValue): string | ||
{ | ||
if (is_array($filterValue)) { | ||
$this->throwValidationException('core.api.invalid_filter_type.must_not_be_array_message'); | ||
} | ||
|
||
return trim($filterValue, '"'); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asInt($filterValue): int | ||
{ | ||
if (! is_numeric($filterValue)) { | ||
$this->throwValidationException('core.api.invalid_filter_type.must_be_numeric_message'); | ||
} | ||
|
||
return (int) $this->asString($filterValue); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
* @return array<int> | ||
*/ | ||
protected function asIntArray($filterValue): array | ||
{ | ||
return array_map(function ($value) { | ||
return $this->asInt($value); | ||
}, $this->asStringArray($filterValue)); | ||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
protected function asBool($filterValue): bool | ||
{ | ||
return $this->asString($filterValue) === '1'; | ||
SychO9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @throws FlarumValidationException | ||
*/ | ||
private function throwValidationException(string $messageCode): void | ||
{ | ||
$translator = resolve(Translator::class); | ||
|
||
throw new FlarumValidationException([ | ||
'message' => $translator->trans($messageCode, ['{filter}' => $this->getFilterKey()]), | ||
]); | ||
} | ||
} |
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
Oops, something went wrong.
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.
If this is an intArray, shouldn't it be
whereIn
?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.
nice catch! technically I should have kept it an
int
rather than anarray<int>
. But nothing wrong with allowing filtering per many tags.