summaryrefslogtreecommitdiffstats
path: root/modules/setup/application/forms/UserGroupBackendPage.php
blob: 751270fd96d91ef59e37e703e767938332df6c8b (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
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Forms;

use Icinga\Application\Config;
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\ResourceFactory;
use Icinga\Forms\Config\UserGroup\LdapUserGroupBackendForm;
use Icinga\Web\Form;

/**
 * Wizard page to define user group backend specific details
 */
class UserGroupBackendPage extends Form
{
    /**
     * The resource configuration to use
     *
     * @var array
     */
    protected $resourceConfig;

    /**
     * The user backend configuration to use
     *
     * @var array
     */
    protected $backendConfig;

    /**
     * Initialize this page
     */
    public function init()
    {
        $this->setName('setup_usergroup_backend');
        $this->setTitle($this->translate('User Group Backend', 'setup.page.title'));
        $this->addDescription($this->translate(
            'To allow Icinga Web 2 to associate users and groups, you\'ll need to provide some further information'
            . ' about the LDAP Connection that is already going to be used to locate account details.'
        ));
    }

    /**
     * Set the resource configuration to use
     *
     * @param   array   $config
     *
     * @return  $this
     */
    public function setResourceConfig(array $config)
    {
        $this->resourceConfig = $config;
        return $this;
    }

    /**
     * Set the user backend configuration to use
     *
     * @param   array   $config
     *
     * @return  $this
     */
    public function setBackendConfig(array $config)
    {
        $this->backendConfig = $config;
        return $this;
    }

    /**
     * Return the resource configuration as Config object
     *
     * @return  Config
     */
    protected function createResourceConfiguration()
    {
        $config = new Config();
        $config->setSection($this->resourceConfig['name'], $this->resourceConfig);
        return $config;
    }

    /**
     * Return the user backend configuration as Config object
     *
     * @return  Config
     */
    protected function createBackendConfiguration()
    {
        $config = new Config();
        $backendConfig = $this->backendConfig;
        $backendConfig['resource'] = $this->resourceConfig['name'];
        $config->setSection($this->backendConfig['name'], $backendConfig);
        return $config;
    }

    /**
     * Create and add elements to this form
     *
     * @param   array   $formData
     */
    public function createElements(array $formData)
    {
        // LdapUserGroupBackendForm requires these factories to provide valid configurations
        ResourceFactory::setConfig($this->createResourceConfiguration());
        UserBackend::setConfig($this->createBackendConfiguration());

        $backendForm = new LdapUserGroupBackendForm();
        $formData['type'] = 'ldap';
        $backendForm->create($formData);
        $backendForm->getElement('name')->setValue('icingaweb2');
        $this->addSubForm($backendForm, 'backend_form');

        $backendForm->addElement(
            'hidden',
            'resource',
            array(
                'required'      => true,
                'value'         => $this->resourceConfig['name'],
                'decorators'    => array('ViewHelper')
            )
        );
        $backendForm->addElement(
            'hidden',
            'user_backend',
            array(
                'required'      => true,
                'value'         => $this->backendConfig['name'],
                'decorators'    => array('ViewHelper')
            )
        );
    }

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