Restrict host and service lists when adding new or editing old nodes

resolves #67
This commit is contained in:
Johannes Meyer 2019-02-19 13:37:18 +01:00
parent d1f32c59f1
commit 68aedc3dce
3 changed files with 115 additions and 90 deletions

View file

@ -6,6 +6,7 @@ use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\ImportedNode;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\MonitoringRestrictions;
use Icinga\Module\Businessprocess\Storage\Storage;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator;
@ -14,6 +15,8 @@ use Icinga\Web\Session\SessionNamespace;
class AddNodeForm extends QuickForm
{
use MonitoringRestrictions;
/** @var MonitoringBackend */
protected $backend;
@ -329,9 +332,13 @@ class AddNodeForm extends QuickForm
protected function enumHostForServiceList()
{
$names = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
))->order('host_name')->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('hostStatus', ['hostname' => 'host_name'])
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('host_name')
->getQuery()
->fetchColumn();
// fetchPairs doesn't seem to work when using the same column with
// different aliases twice
@ -341,9 +348,13 @@ class AddNodeForm extends QuickForm
protected function enumHostList()
{
$names = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
))->order('host_name')->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('hostStatus', ['hostname' => 'host_name'])
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('host_name')
->getQuery()
->fetchColumn();
// fetchPairs doesn't seem to work when using the same column with
// different aliases twice
@ -358,12 +369,14 @@ class AddNodeForm extends QuickForm
protected function enumServiceList($host)
{
$query = $this->backend->select()->from(
'serviceStatus',
array('service' => 'service_description')
)->where('host_name', $host);
$query->order('service_description');
$names = $query->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('serviceStatus', ['service' => 'service_description'])
->where('host_name', $host)
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('service_description')
->getQuery()
->fetchColumn();
$services = array();
foreach ($names as $name) {
@ -440,39 +453,6 @@ class AddNodeForm extends QuickForm
}
}
protected function fetchObjectList()
{
$this->objectList = array();
$hosts = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
'in_downtime' => 'host_in_downtime',
'ack' => 'host_acknowledged',
'state' => 'host_state'
))->order('host_name')->getQuery()->fetchAll();
$services = $this->backend->select()->from('serviceStatus', array(
'hostname' => 'host_name',
'service' => 'service_description',
'in_downtime' => 'service_in_downtime',
'ack' => 'service_acknowledged',
'state' => 'service_state'
))->order('host_name')->order('service_description')->getQuery()->fetchAll();
foreach ($hosts as $host) {
$this->objectList[$host->hostname] = array(
$host->hostname . ';Hoststatus' => 'Host Status'
);
}
foreach ($services as $service) {
$this->objectList[$service->hostname][
$service->hostname . ';' . $service->service
] = $service->service;
}
return $this;
}
public function onSuccess()
{
$changes = ProcessChanges::construct($this->bp, $this->session);

View file

@ -5,6 +5,7 @@ namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Modification\ProcessChanges;
use Icinga\Module\Businessprocess\MonitoringRestrictions;
use Icinga\Module\Businessprocess\Node;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\Validator\NoDuplicateChildrenValidator;
@ -13,6 +14,8 @@ use Icinga\Web\Session\SessionNamespace;
class EditNodeForm extends QuickForm
{
use MonitoringRestrictions;
/** @var MonitoringBackend */
protected $backend;
@ -283,9 +286,13 @@ class EditNodeForm extends QuickForm
protected function enumHostForServiceList()
{
$names = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
))->order('host_name')->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('hostStatus', ['hostname' => 'host_name'])
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('host_name')
->getQuery()
->fetchColumn();
// fetchPairs doesn't seem to work when using the same column with
// different aliases twice
@ -295,9 +302,13 @@ class EditNodeForm extends QuickForm
protected function enumHostList()
{
$names = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
))->order('host_name')->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('hostStatus', ['hostname' => 'host_name'])
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('host_name')
->getQuery()
->fetchColumn();
// fetchPairs doesn't seem to work when using the same column with
// different aliases twice
@ -312,12 +323,14 @@ class EditNodeForm extends QuickForm
protected function enumServiceList($host)
{
$query = $this->backend->select()->from(
'serviceStatus',
array('service' => 'service_description')
)->where('host_name', $host);
$query->order('service_description');
$names = $query->getQuery()->fetchColumn();
$names = $this->backend
->select()
->from('serviceStatus', ['service' => 'service_description'])
->where('host_name', $host)
->applyFilter($this->getRestriction('monitoring/filter/objects'))
->order('service_description')
->getQuery()
->fetchColumn();
$services = array();
foreach ($names as $name) {
@ -367,39 +380,6 @@ class EditNodeForm extends QuickForm
}
}
protected function fetchObjectList()
{
$this->objectList = array();
$hosts = $this->backend->select()->from('hostStatus', array(
'hostname' => 'host_name',
'in_downtime' => 'host_in_downtime',
'ack' => 'host_acknowledged',
'state' => 'host_state'
))->order('host_name')->getQuery()->fetchAll();
$services = $this->backend->select()->from('serviceStatus', array(
'hostname' => 'host_name',
'service' => 'service_description',
'in_downtime' => 'service_in_downtime',
'ack' => 'service_acknowledged',
'state' => 'service_state'
))->order('host_name')->order('service_description')->getQuery()->fetchAll();
foreach ($hosts as $host) {
$this->objectList[$host->hostname] = array(
$host->hostname . ';Hoststatus' => 'Host Status'
);
}
foreach ($services as $service) {
$this->objectList[$service->hostname][
$service->hostname . ';' . $service->service
] = $service->service;
}
return $this;
}
/**
* @param Node $node
* @return $this

View file

@ -0,0 +1,65 @@
<?php
namespace Icinga\Module\Businessprocess;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\QueryException;
trait MonitoringRestrictions
{
/**
* Return a filter for the given restriction
*
* @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)
{
// Borrowed from Icinga\Module\Monitoring\Controller
$restriction = Filter::matchAny();
$restriction->setAllowedFilterColumns(array(
'host_name',
'hostgroup_name',
'instance_name',
'service_description',
'servicegroup_name',
function ($c) {
return preg_match('/^_(?:host|service)_/i', $c);
}
));
foreach (Auth::getInstance()->getRestrictions($name) as $filter) {
if ($filter === '*') {
return Filter::matchAny();
}
try {
$restriction->addFilter(Filter::fromQueryString($filter));
} catch (QueryException $e) {
throw new ConfigurationError(
mt(
'monitoring',
'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'
),
$name,
$filter,
implode(', ', array(
'instance_name',
'host_name',
'hostgroup_name',
'service_description',
'servicegroup_name',
'_(host|service)_<customvar-name>'
)),
$e
);
}
}
return $restriction;
}
}