Passed
Push — develop ( 51bd2c...501708 )
by Felipe
05:51
created

RolesController::render()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 69
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 5.6693
c 0
b 0
f 0
cc 15
eloc 49
nc 15
nop 0

How to fix   Long Method    Complexity   

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
3
/**
4
 * PHPPgAdmin v6.0.0-beta.40
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class RolesController extends BaseController
17
{
18
    public $controller_name = 'RolesController';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        $data = $this->misc->getDatabaseAccessor();
1 ignored issue
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
26
27
        $this->printHeader($this->lang['strroles']);
28
        $this->printBody();
29
30
        switch ($this->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
98
     */
99
    public function doDefault($msg = '')
100
    {
101
        $data = $this->misc->getDatabaseAccessor();
102
103
        $renderRoleConnLimit = function ($val) use ($lang) {
0 ignored issues
show
Unused Code introduced by
The import $lang is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Comprehensibility Best Practice introduced by
The variable $lang seems to be never defined.
Loading history...
104
            return '-1' == $val ? $this->lang['strnolimit'] : htmlspecialchars($val);
105
        };
106
107
        $renderRoleExpires = function ($val) use ($lang) {
0 ignored issues
show
Unused Code introduced by
The import $lang is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

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