-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow restricting reports by
name
& author
- Loading branch information
Showing
7 changed files
with
141 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* Icinga Reporting | (c) 2023 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Reporting\Common; | ||
|
||
use Icinga\Authentication\Auth as IcingaAuth; | ||
use Icinga\Exception\ConfigurationError; | ||
use ipl\Orm\Query; | ||
use ipl\Stdlib\Filter; | ||
use ipl\Stdlib\Filter\Rule; | ||
use ipl\Web\Filter\QueryString; | ||
|
||
trait Auth | ||
{ | ||
/** | ||
* Apply restrictions of this module | ||
* | ||
* @param Query $query | ||
*/ | ||
protected function applyRestrictions(Query $query): void | ||
{ | ||
$auth = IcingaAuth::getInstance(); | ||
$restrictions = $auth->getRestrictions('reporting/reports'); | ||
|
||
$queryFilter = Filter::any(); | ||
foreach ($restrictions as $restriction) { | ||
$queryFilter->add($this->parseRestriction($restriction, 'reporting/reports')); | ||
} | ||
|
||
$query->filter($queryFilter); | ||
} | ||
|
||
/** | ||
* Parse the query string of the given restriction | ||
* | ||
* @param string $queryString | ||
* @param string $restriction | ||
* @param ?callable $onCondition | ||
* | ||
* @return Rule | ||
*/ | ||
protected function parseRestriction( | ||
string $queryString, | ||
string $restriction, | ||
callable $onCondition = null | ||
): Filter\Rule { | ||
$parser = QueryString::fromString($queryString); | ||
if ($onCondition) { | ||
$parser->on(QueryString::ON_CONDITION, $onCondition); | ||
} | ||
|
||
return $parser->on( | ||
QueryString::ON_CONDITION, | ||
function (Filter\Condition $condition) use ($restriction, $queryString) { | ||
$allowedColumns = ['report.name', 'report.author']; | ||
if (in_array($condition->getColumn(), $allowedColumns, true)) { | ||
return; | ||
} | ||
|
||
throw new ConfigurationError( | ||
t( | ||
'Cannot apply restriction %s using the filter %s.' | ||
. ' You can only use the following columns: %s' | ||
), | ||
$restriction, | ||
$queryString, | ||
implode(', ', $allowedColumns) | ||
); | ||
} | ||
)->parse(); | ||
} | ||
} |
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