Passed
Push — master ( 5397c5...a7725b )
by Felipe
05:40
created

PrivilegesController::printGrantLinks()   D

Complexity

Conditions 15
Paths 144

Size

Total Lines 113
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

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