From a0ffdb0f7cec4e31211c0f84e2c1cf42f92d2098 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Thu, 5 Oct 2017 14:53:14 +0200
Subject: [PATCH 1/5] Populate LdapBackendForm with suggestions only on the
 first access

refs #2990
---
 modules/setup/library/Setup/WebWizard.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/setup/library/Setup/WebWizard.php b/modules/setup/library/Setup/WebWizard.php
index 01eb62aaa..d8b0edcd5 100644
--- a/modules/setup/library/Setup/WebWizard.php
+++ b/modules/setup/library/Setup/WebWizard.php
@@ -138,7 +138,7 @@ class WebWizard extends Wizard implements SetupWizard
             } elseif ($authData['type'] === 'ldap') {
                 $page->setResourceConfig($this->getPageData('setup_ldap_resource'));
 
-                if (! $this->hasPageData('setup_authentication_backend')) {
+                if (! $this->hasPageData('setup_authentication_backend') && ! $request->isPost()) {
                     $suggestions = $this->getPageData('setup_ldap_discovery');
                     if (isset($suggestions['backend'])) {
                         $page->populate($suggestions['backend']);

From de53925c7c48736d630acb560c9187a74e1b7a55 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Fri, 6 Oct 2017 11:12:11 +0200
Subject: [PATCH 2/5] Provide suggestions for AuthBackendPage as element
 defaults

refs #2990
---
 .../application/forms/AuthBackendPage.php     | 38 +++++++++++++++++++
 modules/setup/library/Setup/WebWizard.php     | 10 ++---
 2 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/modules/setup/application/forms/AuthBackendPage.php b/modules/setup/application/forms/AuthBackendPage.php
index d73a82dcd..2ce1f5015 100644
--- a/modules/setup/application/forms/AuthBackendPage.php
+++ b/modules/setup/application/forms/AuthBackendPage.php
@@ -23,6 +23,13 @@ class AuthBackendPage extends Form
      */
     protected $config;
 
+    /**
+     * Default values for the subform's elements suggested by a previous step
+     *
+     * @var string[]
+     */
+    protected $suggestions = array();
+
     /**
      * Initialize this page
      */
@@ -112,6 +119,13 @@ class AuthBackendPage extends Form
             ));
         }
 
+        foreach ($this->suggestions as $key => $suggestion) {
+            $element = $backendForm->getElement($key);
+            if ($element !== null) {
+                $element->setValue($suggestion);
+            }
+        }
+
         $backendForm->getElement('name')->setValue('icingaweb2');
         $this->addSubForm($backendForm, 'backend_form');
     }
@@ -231,4 +245,28 @@ class AuthBackendPage extends Form
             )
         );
     }
+
+    /**
+     * Get default values for the subform's elements suggested by a previous step
+     *
+     * @return string[]
+     */
+    public function getSuggestions()
+    {
+        return $this->suggestions;
+    }
+
+    /**
+     * Set default values for the subform's elements suggested by a previous step
+     *
+     * @param string[] $suggestions
+     *
+     * @return $this
+     */
+    public function setSuggestions(array $suggestions)
+    {
+        $this->suggestions = $suggestions;
+
+        return $this;
+    }
 }
