Passed
Pull Request — develop (#92)
by Felipe
04:47
created

RolesController::doSaveCreate()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
840
    }
841
842
    /**
843
     * Show confirmation of change password and actually change password.
844
     *
845
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
846
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
847
     */
848
    public function doChangePassword($confirm, $msg = '')
849
    {
850
        $conf = $this->conf;
851
852
        $lang = $this->lang;
853
        $data = $this->misc->getDatabaseAccessor();
854
855
        $server_info = $this->misc->getServerInfo();
856
857
        if ($confirm) {
858
            $_REQUEST['rolename'] = $server_info['username'];
859
            $this->printTrail('role');
860
            $this->printTitle($lang['strchangepassword'], 'pg.role.alter');
861
            $this->printMsg($msg);
862
863
            if (!isset($_POST['password'])) {
864
                $_POST['password'] = '';
865
            }
866
867
            if (!isset($_POST['confirm'])) {
868
                $_POST['confirm'] = '';
869
            }
870
871
            echo '<form action="'.\SUBFOLDER."/src/views/roles.php\" method=\"post\">\n";
872
            echo "<table>\n";
873
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strpassword']}</th>\n";
874
            echo "\t\t<td><input type=\"password\" name=\"password\" size=\"32\" value=\"",
875
            htmlspecialchars($_POST['password']), "\" /></td>\n\t</tr>\n";
876
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strconfirm']}</th>\n";
877
            echo "\t\t<td><input type=\"password\" name=\"confirm\" size=\"32\" value=\"\" /></td>\n\t</tr>\n";
878
            echo "</table>\n";
879
            echo "<p><input type=\"hidden\" name=\"action\" value=\"changepassword\" />\n";
880
            echo $this->misc->form;
881
            echo "<input type=\"submit\" name=\"ok\" value=\"{$lang['strok']}\" />\n";
882
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
883
            echo "</p></form>\n";
884
        } else {
885
            // Check that password is minimum length
886
            if (strlen($_POST['password']) < $conf['min_password_length']) {
887
                $this->doChangePassword(true, $lang['strpasswordshort']);
888
            }
889
890
            // Check that password matches confirmation password
891
            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...
892
                $this->doChangePassword(true, $lang['strpasswordconfirm']);
893
            } else {
894
                $status = $data->changePassword($server_info['username'], $_POST['password']);
895
                if (0 == $status) {
896
                    $this->doAccount($lang['strpasswordchanged']);
897
                } else {
898
                    $this->doAccount($lang['strpasswordchangedbad']);
899
                }
900
            }
901
        }
902
    }
903
}
904