Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

RolesController::doDefault()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 112
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 76
nc 1
nop 1
dl 0
loc 112
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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