From cc9f5037f4e736addc238b575acdc9ea17ca049d Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 9 Jun 2016 23:49:02 +0200 Subject: [PATCH] IcingaNotificationForm: enforce & render apply to fixes #11895 --- application/forms/IcingaNotificationForm.php | 18 +++++++++ .../Director/Objects/IcingaNotification.php | 37 +++++++++++++++++++ .../Director/Web/Form/DirectorObjectForm.php | 1 + 3 files changed, 56 insertions(+) diff --git a/application/forms/IcingaNotificationForm.php b/application/forms/IcingaNotificationForm.php index 64cb29be..8a83ffac 100644 --- a/application/forms/IcingaNotificationForm.php +++ b/application/forms/IcingaNotificationForm.php @@ -41,6 +41,24 @@ class IcingaNotificationForm extends DirectorObjectForm return $this; } + $this->addElement( + 'select', + 'apply_to', + array( + 'label' => $this->translate('Apply to'), + 'description' => $this->translate( + 'Whether this notification should affect hosts or services' + ), + 'required' => true, + 'multiOptions' => $this->optionalEnum( + array( + 'host' => $this->translate('Hosts'), + 'service' => $this->translate('Services'), + ) + ) + ) + ); + $sub = new AssignListSubForm(); $sub->setObject($this->getObject()); $sub->setup(); diff --git a/library/Director/Objects/IcingaNotification.php b/library/Director/Objects/IcingaNotification.php index 1f00498c..8efb6f11 100644 --- a/library/Director/Objects/IcingaNotification.php +++ b/library/Director/Objects/IcingaNotification.php @@ -111,6 +111,43 @@ class IcingaNotification extends IcingaObject ); } + /** + * Do not render internal property apply_to + * + * Avoid complaints for method names with underscore: + * @codingStandardsIgnoreStart + * + * @return string + */ + public function renderApply_to() + { + // @codingStandardsIgnoreEnd + return ''; + } + + protected function renderObjectHeader() + { + if ($this->isApplyRule()) { + if (($to = $this->get('apply_to')) === null) { + throw new ConfigurationError( + 'Applied notification "%s" has no valid object type', + $this->getObjectName() + ); + } + + return sprintf( + "%s %s to %s %s {\n", + $this->getObjectTypeName(), + $this->getType(), + ucfirst($to), + c::renderString($this->getObjectName()) + ); + + } else { + return parent::renderObjectHeader(); + } + } + protected function setKey($key) { if (is_int($key)) { diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index 09dfcbd4..bf9df167 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -493,6 +493,7 @@ abstract class DirectorObjectForm extends QuickForm 'groups', 'users', 'user_groups', + 'apply_to', 'command_id', // Notification 'notification_interval', 'period_id',