Passed
Push — develop ( c6c0b1...90e9a5 )
by Felipe
10:30 queued 02:43
created

RolesController::doSaveAlter()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 6
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class RolesController extends BaseController
17
{
18
    public $controller_title = 'strroles';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        $this->printHeader();
26
        $this->printBody();
27
28
        switch ($this->action) {
29
            case 'create':
30
                $this->doCreate();
31
32
                break;
33
            case 'save_create':
34
                if (isset($_POST['create'])) {
35
                    $this->doSaveCreate();
36
                } else {
37
                    $this->doDefault();
38
                }
39
40
                break;
41
            case 'alter':
42
                $this->doAlter();
43
44
                break;
45
            case 'save_alter':
46
                if (isset($_POST['alter'])) {
47
                    $this->doSaveAlter();
48
                } else {
49
                    $this->doDefault();
50
                }
51
52
                break;
53
            case 'confirm_drop':
54
                $this->doDrop(true);
55
56
                break;
57
            case 'drop':
58
                if (isset($_POST['drop'])) {
59
                    $this->doDrop(false);
60
                } else {
61
                    $this->doDefault();
62
                }
63
64
                break;
65
            case 'properties':
66
                $this->doProperties();
67
68
                break;
69
            case 'confchangepassword':
70
                $this->doChangePassword(true);
71
72
                break;
73
            case 'changepassword':
74
                if (isset($_REQUEST['ok'])) {
75
                    $this->doChangePassword(false);
76
                } else {
77
                    $this->doAccount();
78
                }
79
80
                break;
81
            case 'account':
82
                $this->doAccount();
83
84
                break;
85
            default:
86
                $this->doDefault();
87
        }
88
89
        $this->printFooter();
90
    }
91
92
    /**
93
     * Show default list of roles in the database.
94
     *
95
     * @param mixed $msg
96
     */
97
    public function doDefault($msg = '')
98
    {
99
        $data = $this->misc->getDatabaseAccessor();
100
101
        $lang                = $this->lang;
102
        $renderRoleConnLimit = function ($val) use ($lang) {
103
            return '-1' == $val ? $lang['strnolimit'] : htmlspecialchars($val);
104
        };
105
106
        $renderRoleExpires = function ($val) use ($lang) {
107
            return 'infinity' == $val ? $lang['strnever'] : htmlspecialchars($val);
108
        };
109
110
        $this->printTrail('server');
111
        $this->printTabs('server', 'roles');
112
        $this->printMsg($msg);
113
114
        $roles = $data->getRoles();
115
116
        $columns = [
117
            'role'       => [
118
                'title' => $this->lang['strrole'],
119
                'field' => Decorator::field('rolname'),
120
                'url'   => \SUBFOLDER . "/redirect/role?action=properties&amp;{$this->misc->href}&amp;",
121
                'vars'  => ['rolename' => 'rolname'],
122
            ],
123
            'superuser'  => [
124
                'title' => $this->lang['strsuper'],
125
                'field' => Decorator::field('rolsuper'),
126
                'type'  => 'yesno',
127
            ],
128
            'createdb'   => [
129
                'title' => $this->lang['strcreatedb'],
130
                'field' => Decorator::field('rolcreatedb'),
131
                'type'  => 'yesno',
132
            ],
133
            'createrole' => [
134
                'title' => $this->lang['strcancreaterole'],
135
                'field' => Decorator::field('rolcreaterole'),
136
                'type'  => 'yesno',
137
            ],
138
            'inherits'   => [
139
                'title' => $this->lang['strinheritsprivs'],
140
                'field' => Decorator::field('rolinherit'),
141
                'type'  => 'yesno',
142
            ],
143
            'canloging'  => [
144
                'title' => $this->lang['strcanlogin'],
145
                'field' => Decorator::field('rolcanlogin'),
146
                'type'  => 'yesno',
147
            ],
148
            'connlimit'  => [
149
                'title'  => $this->lang['strconnlimit'],
150
                'field'  => Decorator::field('rolconnlimit'),
151
                'type'   => 'callback',
152
                'params' => ['function' => $renderRoleConnLimit],
153
            ],
154
            'expires'    => [
155
                'title'  => $this->lang['strexpires'],
156
                'field'  => Decorator::field('rolvaliduntil'),
157
                'type'   => 'callback',
158
                'params' => ['function' => $renderRoleExpires, 'null' => $this->lang['strnever']],
159
            ],
160
            'actions'    => [
161
                'title' => $this->lang['stractions'],
162
            ],
163
        ];
164
165
        $actions = [
166
            'alter' => [
167
                'content' => $this->lang['stralter'],
168
                'attr'    => [
169
                    'href' => [
170
                        'url'     => 'roles',
171
                        'urlvars' => [
172
                            'action'   => 'alter',
173
                            'rolename' => Decorator::field('rolname'),
174
                        ],
175
                    ],
176
                ],
177
            ],
178
            'drop'  => [
179
                'content' => $this->lang['strdrop'],
180
                'attr'    => [
181
                    'href' => [
182
                        'url'     => 'roles',
183
                        'urlvars' => [
184
                            'action'   => 'confirm_drop',
185
                            'rolename' => Decorator::field('rolname'),
186
                        ],
187
                    ],
188
                ],
189
            ],
190
        ];
191
192
        echo $this->printTable($roles, $columns, $actions, 'roles-roles', $this->lang['strnoroles']);
193
194
        $navlinks = [
195
            'create' => [
196
                'attr'    => [
197
                    'href' => [
198
                        'url'     => 'roles',
199
                        'urlvars' => [
200
                            'action' => 'create',
201
                            'server' => $_REQUEST['server'],
202
                        ],
203
                    ],
204
                ],
205
                'content' => $this->lang['strcreaterole'],
206
            ],
207
        ];
208
        $this->printNavLinks($navlinks, 'roles-roles', get_defined_vars());
209
    }