diff --git a/modules/setup/library/Setup/WebWizard.php b/modules/setup/library/Setup/WebWizard.php
index d8b0edcd5..f8f10ef2f 100644
--- a/modules/setup/library/Setup/WebWizard.php
+++ b/modules/setup/library/Setup/WebWizard.php
@@ -132,17 +132,17 @@ class WebWizard extends Wizard implements SetupWizard
         if ($page->getName() === 'setup_requirements') {
             $page->setWizard($this);
         } elseif ($page->getName() === 'setup_authentication_backend') {
+            /** @var AuthBackendPage $page */
+
             $authData = $this->getPageData('setup_authentication_type');
             if ($authData['type'] === 'db') {
                 $page->setResourceConfig($this->getPageData('setup_auth_db_resource'));
             } elseif ($authData['type'] === 'ldap') {
                 $page->setResourceConfig($this->getPageData('setup_ldap_resource'));
 
-                if (! $this->hasPageData('setup_authentication_backend') && ! $request->isPost()) {
-                    $suggestions = $this->getPageData('setup_ldap_discovery');
-                    if (isset($suggestions['backend'])) {
-                        $page->populate($suggestions['backend']);
-                    }
+                $suggestions = $this->getPageData('setup_ldap_discovery');
+                if (isset($suggestions['backend'])) {
+                    $page->setSuggestions($suggestions['backend']);
                 }
 
                 if ($this->getDirection() === static::FORWARD) {

From 4603f0657d5af593dc5f078423d7bc1d6e868b70 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Fri, 6 Oct 2017 12:04:30 +0200
Subject: [PATCH 3/5] Respect suggestions only for the LDAP backend type

refs #2990
---
 .../setup/application/forms/AuthBackendPage.php    | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/modules/setup/application/forms/AuthBackendPage.php b/modules/setup/application/forms/AuthBackendPage.php
index 2ce1f5015..e0d2d813b 100644
--- a/modules/setup/application/forms/AuthBackendPage.php
+++ b/modules/setup/application/forms/AuthBackendPage.php
@@ -110,6 +110,13 @@ class AuthBackendPage extends Form
                     'value'             => $type
                 )
             );
+
+            foreach ($this->suggestions as $key => $suggestion) {
+                $element = $backendForm->getElement($key);
+                if ($element !== null) {
+                    $element->setValue($suggestion);
+                }
+            }
         } else { // $this->config['type'] === 'external'
             $backendForm = new ExternalBackendForm();
             $backendForm->create($formData);
@@ -119,13 +126,6 @@ class AuthBackendPage extends Form
             ));
         }
 
-        foreach ($this->suggestions as $key => $suggestion) {
-            $element = $backendForm->getElement($key);
-            if ($element !== null) {
-                $element->setValue($suggestion);
-            }
-        }
-
         $backendForm->getElement('name')->setValue('icingaweb2');
         $this->addSubForm($backendForm, 'backend_form');
     }

From b7b7796e3498bdbc8cc0fc66b09181f32323a7e9 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Fri, 6 Oct 2017 13:26:21 +0200
Subject: [PATCH 4/5] AuthBackendPage: set the subform's defaults before
 creating it

refs #2990
---
 .../Config/UserBackend/LdapBackendForm.php    | 63 ++++++++++++++++---
 .../application/forms/AuthBackendPage.php     |  8 +--
 2 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/application/forms/Config/UserBackend/LdapBackendForm.php b/application/forms/Config/UserBackend/LdapBackendForm.php
index 89505da41..ab508c12c 100644
--- a/application/forms/Config/UserBackend/LdapBackendForm.php
+++ b/application/forms/Config/UserBackend/LdapBackendForm.php
@@ -22,6 +22,13 @@ class LdapBackendForm extends Form
      */
     protected $resources;
 
+    /**
+     * Default values for the form elements
+     *
+     * @var string[]
+     */
+    protected $suggestions = array();
+
     /**
      * Initialize this form
      */
@@ -60,7 +67,8 @@ class LdapBackendForm extends Form
                 'label'         => $this->translate('Backend Name'),
                 'description'   => $this->translate(
                     'The name of this authentication provider that is used to differentiate it from others.'
-                )
+                ),
+                'value'         => $this->getSuggestion('name')
             )
         );
         $this->addElement(
@@ -74,7 +82,8 @@ class LdapBackendForm extends Form
                 ),
                 'multiOptions'  => !empty($this->resources)
                     ? array_combine($this->resources, $this->resources)
-                    : array()
+                    : array(),
+                'value'         => $this->getSuggestion('resource')
             )
         );
 
@@ -141,7 +150,7 @@ class LdapBackendForm extends Form
                 'disabled'          => $isAd ?: null,
                 'label'             => $this->translate('LDAP User Object Class'),
                 'description'       => $this->translate('The object class used for storing users on the LDAP server.'),
