From 49562e75d7ed96574385a180caee282710950044 Mon Sep 17 00:00:00 2001
From: Johannes Meyer <johannes.meyer@netways.de>
Date: Wed, 3 Sep 2014 15:08:37 +0200
Subject: [PATCH] Adjust ..\Monitoring\Form\Config\SecurityForm to use
 handleRequest()

refs #5525
---
 .../controllers/ConfigController.php          | 25 ++-----
 .../forms/Config/SecurityConfigForm.php       | 68 +++++++++++++++++++
 .../application/forms/Config/SecurityForm.php | 42 ------------
 3 files changed, 73 insertions(+), 62 deletions(-)
 create mode 100644 modules/monitoring/application/forms/Config/SecurityConfigForm.php
 delete mode 100644 modules/monitoring/application/forms/Config/SecurityForm.php

diff --git a/modules/monitoring/application/controllers/ConfigController.php b/modules/monitoring/application/controllers/ConfigController.php
index ce22d73df..a1bed1a4e 100644
--- a/modules/monitoring/application/controllers/ConfigController.php
+++ b/modules/monitoring/application/controllers/ConfigController.php
@@ -8,7 +8,7 @@ use Icinga\Web\Notification;
 use Icinga\Form\ConfirmRemovalForm;
 use Icinga\Module\Monitoring\Form\Config\BackendForm;
 use Icinga\Module\Monitoring\Form\Config\InstanceForm;
-use Icinga\Module\Monitoring\Form\Config\SecurityForm;
+use Icinga\Module\Monitoring\Form\Config\SecurityConfigForm;
 use Icinga\Exception\NotReadableError;
 
 /**
@@ -254,26 +254,11 @@ class Monitoring_ConfigController extends ModuleActionController
      */
     public function securityAction()
     {
-        $this->view->tabs = $this->Module()->getConfigTabs()->activate('security');
-
-        $form = new SecurityForm();
-        $request = $this->getRequest();
-        $config = $this->Config()->toArray();
-        if ($request->isPost()) {
-            if ($form->isValid($request->getPost())) {
-                $config['security'] = $form->getValues();
-                if ($this->writeConfiguration(new Zend_Config($config))) {
-                    Notification::success('Configuration modified successfully');
-                    $this->redirectNow('monitoring/config/security');
-                } else {
-                    $this->render('show-configuration');
-                    return;
-                }
-            }
-        } elseif (isset($config['security'])) {
-            $form->populate($config['security']);
-        }
+        $form = new SecurityConfigForm();
+        $form->setConfig($this->Config());
+        $form->handleRequest();
 
         $this->view->form = $form;
+        $this->view->tabs = $this->Module()->getConfigTabs()->activate('security');
     }
 }
diff --git a/modules/monitoring/application/forms/Config/SecurityConfigForm.php b/modules/monitoring/application/forms/Config/SecurityConfigForm.php
new file mode 100644
index 000000000..33a03328e
--- /dev/null
+++ b/modules/monitoring/application/forms/Config/SecurityConfigForm.php
@@ -0,0 +1,68 @@
+<?php
+// {{{ICINGA_LICENSE_HEADER}}}
+// {{{ICINGA_LICENSE_HEADER}}}
+
+namespace Icinga\Module\Monitoring\Form\Config;
+
+use Icinga\Web\Request;
+use Icinga\Web\Notification;
+use Icinga\Form\ConfigForm;
+
+/**
+ * Form for modifying security relevant settings
+ */
+class SecurityConfigForm extends ConfigForm
+{
+    /**
+     * Initialize this form
+     */
+    public function init()
+    {
+        $this->setName('form_config_monitoring_security');
+        $this->setSubmitLabel(t('Save Changes'));
+    }
+
+    /**
+     * @see Form::onSuccess()
+     */
+    public function onSuccess(Request $request)
+    {
+        $this->config->security = $this->getValues();
+
+        if ($this->save()) {
+            Notification::success(t('New security configuration has successfully been stored'));
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * @see Form::onRequest()
+     */
+    public function onRequest(Request $request)
+    {
+        if (isset($this->config->security)) {
+            $this->populate($this->config->security->toArray());
+        }
+    }
+
+    /**
+     * @see Form::createElements()
+     */
+    public function createElements(array $formData)
+    {
+        $this->addElement(
+            'text',
+            'protected_customvars',
+            array(
+                'required'      => true,
+                'label'         => 'Protected Custom Variables',
+                'description'   => 'Comma separated case insensitive list of protected custom variables.'
+                    . ' Use * as a placeholder for zero or more wildcard characters.'
+                    . ' Existance of those custom variables will be shown, but their values will be masked.'
+            )
+        );
+
+        return $this;
+    }
+}
diff --git a/modules/monitoring/application/forms/Config/SecurityForm.php b/modules/monitoring/application/forms/Config/SecurityForm.php
deleted file mode 100644
index 48368d0f1..000000000
--- a/modules/monitoring/application/forms/Config/SecurityForm.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-// {{{ICINGA_LICENSE_HEADER}}}
-// {{{ICINGA_LICENSE_HEADER}}}
-
-namespace Icinga\Module\Monitoring\Form\Config;
-
-use Icinga\Web\Form;
-
-/**
- * Form for modifying security relevant settings
- */
-class SecurityForm extends Form
-{
-    /**
-     * Initialize this form
-     */
-    public function init()
-    {
-        $this->setName('form_config_monitoring_security');
-        $this->setSubmitLabel(t('Save Changes'));
-    }
-
-    /**
-     * @see Form::createElements()
-     */
-    public function createElements(array $formData)
-    {
-        return array(
-            $this->createElement(
-                'text',
-                'protected_customvars',
-                array(
-                    'label'     =>  'Protected Custom Variables',
-                    'required'  =>  true,
-                    'helptext'  =>  'Comma separated case insensitive list of protected custom variables.'
-                        . ' Use * as a placeholder for zero or more wildcard characters.'
-                        . ' Existance of those custom variables will be shown, but their values will be masked.'
-                )
-            )
-        );
-    }
-}