monitoring: Implement Controller::getRestriction()

Controller::applyRestriction() breaks fluent interfaces whereas Controller::getRestriction() doesn't.
This commit is contained in:
Eric Lippmann 2015-08-20 13:07:57 +02:00
parent e07cdd21ac
commit dcb1502286
1 changed files with 28 additions and 15 deletions

View File

@ -51,17 +51,33 @@ class Controller extends IcingaWebController
}
/**
* Apply a restriction on the given data view
* Apply a restriction of the authenticated on the given filterable
*
* @param string $restriction The name of restriction
* @param Filterable $view The filterable to restrict
* @param string $name Name of the restriction
* @param Filterable $filterable Filterable to restrict
*
* @return Filterable The filterable
* @return Filterable The filterable having the restriction applied
*/
protected function applyRestriction($restriction, Filterable $view)
protected function applyRestriction($name, Filterable $filterable)
{
$restrictions = Filter::matchAny();
$restrictions->setAllowedFilterColumns(array(
if (null !== $restriction = $restriction = $this->getRestriction($name)) {
$filterable->applyFilter($restriction);
}
return $filterable;
}
/**
* Get a restriction of the authenticated
*
* @param string $name Name of the restriction
*
* @return Filter|null Filter object or null if the authenticated user is not restricted
* @throws ConfigurationError If the restriction contains invalid filter columns
*/
protected function getRestriction($name)
{
$restriction = Filter::matchAny();
$restriction->setAllowedFilterColumns(array(
'host_name',
'hostgroup_name',
'service_description',
@ -70,19 +86,18 @@ class Controller extends IcingaWebController
return preg_match('/^_(?:host|service)_/', $c);
}
));
foreach ($this->getRestrictions($restriction) as $filter) {
foreach ($this->getRestrictions($name) as $filter) {
if ($filter === '*') {
return $view;
return null;
}
try {
$restrictions->addFilter(Filter::fromQueryString($filter));
$restriction->addFilter(Filter::fromQueryString($filter));
} catch (QueryException $e) {
throw new ConfigurationError(
$this->translate(
'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'
),
$restriction,
$name,
$filter,
implode(', ', array(
'host_name',
@ -95,9 +110,7 @@ class Controller extends IcingaWebController
);
}
}
$view->applyFilter($restrictions);
return $view;
return $restriction;
}
}