-                'value'             => $userClass
+                'value'             => $this->getSuggestion('user_class', $userClass)
             )
         );
         $this->addElement(
@@ -150,7 +159,7 @@ class LdapBackendForm extends Form
             array(
                 'preserveDefault'   => true,
                 'allowEmpty'        => true,
-                'value'             => $filter,
+                'value'             => $this->getSuggestion('filter', $filter),
                 'label'             => $this->translate('LDAP Filter'),
                 'description'       => $this->translate(
                     'An additional filter to use when looking up users using the specified connection. '
@@ -193,7 +202,7 @@ class LdapBackendForm extends Form
                 'description'       => $this->translate(
                     'The attribute name used for storing the user name on the LDAP server.'
                 ),
-                'value'             => $userNameAttribute
+                'value'             => $this->getSuggestion('user_name_attribute', $userNameAttribute)
             )
         );
         $this->addElement(
@@ -201,7 +210,7 @@ class LdapBackendForm extends Form
             'backend',
             array(
                 'disabled'  => true,
-                'value'     => $isAd ? 'msldap' : 'ldap'
+                'value'     => $this->getSuggestion('backend', $isAd ? 'msldap' : 'ldap')
             )
         );
         $this->addElement(
@@ -215,7 +224,7 @@ class LdapBackendForm extends Form
                     'The path where users can be found on the LDAP server. Leave ' .
                     'empty to select all users available using the specified connection.'
                 ),
-                'value'             => $baseDn
+                'value'             => $this->getSuggestion('base_dn', $baseDn)
             )
         );
 
@@ -233,7 +242,8 @@ class LdapBackendForm extends Form
                     . ' If your LDAP backend holds usernames with a domain part or if it is not necessary in your setup'
                     . ' to authenticate users based on their domains, leave this field empty.'
                 ),
-                'preserveDefault' => true
+                'preserveDefault' => true,
+                'value'         => $this->getSuggestion('domain')
             )
         );
 
@@ -318,4 +328,41 @@ class LdapBackendForm extends Form
             }
         }
     }
+
+    /**
+     * Get the default values for the form elements
+     *
+     * @return string[]
+     */
+    public function getSuggestions()
+    {
+        return $this->suggestions;
+    }
+
+    /**
+     * Get the default value for the given form element or the given default
+     *
+     * @param   string  $element
+     * @param   string  $default
+     *
+     * @return  string
+     */
+    public function getSuggestion($element, $default = null)
+    {
+        return isset($this->suggestions[$element]) ? $this->suggestions[$element] : $default;
+    }
+
+    /**
+     * Set the default values for the form elements
+     *
+     * @param string[] $suggestions
+     *
+     * @return $this
+     */
+    public function setSuggestions(array $suggestions)
+    {
+        $this->suggestions = $suggestions;
+
+        return $this;
+    }
 }
diff --git a/modules/setup/application/forms/AuthBackendPage.php b/modules/setup/application/forms/AuthBackendPage.php
index e0d2d813b..91fdc0183 100644
--- a/modules/setup/application/forms/AuthBackendPage.php
+++ b/modules/setup/application/forms/AuthBackendPage.php
@@ -85,6 +85,7 @@ class AuthBackendPage extends Form
             }
 
             $backendForm = new LdapBackendForm();
+            $backendForm->setSuggestions($this->suggestions);
             $backendForm->setResources(array($this->config['name']));
             $backendForm->create($formData);
             $backendForm->getElement('resource')->setIgnore(true);
@@ -110,13 +111,6 @@ class AuthBackendPage extends Form
                     'value'             => $type
                 )
             );
-
-            foreach ($this->suggestions as $key => $suggestion) {
-                $element = $backendForm->getElement($key);
-                if ($element !== null) {
-                    $element->setValue($suggestion);
-                }
-            }
         } else { // $this->config['type'] === 'external'
             $backendForm = new ExternalBackendForm();
             $backendForm->create($formData);

From 6ac0f7fe467f6c087762bde7192b65cbc0b8ba63 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Fri, 6 Oct 2017 13:29:55 +0200
Subject: [PATCH 5/5] LdapBackendForm: fill in the base DN if discovered one

refs #2990
---
 application/forms/Config/UserBackend/LdapBackendForm.php | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/application/forms/Config/UserBackend/LdapBackendForm.php b/application/forms/Config/UserBackend/LdapBackendForm.php
index ab508c12c..7e6f12a89 100644
--- a/application/forms/Config/UserBackend/LdapBackendForm.php
+++ b/application/forms/Config/UserBackend/LdapBackendForm.php
@@ -87,7 +87,6 @@ class LdapBackendForm extends Form
             )
         );
 
-        $baseDn = null;
         $hasAdOid = false;
         if (! $isAd && !empty($this->resources)) {
             $this->addElement(
@@ -224,7 +223,7 @@ class LdapBackendForm extends Form
                     'The path where users can be found on the LDAP server. Leave ' .
                     'empty to select all users available using the specified connection.'
                 ),
-                'value'             => $this->getSuggestion('base_dn', $baseDn)
+                'value'             => isset($baseDn) ? $baseDn : $this->getSuggestion('base_dn')
             )
         );