summaryrefslogtreecommitdiffstats
path: root/modules/setup/library/Setup/Steps/AuthenticationStep.php
blob: 3c6c64a16468726a79aea3eed3b0e5afb23810de (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Steps;

use Exception;
use Icinga\Application\Config;
use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\IcingaException;
use Icinga\Authentication\User\DbUserBackend;
use Icinga\Module\Setup\Step;

class AuthenticationStep extends Step
{
    protected $data;

    protected $dbError;

    protected $authIniError;

    protected $permIniError;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function apply()
    {
        $success = $this->createAuthenticationIni();
        if (isset($this->data['adminAccountData']['resourceConfig'])) {
            $success &= $this->createAccount();
        }

        $success &= $this->createRolesIni();
        return $success;
    }

    protected function createAuthenticationIni()
    {
        $config = array();
        $backendConfig = $this->data['backendConfig'];
        $backendName = $backendConfig['name'];
        unset($backendConfig['name']);
        $config[$backendName] = $backendConfig;
        if (isset($this->data['resourceName'])) {
            $config[$backendName]['resource'] = $this->data['resourceName'];
        }

        try {
            Config::fromArray($config)
                ->setConfigFile(Config::resolvePath('authentication.ini'))
                ->saveIni();
        } catch (Exception $e) {
            $this->authIniError = $e;
            return false;
        }

        $this->authIniError = false;
        return true;
    }

    protected function createRolesIni()
    {
        if (isset($this->data['adminAccountData']['username'])) {
            $config = array(
                'users'         => $this->data['adminAccountData']['username'],
                'permissions'   => '*'
            );

            if ($this->data['backendConfig']['backend'] === 'db') {
                $config['groups'] = mt('setup', 'Administrators', 'setup.role.name');
            }
        } else { // isset($this->data['adminAccountData']['groupname'])
            $config = array(
                'groups'        => $this->data['adminAccountData']['groupname'],
                'permissions'   => '*'
            );
        }

        try {
            Config::fromArray(array(mt('setup', 'Administrators', 'setup.role.name') => $config))
                ->setConfigFile(Config::resolvePath('roles.ini'))
                ->saveIni();
        } catch (Exception $e) {
            $this->permIniError = $e;
            return false;
        }

        $this->permIniError = false;
        return true;
    }

    protected function createAccount()
    {
        try {
            $backend = new DbUserBackend(
                ResourceFactory::createResource(new ConfigObject($this->data['adminAccountData']['resourceConfig']))
            );

            if ($backend->select()->where('user_name', $this->data['adminAccountData']['username'])->count() === 0) {
                $backend->insert('user', array(
                    'user_name' => $this->data['adminAccountData']['username'],
                    'password'  => $this->data['adminAccountData']['password'],
                    'is_active' => true
                ));
                $this->dbError = false;
            }
        } catch (Exception $e) {
            $this->dbError = $e;
            return false;
        }

        return true;
    }

    public function getSummary()
    {
        $pageTitle = '<h2>' . mt('setup', 'Authentication', 'setup.page.title') . '</h2>';
        $backendTitle = '<h3>' . mt('setup', 'Authentication Backend', 'setup.page.title') . '</h3>';
        $adminTitle = '<h3>' . mt('setup', 'Administration', 'setup.page.title') . '</h3>';

        $authType = $this->data['backendConfig']['backend'];
        $backendDesc = '<p>' . sprintf(
            mt('setup', 'Users will authenticate using %s.', 'setup.summary.auth'),
            $authType === 'db' ? mt('setup', 'a database', 'setup.summary.auth.type') : (
                $authType === 'ldap' || $authType === 'msldap' ? 'LDAP' : (
                    mt('setup', 'webserver authentication', 'setup.summary.auth.type')
                )
            )
        ) . '</p>';

        $backendHtml = ''
            . '<table>'
            . '<tbody>'
            . '<tr>'
            . '<td><strong>' . t('Backend Name') . '</strong></td>'
            . '<td>' . $this->data['backendConfig']['name'] . '</td>'
            . '</tr>'
            . ($authType === 'ldap' || $authType === 'msldap' ? (
                '<tr>'
                . '<td><strong>' . mt('setup', 'User Object Class') . '</strong></td>'
                . '<td>' . ($authType === 'msldap' ? 'user' : $this->data['backendConfig']['user_class']) . '</td>'
                . '</tr>'
                . '<tr>'
                . '<td><strong>' . mt('setup', 'Custom Filter') . '</strong></td>'
                . '<td>' . (trim($this->data['backendConfig']['filter']) ?: t('None', 'auth.ldap.filter')) . '</td>'
                . '</tr>'
                . '<tr>'
                . '<td><strong>' . mt('setup', 'User Name Attribute') . '</strong></td>'
                . '<td>' . ($authType === 'msldap'
                    ? 'sAMAccountName'
                    : $this->data['backendConfig']['user_name_attribute']) . '</td>'
                . '</tr>'
            ) : ($authType === 'external' ? (
                '<tr>'
                . '<td><strong>' . t('Filter Pattern') . '</strong></td>'
                . '<td>' . $this->data['backendConfig']['strip_username_regexp'] . '</td>'
                . '</tr>'
            ) : ''))
            . '</tbody>'
            . '</table>';

        if (isset($this->data['adminAccountData']['username'])) {
            $adminHtml = '<p>' . (isset($this->data['adminAccountData']['resourceConfig']) ? sprintf(
                mt('setup', 'Administrative rights will initially be granted to a new account called "%s".'),
                $this->data['adminAccountData']['username']
            ) : sprintf(
                mt('setup', 'Administrative rights will initially be granted to an existing account called "%s".'),
                $this->data['adminAccountData']['username']
            )) . '</p>';
        } else { // isset($this->data['adminAccountData']['groupname'])
            $adminHtml = '<p>' . sprintf(
                mt('setup', 'Administrative rights will initially be granted to members of the user group "%s".'),
                $this->data['adminAccountData']['groupname']
            ) . '</p>';
        }

        return $pageTitle . '<div class="topic">' . $backendDesc . $backendTitle . $backendHtml . '</div>'
            . '<div class="topic">' . $adminTitle . $adminHtml . '</div>';
    }

    public function getReport()
    {
        $report = array();

        if ($this->authIniError === false) {
            $report[] = sprintf(
                mt('setup', 'Authentication configuration has been successfully written to: %s'),
                Config::resolvePath('authentication.ini')
            );
        } elseif ($this->authIniError !== null) {
            $report[] = sprintf(
                mt('setup', 'Authentication configuration could not be written to: %s. An error occured:'),
                Config::resolvePath('authentication.ini')
            );
            $report[] = sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->authIniError));
        }

        if ($this->dbError === false) {
            $report[] = sprintf(
                mt('setup', 'Account "%s" has been successfully created.'),
                $this->data['adminAccountData']['username']
            );
        } elseif ($this->dbError !== null) {
            $report[] = sprintf(
                mt('setup', 'Unable to create account "%s". An error occured:'),
                $this->data['adminAccountData']['username']
            );
            $report[] = sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->dbError));
        }

        if ($this->permIniError === false) {
            $report[] = isset($this->data['adminAccountData']['username']) ? sprintf(
                mt('setup', 'Account "%s" has been successfully defined as initial administrator.'),
                $this->data['adminAccountData']['username']
            ) : sprintf(
                mt('setup', 'The members of the user group "%s" were successfully defined as initial administrators.'),
                $this->data['adminAccountData']['groupname']
            );
        } elseif ($this->permIniError !== null) {
            $report[] = isset($this->data['adminAccountData']['username']) ? sprintf(
                mt('setup', 'Unable to define account "%s" as initial administrator. An error occured:'),
                $this->data['adminAccountData']['username']
            ) : sprintf(
                mt(
                    'setup',
                    'Unable to define the members of the user group "%s" as initial administrators. An error occured:'
                ),
                $this->data['adminAccountData']['groupname']
            );
            $report[] = sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->permIniError));
        }

        return $report;
    }
}