210
211
    /**
212
     * Displays a screen for create a new role.
213
     *
214
     * @param mixed $msg
215
     */
216
    public function doCreate($msg = '')
217
    {
218
        $data = $this->misc->getDatabaseAccessor();
219
220
        $this->coalesceArr($_POST, 'formRolename', '');
221
222
        $this->coalesceArr($_POST, 'formPassword', '');
223
224
        $this->coalesceArr($_POST, 'formConfirm', '');
225
226
        $this->coalesceArr($_POST, 'formConnLimit', '');
227
228
        $this->coalesceArr($_POST, 'formExpires', '');
229
230
        $this->coalesceArr($_POST, 'memberof', []);
231
232
        $this->coalesceArr($_POST, 'members', []);
233
234
        $this->coalesceArr($_POST, 'adminmembers', []);
235
236
        $this->printTrail('role');
237
        $this->printTitle($this->lang['strcreaterole'], 'pg.role.create');
238
        $this->printMsg($msg);
239
240
        echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n";
241
        echo "<table>\n";
242
        echo "\t<tr>\n\t\t<th class=\"data left required\" style=\"width: 130px\">{$this->lang['strname']}</th>\n";
243
        echo "\t\t<td class=\"data1\"><input size=\"15\" maxlength=\"{$data->_maxNameLen}\" name=\"formRolename\" value=\"", htmlspecialchars($_POST['formRolename']), "\" /></td>\n\t</tr>\n";
244
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strpassword']}</th>\n";
245
        echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
246
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconfirm']}</th>\n";
247
        echo "\t\t<td class=\"data1\"><input size=\"15\" type=\"password\" name=\"formConfirm\" value=\"", htmlspecialchars($_POST['formConfirm']), "\" /></td>\n\t</tr>\n";
248
        echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$this->lang['strsuper']}</label></th>\n";
249
        echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"",
250
        (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
251
        echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$this->lang['strcreatedb']}</label></th>\n";
252
        echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"",
