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

PrivilegesController   F

Complexity

Total Complexity 71

Size/Duplication

Total Lines 417
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 417
rs 2.6315
c 0
b 0
f 0
wmc 71

3 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 31 4
F doDefault() 0 214 36
F doAlter() 0 148 31

How to fix   Complexity   

Complex Class

Complex classes like PrivilegesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PrivilegesController, and based on these observations, apply Extract Interface, too.

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
/**
10
 * PrivilegesController controller class.
11
 */
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...
12
class PrivilegesController extends BaseController
13
{
14
    public $controller_name = 'PrivilegesController';
15
    public $table_place     = 'privileges-privileges';
16
17
    /**
18
     * Default method to render the controller according to the action parameter.
19
     */
20
    public function render()
21
    {
22
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
23
24
        $lang   = $this->lang;
25
        $action = $this->action;
26
        $data   = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
27
28
        $this->printHeader($lang['strprivileges']);
29
        $this->printBody();
30
31
        switch ($action) {
32
            case 'save':
33
                if (isset($_REQUEST['cancel'])) {
34
                    $this->doDefault();
35
                } else {
36
                    $this->doAlter(false, $_REQUEST['mode']);
37
                }
38
39
                break;
40
            case 'alter':
41
                $this->doAlter(true, $_REQUEST['mode']);
42
43
                break;
44
            default:
45
                $this->doDefault();
46
47
                break;
48
        }
49
50
        $this->printFooter();
51
    }
52
53
    /**
54
     * Show permissions on a database, namespace, relation, language or function.
55
     *
56
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     */
58
    public function doDefault($msg = '')
59
    {
60
        $conf = $this->conf;
61
62
        $lang     = $this->lang;
63
        $action   = $this->action;
64
        $data     = $this->misc->getDatabaseAccessor();
65
        $database = $this->misc->getDatabase();
66
67
        $this->printTrail($_REQUEST['subject']);
68
69
        // @@@FIXME: This switch is just a temporary solution,
70
        // need a better way, maybe every type of object should
71
        // have a tab bar???
72
        switch ($_REQUEST['subject']) {
73
            case 'server':
74
            case 'database':
75
            case 'schema':
76
            case 'table':
77
            case 'column':
78
            case 'view':
79
                $this->printTabs($_REQUEST['subject'], 'privileges');
80
81
                break;
82
            default:
83
                $this->printTitle($lang['strprivileges'], 'pg.privilege');
84
        }
85
        $this->printMsg($msg);
86
87
        // Determine whether object should be ref'd by name or oid.
88
        if (isset($_REQUEST[$_REQUEST['subject'].'_oid'])) {
89
            $object = $_REQUEST[$_REQUEST['subject'].'_oid'];
90
        } else {
91
            $object = $_REQUEST[$_REQUEST['subject']];
92
        }
93
94
        // Get the privileges on the object, given its type
95
        if ('column' == $_REQUEST['subject']) {
96
            $privileges = $data->getPrivileges($object, 'column', $_REQUEST['table']);
97
        } else {
98
            $privileges = $data->getPrivileges($object, $_REQUEST['subject']);
99
        }
100
101
        if (sizeof($privileges) > 0) {
102
            echo "<table>\n";
103
            if ($data->hasRoles()) {
104
                echo "<tr><th class=\"data\">{$lang['strrole']}</th>";
105
            } else {
106
                echo "<tr><th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['struser']}/{$lang['strgroup']}</th>";
107
            }
108
109
            foreach ($data->privlist[$_REQUEST['subject']] as $v2) {
110
                // Skip over ALL PRIVILEGES
111
                if ('ALL PRIVILEGES' == $v2) {
112
                    continue;
113
                }
114
115
                echo "<th class=\"data\">{$v2}</th>\n";
116
            }
117
            if ($data->hasGrantOption()) {
118
                echo "<th class=\"data\">{$lang['strgrantor']}</th>";
119
            }
120
            echo "</tr>\n";
121
122
            // Loop over privileges, outputting them
123
            $i = 0;
124
            foreach ($privileges as $v) {
125
                $id = (0 == ($i % 2) ? '1' : '2');
126
                echo "<tr class=\"data{$id}\">\n";
127
                if (!$data->hasRoles()) {
128
                    echo '<td>', $this->misc->printVal($v[0]), "</td>\n";
129
                }
130
131
                echo '<td>', $this->misc->printVal($v[1]), "</td>\n";
132
                foreach ($data->privlist[$_REQUEST['subject']] as $v2) {
133
                    // Skip over ALL PRIVILEGES
134
                    if ('ALL PRIVILEGES' == $v2) {
135
                        continue;
136
                    }
137
138
                    echo '<td>';
139
                    if (in_array($v2, $v[2], true)) {
140
                        echo $lang['stryes'];
141
                    } else {
142
                        echo $lang['strno'];
143
                    }
144
145
                    // If we have grant option for this, end mark
146
                    if ($data->hasGrantOption() && in_array($v2, $v[4], true)) {
147
                        echo $lang['strasterisk'];
148
                    }
149
150
                    echo "</td>\n";
151
                }
152
                if ($data->hasGrantOption()) {
153
                    echo '<td>', $this->misc->printVal($v[3]), "</td>\n";
154
                }
155
                echo "</tr>\n";
156
                ++$i;
157
            }
158
159
            echo '</table>';
160
        } else {
161
            echo "<p>{$lang['strnoprivileges']}</p>\n";
162
        }
163
164
        // Links for granting to a user or group
165
        switch ($_REQUEST['subject']) {
166
            case 'table':
167
            case 'view':
168
            case 'sequence':
169
            case 'function':
170
            case 'tablespace':
171
                $alllabel = "showall{$_REQUEST['subject']}s";
172
                $allurl   = "{$_REQUEST['subject']}s.php";
173
                $alltxt   = $lang["strshowall{$_REQUEST['subject']}s"];
174
175
                break;
176
            case 'schema':
177
                $alllabel = 'showallschemas';
178
                $allurl   = 'schemas.php';
179
                $alltxt   = $lang['strshowallschemas'];
180
181
                break;
182
            case 'database':
183
                $alllabel = 'showalldatabases';
184
                $allurl   = 'alldb.php';
185
                $alltxt   = $lang['strshowalldatabases'];
186
187
                break;
188
        }
189
190
        $subject = $_REQUEST['subject'];
191
        $object  = $_REQUEST[$_REQUEST['subject']];
192
193
        if ('function' == $_REQUEST['subject']) {
194
            $objectoid = $_REQUEST[$_REQUEST['subject'].'_oid'];
195
            $urlvars   = [
196
                'action'         => 'alter',
197
                'server'         => $_REQUEST['server'],
198
                'database'       => $_REQUEST['database'],
199
                'schema'         => $_REQUEST['schema'],
200
                $subject         => $object,
201
                "{$subject}_oid" => $objectoid,
202
                'subject'        => $subject,
203
            ];
204
        } elseif ('column' == $_REQUEST['subject']) {
205
            $urlvars = [
206
                'action'   => 'alter',
207
                'server'   => $_REQUEST['server'],
208
                'database' => $_REQUEST['database'],
209
                'schema'   => $_REQUEST['schema'],
210
                $subject   => $object,
211
                'subject'  => $subject,
212
            ];
213
214
            if (isset($_REQUEST['table'])) {
215
                $urlvars['table'] = $_REQUEST['table'];
216
            } else {
217
                $urlvars['view'] = $_REQUEST['view'];
218
            }
219
        } else {
220
            $urlvars = [
221
                'action'   => 'alter',
222
                'server'   => $_REQUEST['server'],
223
                'database' => $_REQUEST['database'],
224
                $subject   => $object,
225
                'subject'  => $subject,
226
            ];
227
            if (isset($_REQUEST['schema'])) {
228
                $urlvars['schema'] = $_REQUEST['schema'];
229
            }
230
        }
231
232
        $navlinks = [
233
            'grant' => [
234
                'attr' => [
235
                    'href' => [
236
                        'url'     => 'privileges.php',
237
                        'urlvars' => array_merge($urlvars, ['mode' => 'grant']),
238
                    ],
239
                ],
240
                'content' => $lang['strgrant'],
241
            ],
242
            'revoke' => [
243
                'attr' => [
244
                    'href' => [
245
                        'url'     => 'privileges.php',
246
                        'urlvars' => array_merge($urlvars, ['mode' => 'revoke']),
247
                    ],
248
                ],
249
                'content' => $lang['strrevoke'],
250
            ],
251
        ];
252
253
        if (isset($allurl)) {
254
            $navlinks[$alllabel] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $alllabel does not seem to be defined for all execution paths leading up to this point.
Loading history...
255
                'attr' => [
256
                    'href' => [
257
                        'url'     => $allurl,
258
                        'urlvars' => [
259
                            'server'   => $_REQUEST['server'],
260
                            'database' => $_REQUEST['database'],
261
                        ],
262
                    ],
263
                ],
264
                'content' => $alltxt,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $alltxt does not seem to be defined for all execution paths leading up to this point.
Loading history...
265
            ];
266
            if (isset($_REQUEST['schema'])) {
267
                $navlinks[$alllabel]['attr']['href']['urlvars']['schema'] = $_REQUEST['schema'];
268
            }
269
        }
270
271
        $this->printNavLinks($navlinks, $this->table_place, get_defined_vars());
272
    }
