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

[BUG] fix permission check in GridHelperService for wrong query column if type is asset #793

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Changes from 2 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
28 changes: 17 additions & 11 deletions src/Helper/GridHelperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ public function getFilterCondition(string $filterJson, ClassDefinition $class, ?
$fieldConditions = [];
foreach ($filter['value'] as $filterValue) {
$brickCondition = '(' . $brickField->getFilterCondition($filterValue, $operator,
['brickPrefix' => $brickPrefix]
) . ' AND ' . $brickType . '.fieldname = ' . $db->quote($brickFilterField) . ')';
['brickPrefix' => $brickPrefix]
) . ' AND ' . $brickType . '.fieldname = ' . $db->quote($brickFilterField) . ')';
$fieldConditions[] = $brickCondition;
}

Expand All @@ -325,7 +325,7 @@ public function getFilterCondition(string $filterJson, ClassDefinition $class, ?
}
} else {
$brickCondition = '(' . $brickField->getFilterCondition($filter['value'], $operator,
['brickPrefix' => $brickPrefix]) . ' AND ' . $brickType . '.fieldname = ' . $db->quote($brickFilterField) . ')';
['brickPrefix' => $brickPrefix]) . ' AND ' . $brickType . '.fieldname = ' . $db->quote($brickFilterField) . ')';
$conditionPartsFilters[] = $brickCondition;
}
} elseif ($field instanceof ClassDefinition\Data\UrlSlug) {
Expand Down Expand Up @@ -926,14 +926,15 @@ public function createXlsxExportFile(FilesystemOperator $storage, string $fileHa
/**
* A more performant alternative to "CONCAT(`path`,`key`) LIKE $fullpath"
*/
private function optimizedConcatLike(string $fullpath): string
private function optimizedConcatLike(string $fullpath, string $type = 'object'): string
{
$pathParts = explode('/', $fullpath);
$leaf = array_pop($pathParts);
$path = implode('/', $pathParts);
$queryColumn = $type === 'asset' ? '`filename`' : '`key`';

return '(
(`path` = "' . $path . '/" AND `key` = "' . $leaf . '")
(`path` = "' . $path . '/" AND ' . $queryColumn . ' = "' . $leaf . '")
OR
`path` LIKE "' . $fullpath . '%"
)';
Expand All @@ -943,20 +944,25 @@ private function optimizedConcatLike(string $fullpath): string
* A more performant alternative to "CONCAT(`path`,`key`) NOT LIKE $fullpath"
* Set $onlyChildren to true when you want to exclude the folder/element itself
*/
private function optimizedConcatNotLike(string $fullpath, bool $onlyChildren = false): string
private function optimizedConcatNotLike(
string $fullpath,
bool $onlyChildren = false,
string $type = 'object'
): string
{
$pathParts = explode('/', $fullpath);
$leaf = array_pop($pathParts);
$path = implode('/', $pathParts);
$queryColumn = $type === 'asset' ? '`filename`' : '`key`';

if ($onlyChildren) {
return '`path` NOT LIKE "' . $fullpath . '/%"';
}

return '(
(`path` != "' . $path . '/" AND `key` != "' . $leaf . '")
(`path` != "' . $path . '/" AND ' . $queryColumn . ' != "' . $leaf . '")
AND
`path` NOT LIKE "' . $fullpath . '/%"
`path` NOT LIKE "' . $fullpath . '%"
fashxp marked this conversation as resolved.
Show resolved Hide resolved
)';

}
Expand All @@ -983,16 +989,16 @@ protected function getPermittedPathsByUser(string $type, User $user): string
if ($exceptionsConcat !== '') {
$exceptionsConcat.= ' OR ';
}
$exceptionsConcat.= $this->optimizedConcatLike($path);
$exceptionsConcat.= $this->optimizedConcatLike($path, $type);
}
$exceptions = ' OR (' . $exceptionsConcat . ')';
//if any allowed child is found, the current folder can be listed but its content is still blocked
$onlyChildren = true;
}
$forbiddenPathSql[] = $this->optimizedConcatNotLike($forbiddenPath, $onlyChildren) . $exceptions;
$forbiddenPathSql[] = $this->optimizedConcatNotLike($forbiddenPath, $onlyChildren, $type) . $exceptions;
}
foreach ($elementPaths['allowed'] as $allowedPaths) {
$allowedPathSql[] = $this->optimizedConcatLike($allowedPaths);
$allowedPathSql[] = $this->optimizedConcatLike($allowedPaths, $type);
}

// this is to avoid query error when implode is empty.
Expand Down
Loading