253
        (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
254
        echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateRole\">{$this->lang['strcancreaterole']}</label></th>\n";
255
        echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateRole\" name=\"formCreateRole\"",
256
        (isset($_POST['formCreateRole'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
257
        echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formInherits\">{$this->lang['strinheritsprivs']}</label></th>\n";
258
        echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formInherits\" name=\"formInherits\"",
259
        (isset($_POST['formInherits'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
260
        echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCanLogin\">{$this->lang['strcanlogin']}</label></th>\n";
261
        echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCanLogin\" name=\"formCanLogin\"",
262
        (isset($_POST['formCanLogin'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
263
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconnlimit']}</th>\n";
264
        echo "\t\t<td class=\"data1\"><input size=\"4\" name=\"formConnLimit\" value=\"", htmlspecialchars($_POST['formConnLimit']), "\" /></td>\n\t</tr>\n";
265
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strexpires']}</th>\n";
266
        echo "\t\t<td class=\"data1\"><input size=\"23\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
267
268
        $roles = $data->getRoles();
269
        if ($roles->recordCount() > 0) {
270
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmemberof']}</th>\n";
271
            echo "\t\t<td class=\"data\">\n";
272
            echo "\t\t\t<select name=\"memberof[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
273
            while (!$roles->EOF) {
274
                $rolename = $roles->fields['rolname'];
275
                echo "\t\t\t\t<option value=\"{$rolename}\"",
276
                (in_array($rolename, $_POST['memberof'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
277
                $roles->moveNext();
278
            }
279
            echo "\t\t\t</select>\n";
280
            echo "\t\t</td>\n\t</tr>\n";
281
282
            $roles->moveFirst();
283
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>\n";
284
            echo "\t\t<td class=\"data\">\n";
285
            echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
286
            while (!$roles->EOF) {
287
                $rolename = $roles->fields['rolname'];
288
                echo "\t\t\t\t<option value=\"{$rolename}\"",
289
                (in_array($rolename, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
290
                $roles->moveNext();
291
            }
292
            echo "\t\t\t</select>\n";
293
            echo "\t\t</td>\n\t</tr>\n";
294
295
            $roles->moveFirst();
296
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stradminmembers']}</th>\n";
297
            echo "\t\t<td class=\"data\">\n";
298
            echo "\t\t\t<select name=\"adminmembers[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
299
            while (!$roles->EOF) {
300
                $rolename = $roles->fields['rolname'];
301
                echo "\t\t\t\t<option value=\"{$rolename}\"",
302
                (in_array($rolename, $_POST['adminmembers'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
303
                $roles->moveNext();
304
            }
305
            echo "\t\t\t</select>\n";
306
            echo "\t\t</td>\n\t</tr>\n";
307
        }
308
309
        echo "</table>\n";
310
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
311
        echo $this->misc->form;
312
        echo "<input type=\"submit\" name=\"create\" value=\"{$this->lang['strcreate']}\" />\n";
313
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
314
        echo "</form>\n";
315
    }
316
317
    /**
318
     * Actually creates the new role in the database.
319
     */
320
    public function doSaveCreate()
321
    {
322
        $data = $this->misc->getDatabaseAccessor();
323
324
        $this->coalesceArr($_POST, 'memberof', []);
325
326
        $this->coalesceArr($_POST, 'members', []);
327
328
        $this->coalesceArr($_POST, 'adminmembers', []);
329
330
        // Check data
331
        if ('' == $_POST['formRolename']) {
332
            $this->doCreate($this->lang['strroleneedsname']);
333
        } elseif ($_POST['formPassword'] != $_POST['formConfirm']) {
334
            $this->doCreate($this->lang['strpasswordconfirm']);
335
        } else {
336
            $status = $data->createRole(
337
                $_POST['formRolename'],
338
                $_POST['formPassword'],
339
                isset($_POST['formSuper']),
340
                isset($_POST['formCreateDB']),
341
                isset($_POST['formCreateRole']),
342
                isset($_POST['formInherits']),
343
                isset($_POST['formCanLogin']),
344
                $_POST['formConnLimit'],
345
                $_POST['formExpires'],
346
                $_POST['memberof'],
347
                $_POST['members'],
348
                $_POST['adminmembers']
349
            );
350
            if (0 == $status) {
351
                $this->doDefault($this->lang['strrolecreated']);
352
            } else {
353
                $this->doCreate($this->lang['strrolecreatedbad']);
354
            }
355
        }
356
    }
357
358
    /**
359
     * Function to allow alter a role.
360
     *
361
     * @param mixed $msg
362
     */
363
    public function doAlter($msg = '')
364
    {
365
        $data = $this->misc->getDatabaseAccessor();
366
367
        $this->printTrail('role');
368
        $this->printTitle($this->lang['stralter'], 'pg.role.alter');
369
        $this->printMsg($msg);
370
371
        $roledata = $data->getRole($_REQUEST['rolename']);
372
373
        if ($roledata->recordCount() > 0) {
374
            $server_info                       = $this->misc->getServerInfo();
375
            $canRename                         = $data->hasUserRename() && ($_REQUEST['rolename'] != $server_info['username']);
376
            $roledata->fields['rolsuper']      = $data->phpBool($roledata->fields['rolsuper']);
377
            $roledata->fields['rolcreatedb']   = $data->phpBool($roledata->fields['rolcreatedb']);
378
            $roledata->fields['rolcreaterole'] = $data->phpBool($roledata->fields['rolcreaterole']);
379
            $roledata->fields['rolinherit']    = $data->phpBool($roledata->fields['rolinherit']);
380
            $roledata->fields['rolcanlogin']   = $data->phpBool($roledata->fields['rolcanlogin']);
381
382
            if (!isset($_POST['formExpires'])) {
383
                if ($canRename) {
384
                    $_POST['formNewRoleName'] = $roledata->fields['rolname'];
385
                }
386
387
                if ($roledata->fields['rolsuper']) {
388
                    $_POST['formSuper'] = '';
389
                }
390
391
                if ($roledata->fields['rolcreatedb']) {
392
                    $_POST['formCreateDB'] = '';
393
                }
394
395
                if ($roledata->fields['rolcreaterole']) {
396
                    $_POST['formCreateRole'] = '';
397
                }
398
399
                if ($roledata->fields['rolinherit']) {
400
                    $_POST['formInherits'] = '';
401
                }
402
403
                if ($roledata->fields['rolcanlogin']) {
404
                    $_POST['formCanLogin'] = '';
405
                }
406
407
                $_POST['formConnLimit'] = '-1' == $roledata->fields['rolconnlimit'] ? '' : $roledata->fields['rolconnlimit'];
408
                $_POST['formExpires']   = 'infinity' == $roledata->fields['rolvaliduntil'] ? '' : $roledata->fields['rolvaliduntil'];
409
                $_POST['formPassword']  = '';
410
            }
411
412
            echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n";
413
            echo "<table>\n";
414
            echo "\t<tr>\n\t\t<th class=\"data left\" style=\"width: 130px\">{$this->lang['strname']}</th>\n";
415
            echo "\t\t<td class=\"data1\">", ($canRename ? "<input name=\"formNewRoleName\" size=\"15\" maxlength=\"{$data->_maxNameLen}\" value=\"" . htmlspecialchars($_POST['formNewRoleName']) . '" />' : $this->misc->printVal($roledata->fields['rolname'])), "</td>\n\t</tr>\n";
416
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strpassword']}</th>\n";
417
            echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"15\" name=\"formPassword\" value=\"", htmlspecialchars($_POST['formPassword']), "\" /></td>\n\t</tr>\n";
418
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconfirm']}</th>\n";
419
            echo "\t\t<td class=\"data1\"><input type=\"password\" size=\"15\" name=\"formConfirm\" value=\"\" /></td>\n\t</tr>\n";
420
            echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formSuper\">{$this->lang['strsuper']}</label></th>\n";
421
            echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formSuper\" name=\"formSuper\"",
422
            (isset($_POST['formSuper'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
423
            echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateDB\">{$this->lang['strcreatedb']}</label></th>\n";
424
            echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateDB\" name=\"formCreateDB\"",
425
            (isset($_POST['formCreateDB'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
426
            echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCreateRole\">{$this->lang['strcancreaterole']}</label></th>\n";
427
            echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCreateRole\" name=\"formCreateRole\"",
428
            (isset($_POST['formCreateRole'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
429
            echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formInherits\">{$this->lang['strinheritsprivs']}</label></th>\n";
430
            echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formInherits\" name=\"formInherits\"",
431
            (isset($_POST['formInherits'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
432
            echo "\t<tr>\n\t\t<th class=\"data left\"><label for=\"formCanLogin\">{$this->lang['strcanlogin']}</label></th>\n";
433
            echo "\t\t<td class=\"data1\"><input type=\"checkbox\" id=\"formCanLogin\" name=\"formCanLogin\"",
434
            (isset($_POST['formCanLogin'])) ? ' checked="checked"' : '', " /></td>\n\t</tr>\n";
435
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strconnlimit']}</th>\n";
436
            echo "\t\t<td class=\"data1\"><input size=\"4\" name=\"formConnLimit\" value=\"", htmlspecialchars($_POST['formConnLimit']), "\" /></td>\n\t</tr>\n";
437
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strexpires']}</th>\n";
438
            echo "\t\t<td class=\"data1\"><input size=\"23\" name=\"formExpires\" value=\"", htmlspecialchars($_POST['formExpires']), "\" /></td>\n\t</tr>\n";
439
440
            if (!isset($_POST['memberof'])) {
441
                $memberof = $data->getMemberOf($_REQUEST['rolename']);
442
                if ($memberof->recordCount() > 0) {
443
                    $i = 0;
444
                    while (!$memberof->EOF) {
445
                        $_POST['memberof'][$i++] = $memberof->fields['rolname'];
446
                        $memberof->moveNext();
447
                    }
448
                } else {
449
                    $_POST['memberof'] = [];
450
                }
451
452
                $memberofold = implode(',', $_POST['memberof']);
453
            }
454
            if (!isset($_POST['members'])) {
455
                $members = $data->getMembers($_REQUEST['rolename']);
456
                if ($members->recordCount() > 0) {
457
                    $i = 0;
458
                    while (!$members->EOF) {
459
                        $_POST['members'][$i++] = $members->fields['rolname'];
460
                        $members->moveNext();
461
                    }
462
                } else {
463
                    $_POST['members'] = [];
464
                }
465
466
                $membersold = implode(',', $_POST['members']);
467
            }
468
            if (!isset($_POST['adminmembers'])) {
469
                $adminmembers = $data->getMembers($_REQUEST['rolename'], 't');
470
                if ($adminmembers->recordCount() > 0) {
471
                    $i = 0;
472
                    while (!$adminmembers->EOF) {
473
                        $_POST['adminmembers'][$i++] = $adminmembers->fields['rolname'];
474
                        $adminmembers->moveNext();
475
                    }
476
                } else {
477
                    $_POST['adminmembers'] = [];
478
                }
479
480
                $adminmembersold = implode(',', $_POST['adminmembers']);
481
            }
482
483
            $roles = $data->getRoles($_REQUEST['rolename']);
484
            if ($roles->recordCount() > 0) {
485
                echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmemberof']}</th>\n";
486
                echo "\t\t<td class=\"data\">\n";
487
                echo "\t\t\t<select name=\"memberof[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
488
                while (!$roles->EOF) {
489
                    $rolename = $roles->fields['rolname'];
490
                    echo "\t\t\t\t<option value=\"{$rolename}\"",
491
                    (in_array($rolename, $_POST['memberof'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
492
                    $roles->moveNext();
493
                }
494
                echo "\t\t\t</select>\n";
495
                echo "\t\t</td>\n\t</tr>\n";
496
497
                $roles->moveFirst();
498
                echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>\n";
499
                echo "\t\t<td class=\"data\">\n";
500
                echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
501
                while (!$roles->EOF) {
502
                    $rolename = $roles->fields['rolname'];
503
                    echo "\t\t\t\t<option value=\"{$rolename}\"",
504
                    (in_array($rolename, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
505
                    $roles->moveNext();
506
                }
507
                echo "\t\t\t</select>\n";
508
                echo "\t\t</td>\n\t</tr>\n";
509
510
                $roles->moveFirst();
511
                echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stradminmembers']}</th>\n";
512
                echo "\t\t<td class=\"data\">\n";
513
                echo "\t\t\t<select name=\"adminmembers[]\" multiple=\"multiple\" size=\"", min(20, $roles->recordCount()), "\">\n";
514
                while (!$roles->EOF) {
515
                    $rolename = $roles->fields['rolname'];
516
                    echo "\t\t\t\t<option value=\"{$rolename}\"",
517
                    (in_array($rolename, $_POST['adminmembers'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($rolename), "</option>\n";
518
                    $roles->moveNext();
519
                }
520
                echo "\t\t\t</select>\n";
521
                echo "\t\t</td>\n\t</tr>\n";
522
            }
523
            echo "</table>\n";
524
525
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
526
            echo '<input type="hidden" name="rolename" value="', htmlspecialchars($_REQUEST['rolename']), "\" />\n";
527
            echo '<input type="hidden" name="memberofold" value="', isset($_POST['memberofold']) ? $_POST['memberofold'] : htmlspecialchars($memberofold), "\" />\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $memberofold does not seem to be defined for all execution paths leading up to this point.
Loading history...
528
            echo '<input type="hidden" name="membersold" value="', isset($_POST['membersold']) ? $_POST['membersold'] : htmlspecialchars($membersold), "\" />\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $membersold does not seem to be defined for all execution paths leading up to this point.
Loading history...
529
            echo '<input type="hidden" name="adminmembersold" value="', isset($_POST['adminmembersold']) ? $_POST['adminmembersold'] : htmlspecialchars($adminmembersold), "\" />\n";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $adminmembersold does not seem to be defined for all execution paths leading up to this point.
Loading history...
530
            echo $this->misc->form;
531
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
532
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
533
            echo "</form>\n";
534
        } else {
535
            echo "<p>{$this->lang['strnodata']}</p>\n";
536
        }
537
    }
538
539
    /**
540
     * Function to save after editing a role.
541
     */
542
    public function doSaveAlter()
543
    {
544
        $data = $this->misc->getDatabaseAccessor();
545
546
        $this->coalesceArr($_POST, 'memberof', []);
547
548
        $this->coalesceArr($_POST, 'members', []);
549
550
        $this->coalesceArr($_POST, 'adminmembers', []);
551
552
        // Check name and password
553
        if (isset($_POST['formNewRoleName']) && '' == $_POST['formNewRoleName']) {
554
            $this->doAlter($this->lang['strroleneedsname']);
555
        } elseif ($_POST['formPassword'] != $_POST['formConfirm']) {
556
            $this->doAlter($this->lang['strpasswordconfirm']);
557
        } else {
558
            if (isset($_POST['formNewRoleName'])) {
559
                $status = $data->setRenameRole($_POST['rolename'], $_POST['formPassword'], isset($_POST['formSuper']), isset($_POST['formCreateDB']), isset($_POST['formCreateRole']), isset($_POST['formInherits']), isset($_POST['formCanLogin']), $_POST['formConnLimit'], $_POST['formExpires'], $_POST['memberof'], $_POST['members'], $_POST['adminmembers'], $_POST['memberofold'], $_POST['membersold'], $_POST['adminmembersold'], $_POST['formNewRoleName']);
560
            } else {
561
                $status = $data->setRole($_POST['rolename'], $_POST['formPassword'], isset($_POST['formSuper']), isset($_POST['formCreateDB']), isset($_POST['formCreateRole']), isset($_POST['formInherits']), isset($_POST['formCanLogin']), $_POST['formConnLimit'], $_POST['formExpires'], $_POST['memberof'], $_POST['members'], $_POST['adminmembers'], $_POST['memberofold'], $_POST['membersold'], $_POST['adminmembersold']);
562
            }
563
564
            if (0 == $status) {
565
                $this->doDefault($this->lang['strrolealtered']);
566
            } else {
567
                $this->doAlter($this->lang['strrolealteredbad']);
568
            }
569
        }
570
    }
571
572
    /**
573
     * Show confirmation of drop a role and perform actual drop.
574
     *
575
     * @param mixed $confirm
576
     */
577
    public function doDrop($confirm)
578
    {
579
        $data = $this->misc->getDatabaseAccessor();
580
581
        if ($confirm) {
582
            $this->printTrail('role');
583
            $this->printTitle($this->lang['strdroprole'], 'pg.role.drop');
584
585
            echo '<p>', sprintf($this->lang['strconfdroprole'], $this->misc->printVal($_REQUEST['rolename'])), "</p>\n";
586
587
            echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n";
588
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
589
            echo '<input type="hidden" name="rolename" value="', htmlspecialchars($_REQUEST['rolename']), "\" />\n";
590
            echo $this->misc->form;
591
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
592
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
593
            echo "</form>\n";
594
        } else {
595
            $status = $data->dropRole($_REQUEST['rolename']);
596
            if (0 == $status) {
597
                $this->doDefault($this->lang['strroledropped']);
598
            } else {
599
                $this->doDefault($this->lang['strroledroppedbad']);
600
            }
601
        }
602
    }
603
604
    /**
605
     * Show the properties of a role.
606
     *
607
     * @param mixed $msg
608
     */
609
    public function doProperties($msg = '')
610
    {
611
        $data = $this->misc->getDatabaseAccessor();
612
613
        $this->printTrail('role');
614
        $this->printTitle($this->lang['strproperties'], 'pg.role');
615
        $this->printMsg($msg);
616
617
        $roledata = $data->getRole($_REQUEST['rolename']);
618
        if ($roledata->recordCount() > 0) {
619
            $roledata->fields['rolsuper']      = $data->phpBool($roledata->fields['rolsuper']);
620
            $roledata->fields['rolcreatedb']   = $data->phpBool($roledata->fields['rolcreatedb']);
621
            $roledata->fields['rolcreaterole'] = $data->phpBool($roledata->fields['rolcreaterole']);
622
            $roledata->fields['rolinherit']    = $data->phpBool($roledata->fields['rolinherit']);
623
            $roledata->fields['rolcanlogin']   = $data->phpBool($roledata->fields['rolcanlogin']);
624
625
            echo "<table>\n";
626
            echo "\t<tr>\n\t\t<th class=\"data\" style=\"width: 130px\">Description</th>\n";
627
            echo "\t\t<th class=\"data\" style=\"width: 120\">Value</th>\n\t</tr>\n";
628
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strname']}</td>\n";
629
            echo "\t\t<td class=\"data1\">", htmlspecialchars($_REQUEST['rolename']), "</td>\n\t</tr>\n";
630
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strsuper']}</td>\n";
631
            echo "\t\t<td class=\"data2\">", (($roledata->fields['rolsuper']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n\t</tr>\n";
632
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strcreatedb']}</td>\n";
633
            echo "\t\t<td class=\"data1\">", (($roledata->fields['rolcreatedb']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n";
634
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strcancreaterole']}</td>\n";
635
            echo "\t\t<td class=\"data2\">", (($roledata->fields['rolcreaterole']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n";
636
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strinheritsprivs']}</td>\n";
637
            echo "\t\t<td class=\"data1\">", (($roledata->fields['rolinherit']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n";
638
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strcanlogin']}</td>\n";
639
            echo "\t\t<td class=\"data2\">", (($roledata->fields['rolcanlogin']) ? $this->lang['stryes'] : $this->lang['strno']), "</td>\n";
640
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strconnlimit']}</td>\n";
641
            echo "\t\t<td class=\"data1\">", ('-1' == $roledata->fields['rolconnlimit'] ? $this->lang['strnolimit'] : $this->misc->printVal($roledata->fields['rolconnlimit'])), "</td>\n";
642
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strexpires']}</td>\n";
643
            echo "\t\t<td class=\"data2\">", ('infinity' == $roledata->fields['rolvaliduntil'] || is_null($roledata->fields['rolvaliduntil']) ? $this->lang['strnever'] : $this->misc->printVal($roledata->fields['rolvaliduntil'])), "</td>\n";
644
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strsessiondefaults']}</td>\n";
645
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolconfig']), "</td>\n";
646
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['strmemberof']}</td>\n";
647
            echo "\t\t<td class=\"data2\">";
648
            $memberof = $data->getMemberOf($_REQUEST['rolename']);
649
            if ($memberof->recordCount() > 0) {
650
                while (!$memberof->EOF) {
651
                    echo $this->misc->printVal($memberof->fields['rolname']), "<br />\n";
652
                    $memberof->moveNext();
653
                }
654
            }
655
            echo "</td>\n\t</tr>\n";
656
            echo "\t<tr>\n\t\t<td class=\"data1\">{$this->lang['strmembers']}</td>\n";
657
            echo "\t\t<td class=\"data1\">";
658
            $members = $data->getMembers($_REQUEST['rolename']);
659
            if ($members->recordCount() > 0) {
660
                while (!$members->EOF) {
661
                    echo $this->misc->printVal($members->fields['rolname']), "<br />\n";
662
                    $members->moveNext();
663
                }
664
            }
665
            echo "</td>\n\t</tr>\n";
666
            echo "\t<tr>\n\t\t<td class=\"data2\">{$this->lang['stradminmembers']}</td>\n";
667
            echo "\t\t<td class=\"data2\">";
668
            $adminmembers = $data->getMembers($_REQUEST['rolename'], 't');
669
            if ($adminmembers->recordCount() > 0) {
670
                while (!$adminmembers->EOF) {
671
                    echo $this->misc->printVal($adminmembers->fields['rolname']), "<br />\n";
672
                    $adminmembers->moveNext();
673
                }
674
            }
675
            echo "</td>\n\t</tr>\n";
676
            echo "</table>\n";
677
        } else {
678
            echo "<p>{$this->lang['strnodata']}</p>\n";
679
        }
680
681
        $navlinks = [
682
            'showall' => [
683
                'attr'    => [
684
                    'href' => [
685
                        'url'     => 'roles',
686
                        'urlvars' => [
687
                            'server' => $_REQUEST['server'],
688
                        ],
689
                    ],
690
                ],
691
                'content' => $this->lang['strshowallroles'],
692
            ],
693
            'alter'   => [
694
                'attr'    => [
695
                    'href' => [
696
                        'url'     => 'roles',
697
                        'urlvars' => [
698
                            'action'   => 'alter',
699
                            'server'   => $_REQUEST['server'],
700
                            'rolename' => $_REQUEST['rolename'],
701
                        ],
702
                    ],
703
                ],
704
                'content' => $this->lang['stralter'],
705
            ],
706
            'drop'    => [
707
                'attr'    => [
708
                    'href' => [
709
                        'url'     => 'roles',
710
                        'urlvars' => [
711
                            'action'   => 'confirm_drop',
712
                            'server'   => $_REQUEST['server'],
713
                            'rolename' => $_REQUEST['rolename'],
714
                        ],
715
                    ],
716
                ],
717
                'content' => $this->lang['strdrop'],
718
            ],
719
        ];
720
721
        $this->printNavLinks($navlinks, 'roles-properties', get_defined_vars());
722
    }
723
724
    /**
725
     * If a role is not a superuser role, then we have an 'account management'
726
     * page for change his password, etc.  We don't prevent them from
727
     * messing with the URL to gain access to other role admin stuff, because
728
     * the PostgreSQL permissions will prevent them changing anything anyway.
729
     *
730
     * @param mixed $msg
731
     */
732
    public function doAccount($msg = '')
733
    {
734
        $data = $this->misc->getDatabaseAccessor();
735
736
        $server_info = $this->misc->getServerInfo();
737
738
        $roledata             = $data->getRole($server_info['username']);
739
        $_REQUEST['rolename'] = $server_info['username'];
740
741
        $this->printTrail('role');
742
        $this->printTabs('server', 'account');
743
        $this->printMsg($msg);
744
745
        if ($roledata->recordCount() > 0) {
746
            $roledata->fields['rolsuper']      = $data->phpBool($roledata->fields['rolsuper']);
747
            $roledata->fields['rolcreatedb']   = $data->phpBool($roledata->fields['rolcreatedb']);
748
            $roledata->fields['rolcreaterole'] = $data->phpBool($roledata->fields['rolcreaterole']);
749
            $roledata->fields['rolinherit']    = $data->phpBool($roledata->fields['rolinherit']);
750
            echo "<table>\n";
751
            echo "\t<tr>\n\t\t<th class=\"data\">{$this->lang['strname']}</th>\n";
752
            echo "\t\t<th class=\"data\">{$this->lang['strsuper']}</th>\n";
753
            echo "\t\t<th class=\"data\">{$this->lang['strcreatedb']}</th>\n";
754
            echo "\t\t<th class=\"data\">{$this->lang['strcancreaterole']}</th>\n";
755
            echo "\t\t<th class=\"data\">{$this->lang['strinheritsprivs']}</th>\n";
756
            echo "\t\t<th class=\"data\">{$this->lang['strconnlimit']}</th>\n";
757
            echo "\t\t<th class=\"data\">{$this->lang['strexpires']}</th>\n";
758
            echo "\t\t<th class=\"data\">{$this->lang['strsessiondefaults']}</th>\n";
759
            echo "\t</tr>\n";
760
            echo "\t<tr>\n\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolname']), "</td>\n";
761
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolsuper'], 'yesno'), "</td>\n";
762
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolcreatedb'], 'yesno'), "</td>\n";
763
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolcreaterole'], 'yesno'), "</td>\n";
764
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolinherit'], 'yesno'), "</td>\n";
765
            echo "\t\t<td class=\"data1\">", ('-1' == $roledata->fields['rolconnlimit'] ? $this->lang['strnolimit'] : $this->misc->printVal($roledata->fields['rolconnlimit'])), "</td>\n";
766
            echo "\t\t<td class=\"data1\">", ('infinity' == $roledata->fields['rolvaliduntil'] || is_null($roledata->fields['rolvaliduntil']) ? $this->lang['strnever'] : $this->misc->printVal($roledata->fields['rolvaliduntil'])), "</td>\n";
767
            echo "\t\t<td class=\"data1\">", $this->misc->printVal($roledata->fields['rolconfig']), "</td>\n";
768
            echo "\t</tr>\n</table>\n";
769
        } else {
770
            echo "<p>{$this->lang['strnodata']}</p>\n";
771
        }
772
773
        $this->printNavLinks(['changepassword' => [
774
            'attr'    => [
775
                'href' => [
776
                    'url'     => 'roles',
777
                    'urlvars' => [
778
                        'action' => 'confchangepassword',
779
                        'server' => $_REQUEST['server'],
780
                    ],
781
                ],
782
            ],
783
            'content' => $this->lang['strchangepassword'],
784
        ]], 'roles-account', get_defined_vars());
785
    }
786
787
    /**
788
     * Show confirmation of change password and actually change password.
789
     *
790
     * @param mixed $confirm
791
     * @param mixed $msg
792
     */
793
    public function doChangePassword($confirm, $msg = '')
794
    {
795
        $data = $this->misc->getDatabaseAccessor();
796
797
        $server_info = $this->misc->getServerInfo();
798
799
        if ($confirm) {
800
            $_REQUEST['rolename'] = $server_info['username'];
801
            $this->printTrail('role');
802
            $this->printTitle($this->lang['strchangepassword'], 'pg.role.alter');
803
            $this->printMsg($msg);
804
805
            $this->coalesceArr($_POST, 'password', '');
806
807
            $this->coalesceArr($_POST, 'confirm', '');
808
809
            echo '<form action="' . \SUBFOLDER . "/src/views/roles\" method=\"post\">\n";
810
            echo "<table>\n";
811
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strpassword']}</th>\n";
812
            echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"",
813
            htmlspecialchars($_POST['password']), "\" /></td>\n\t</tr>\n";
814
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strconfirm']}</th>\n";
815
            echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>\n";
816
            echo "</table>\n";
817
            echo "<p><input type=\"hidden\" name=\"action\" value=\"changepassword\" />\n";
818
            echo $this->misc->form;
819
            echo "<input type=\"submit\" name=\"ok\" value=\"{$this->lang['strok']}\" />\n";
820
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
821
            echo "</p></form>\n";
822
        } else {
823
            // Check that password is minimum length
824
            if (strlen($_POST['password']) < $this->conf['min_password_length']) {
825
                $this->doChangePassword(true, $this->lang['strpasswordshort']);
826
            }
827
828
            // Check that password matches confirmation password
829
            elseif ($_POST['password'] != $_POST['confirm']) {
0 ignored issues
show
Coding Style introduced by
Expected "} elseif (...) \n"; found "\n\n // Check that password matches confirmation password\n elseif (...) {\n"
Loading history...
830
                $this->doChangePassword(true, $this->lang['strpasswordconfirm']);
831
            } else {
832
                $status = $data->changePassword($server_info['username'], $_POST['password']);
833
                if (0 == $status) {
834
                    $this->doAccount($this->lang['strpasswordchanged']);
835
                } else {
836
                    $this->doAccount($this->lang['strpasswordchangedbad']);
837
                }
838
            }
839
        }
840
    }
841
}
842