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

PrivilegesController::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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