summaryrefslogtreecommitdiffstats
path: root/modules/setup/application/forms/AuthBackendPage.php
blob: 88c77e679d39de71757f839eda50009a415d8b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Forms;

use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use Icinga\Forms\Config\UserBackendConfigForm;
use Icinga\Forms\Config\UserBackend\DbBackendForm;
use Icinga\Forms\Config\UserBackend\LdapBackendForm;
use Icinga\Forms\Config\UserBackend\ExternalBackendForm;
use Icinga\Web\Form;

/**
 * Wizard page to define authentication backend specific details
 */
class AuthBackendPage extends Form
{
    /**
     * The resource configuration to use
     *
     * @var array
     */
    protected $config;

    /**
     * Default values for the subform's elements suggested by a previous step
     *
     * @var string[]
     */
    protected $suggestions = array();

    /**
     * Initialize this page
     */
    public function init()
    {
        $this->setName('setup_authentication_backend');
        $this->setTitle($this->translate('Authentication Backend', 'setup.page.title'));
        $this->setValidatePartial(true);
    }

    /**
     * Set the resource configuration to use
     *
     * @param   array   $config
     *
     * @return  $this
     */
    public function setResourceConfig(array $config)
    {
        $resourceConfig = new Config();
        $resourceConfig->setSection($config['name'], $config);
        ResourceFactory::setConfig($resourceConfig);

        $this->config = $config;
        return $this;
    }

    /**
     * Create and add elements to this form
     *
     * @param   array   $formData
     */
    public function createElements(array $formData)
    {
        if (isset($formData['skip_validation']) && $formData['skip_validation']) {
            $this->addSkipValidationCheckbox();
        }

        $backendForm = null;
        if (! isset($this->config) || $this->config['type'] === 'external') {
            $backendForm = new ExternalBackendForm();
            $backendForm->create($formData);
            $this->addDescription($this->translate(
                'You\'ve chosen to authenticate using a web server\'s mechanism so it may be necessary'
                . ' to adjust usernames before any permissions, restrictions, etc. are being applied.'
            ));
        } elseif ($this->config['type'] === 'db') {
            $this->setRequiredCue(null);
            $backendForm = new DbBackendForm();
            $backendForm->setRequiredCue(null);
            $backendForm->create($formData)->removeElement('resource');
            $this->addDescription($this->translate(
                'As you\'ve chosen to use a database for authentication all you need '
                . 'to do now is defining a name for your first authentication backend.'
            ));
        } elseif ($this->config['type'] === 'ldap') {
            $type = null;
            if (! isset($formData['type'])) {
                if (isset($formData['backend'])) {
                    $formData['type'] = $type = $formData['backend'];
                } elseif (isset($this->suggestions['backend'])) {
                    $formData['type'] = $type = $this->suggestions['backend'];
                }
            }

            $backendForm = new LdapBackendForm();
            $backendForm->setSuggestions($this->suggestions);
            $backendForm->setResources(array($this->config['name']));
            $backendForm->create($formData);
            $backendForm->getElement('resource')->setIgnore(true);
            $this->addDescription($this->translate(
                'Before you are able to authenticate using the LDAP connection defined earlier you need to'
                . ' provide some more information so that Icinga Web 2 is able to locate account details.'
            ));
            $this->addElement(
                'select',
                'type',
                array(
                    'ignore'            => true,
                    'required'          => true,
                    'autosubmit'        => true,
                    'label'             => $this->translate('Backend Type'),
                    'description'       => $this->translate(
                        'The type of the resource being used for this authenticaton provider'
                    ),
                    'multiOptions'      => array(
                        'ldap'      => 'LDAP',
                        'msldap'    => 'ActiveDirectory'
                    ),
                    'value'             => $type
                )
            );
        }

        $backendForm->getElement('name')->setValue('icingaweb2');
        $this->addSubForm($backendForm, 'backend_form');
    }

    /**
     * Retrieve all form element values
     *
     * @param   bool    $suppressArrayNotation  Ignored
     *
     * @return  array
     */
    public function getValues($suppressArrayNotation = false)
    {
        $values = parent::getValues();
        $values = array_merge($values, $values['backend_form']);
        unset($values['backend_form']);
        return $values;
    }

    /**
     * Validate the given form data and check whether it's possible to authenticate using the configured backend
     *
     * @param   array   $data   The data to validate
     *
     * @return  bool
     */
    public function isValid($data)
    {
        if (! parent::isValid($data)) {
            return false;
        }

        if (isset($this->config)) {
            if ($this->config['type'] === 'ldap' && (
                ! isset($data['skip_validation']) || $data['skip_validation'] == 0)
            ) {
                $self = clone $this;
                $self->getSubForm('backend_form')->getElement('resource')->setIgnore(false);
                $inspection = UserBackendConfigForm::inspectUserBackend($self);
                if ($inspection && $inspection->hasError()) {
                    $this->error($inspection->getError());
                    $this->addSkipValidationCheckbox();
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Run the configured backend's inspection checks and show the result, if necessary
     *
     * This will only run any validation if the user pushed the 'backend_validation' button.
     *
     * @param   array   $formData
     *
     * @return  bool
     */
    public function isValidPartial(array $formData)
    {
        if (isset($formData['backend_validation']) && parent::isValid($formData)) {
            $self = clone $this;
            if (($resourceElement = $self->getSubForm('backend_form')->getElement('resource')) !== null) {
                $resourceElement->setIgnore(false);
            }

            $inspection = UserBackendConfigForm::inspectUserBackend($self);
            if ($inspection !== null) {
                $join = function ($e) use (&$join) {
                    return is_string($e) ? $e : join("\n", array_map($join, $e));
                };
                $this->addElement(
                    'note',
                    'inspection_output',
                    array(
                        'order'         => 0,
                        'value'         => '<strong>' . $this->translate('Validation Log') . "</strong>\n\n"
                            . join("\n", array_map($join, $inspection->toArray())),
                        'decorators'    => array(
                            'ViewHelper',
                            array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')),
                        )
                    )
                );

                if ($inspection->hasError()) {
                    $this->warning(sprintf(
                        $this->translate('Failed to successfully validate the configuration: %s'),
                        $inspection->getError()
                    ));
                    return false;
                }
            }

            $this->info($this->translate('The configuration has been successfully validated.'));
        } elseif (isset($formData['discovery_btn']) || isset($formData['btn_discover_domain'])) {
            return parent::isValidPartial($formData);
        } elseif (! isset($formData['backend_validation'])) {
            // This is usually done by isValid(Partial), but as we're not calling any of these...
            $this->populate($formData);
        }

        return true;
    }

    /**
     * Add a checkbox to this form by which the user can skip the authentication validation
     */
    protected function addSkipValidationCheckbox()
    {
        $this->addElement(
            'checkbox',
            'skip_validation',
            array(
                'order'         => 0,
                'ignore'        => true,
                'required'      => true,
                'label'         => $this->translate('Skip Validation'),
                'description'   => $this->translate('Check this to not to validate authentication using this backend')
            )
        );
    }

    /**
     * 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;
    }
}