From 68aedc3dceee364545193f81869fe3945c6ad6e4 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 19 Feb 2019 13:37:18 +0100 Subject: [PATCH] Restrict host and service lists when adding new or editing old nodes resolves #67 --- application/forms/AddNodeForm.php | 70 +++++++------------ application/forms/EditNodeForm.php | 70 +++++++------------ .../MonitoringRestrictions.php | 65 +++++++++++++++++ 3 files changed, 115 insertions(+), 90 deletions(-) create mode 100644 library/Businessprocess/MonitoringRestrictions.php diff --git a/application/forms/AddNodeForm.php b/application/forms/AddNodeForm.php index fe38d35..ed3199b 100644 --- a/application/forms/AddNodeForm.php +++ b/application/forms/AddNodeForm.php @@ -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); diff --git a/application/forms/EditNodeForm.php b/application/forms/EditNodeForm.php index 360fed9..87b804a 100644 --- a/application/forms/EditNodeForm.php +++ b/application/forms/EditNodeForm.php @@ -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 diff --git a/library/Businessprocess/MonitoringRestrictions.php b/library/Businessprocess/MonitoringRestrictions.php new file mode 100644 index 0000000..4dd4caa --- /dev/null +++ b/library/Businessprocess/MonitoringRestrictions.php @@ -0,0 +1,65 @@ +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)_' + )), + $e + ); + } + } + + return $restriction; + } +}