273
274
    /**
275
     * Grant permissions on an object to a user.
276
     *
277
     * @param $confirm To show entry screen
278
     * @param $mode 'grant' or 'revoke'
279
     * @param $msg (optional) A message to show
280
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'grant' at position 0 could not be parsed: Unknown type name ''grant'' at position 0 in 'grant'.
Loading history...
281
    public function doAlter($confirm, $mode, $msg = '')
282
    {
283
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
284
285
        $lang   = $this->lang;
286
        $action = $this->action;
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
287
        $data   = $this->misc->getDatabaseAccessor();
288
289
        if (!isset($_REQUEST['username'])) {
290
            $_REQUEST['username'] = [];
291
        }
292
293
        if (!isset($_REQUEST['groupname'])) {
294
            $_REQUEST['groupname'] = [];
295
        }
296
297
        if (!isset($_REQUEST['privilege'])) {
298
            $_REQUEST['privilege'] = [];
299
        }
300
301
        if ($confirm) {
302
            // Get users from the database
303
            $users = $data->getUsers();
304
            // Get groups from the database
305
            $groups = $data->getGroups();
306
307
            $this->printTrail($_REQUEST['subject']);
308
309
            switch ($mode) {
310
                case 'grant':
311
                    $this->printTitle($lang['strgrant'], 'pg.privilege.grant');
312
313
                    break;
314
                case 'revoke':
315
                    $this->printTitle($lang['strrevoke'], 'pg.privilege.revoke');
316
317
                    break;
318
            }
319
            $this->printMsg($msg);
320
321
            echo '<form action="'.\SUBFOLDER."/src/views/privileges.php\" method=\"post\">\n";
322
            echo "<table>\n";
323
            echo "<tr><th class=\"data left\">{$lang['strusers']}</th>\n";
324
            echo '<td class="data1"><select name="username[]" multiple="multiple" size="', min(6, $users->recordCount()), "\">\n";
325
            while (!$users->EOF) {
326
                $uname = htmlspecialchars($users->fields['usename']);
327
                echo "<option value=\"{$uname}\"",
328
                in_array($users->fields['usename'], $_REQUEST['username'], true) ? ' selected="selected"' : '', ">{$uname}</option>\n";
329
                $users->moveNext();
330
            }
331
            echo "</select></td></tr>\n";
332
            echo "<tr><th class=\"data left\">{$lang['strgroups']}</th>\n";
333
            echo "<td class=\"data1\">\n";
334
            echo '<input type="checkbox" id="public" name="public"', (isset($_REQUEST['public']) ? ' checked="checked"' : ''), " /><label for=\"public\">PUBLIC</label>\n";
335
            // Only show groups if there are groups!
336
            if ($groups->recordCount() > 0) {
337
                echo '<br /><select name="groupname[]" multiple="multiple" size="', min(6, $groups->recordCount()), "\">\n";
338
                while (!$groups->EOF) {
339
                    $gname = htmlspecialchars($groups->fields['groname']);
340
                    echo "<option value=\"{$gname}\"",
341
                    in_array($groups->fields['groname'], $_REQUEST['groupname'], true) ? ' selected="selected"' : '', ">{$gname}</option>\n";
342
                    $groups->moveNext();
343
                }
344
                echo "</select>\n";
345
            }
346
            echo "</td></tr>\n";
347
            echo "<tr><th class=\"data left required\">{$lang['strprivileges']}</th>\n";
348
            echo "<td class=\"data1\">\n";
349
            foreach ($data->privlist[$_REQUEST['subject']] as $v) {
350
                $v = htmlspecialchars($v);
351
                echo "<input type=\"checkbox\" id=\"privilege[${v}]\" name=\"privilege[${v}]\"",
352
                isset($_REQUEST['privilege'][$v]) ? ' checked="checked"' : '', " /><label for=\"privilege[${v}]\">{$v}</label><br />\n";
353
            }
354
            echo "</td></tr>\n";
355
            // Grant option
356
            if ($data->hasGrantOption()) {
357
                echo "<tr><th class=\"data left\">{$lang['stroptions']}</th>\n";
358
                echo "<td class=\"data1\">\n";
359
                if ('grant' == $mode) {
360
                    echo '<input type="checkbox" id="grantoption" name="grantoption"',
361
                    isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', " /><label for=\"grantoption\">GRANT OPTION</label>\n";
362
                } elseif ('revoke' == $mode) {
363
                    echo '<input type="checkbox" id="grantoption" name="grantoption"',
364
                    isset($_REQUEST['grantoption']) ? ' checked="checked"' : '', " /><label for=\"grantoption\">GRANT OPTION FOR</label><br />\n";
365
                    echo '<input type="checkbox" id="cascade" name="cascade"',
366
                    isset($_REQUEST['cascade']) ? ' checked="checked"' : '', " /><label for=\"cascade\">CASCADE</label><br />\n";
367
                }
368
                echo "</td></tr>\n";
369
            }
370
            echo "</table>\n";
371
372
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save\" />\n";
373
            echo '<input type="hidden" name="mode" value="', htmlspecialchars($mode), "\" />\n";
374
            echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['subject']), "\" />\n";
375
            if (isset($_REQUEST[$_REQUEST['subject'].'_oid'])) {
376
                echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject'].'_oid'),
377
                '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject'].'_oid']), "\" />\n";
378
            }
379
380
            echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject']),
381
            '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject']]), "\" />\n";
382
            if ('column' == $_REQUEST['subject']) {
383
                echo '<input type="hidden" name="table" value="',
384
                htmlspecialchars($_REQUEST['table']), "\" />\n";
385
            }
386
387
            echo $this->misc->form;
388
            if ('grant' == $mode) {
389
                echo "<input type=\"submit\" name=\"grant\" value=\"{$lang['strgrant']}\" />\n";
390
            } elseif ('revoke' == $mode) {
391
                echo "<input type=\"submit\" name=\"revoke\" value=\"{$lang['strrevoke']}\" />\n";
392
            }
393
394
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>";
395
            echo "</form>\n";
396
        } else {
397
            // Determine whether object should be ref'd by name or oid.
398
            if (isset($_REQUEST[$_REQUEST['subject'].'_oid'])) {
399
                $object = $_REQUEST[$_REQUEST['subject'].'_oid'];
400
            } else {
401
                $object = $_REQUEST[$_REQUEST['subject']];
402
            }
403
404
            if (isset($_REQUEST['table'])) {
405
                $table = $_REQUEST['table'];
406
            } else {
407
                $table = null;
408
            }
409
410
            $status = $data->setPrivileges(
411
                ('grant' == $mode) ? 'GRANT' : 'REVOKE',
412
                $_REQUEST['subject'],
413
                $object,
414
                isset($_REQUEST['public']),
415
                $_REQUEST['username'],
416
                $_REQUEST['groupname'],
417
                array_keys($_REQUEST['privilege']),
418
                isset($_REQUEST['grantoption']),
419
                isset($_REQUEST['cascade']),
420
                $table
421
            );
422
423
            if (0 == $status) {
424
                $this->doDefault($lang['strgranted']);
425
            } elseif ($status == -3 || $status == -4) {
426
                $this->doAlter(true, $_REQUEST['mode'], $lang['strgrantbad']);
427
            } else {
428
                $this->doAlter(true, $_REQUEST['mode'], $lang['strgrantfailed']);
429
            }
430
        }
431
    }
432
}
433