Issues (217)

src/controllers/GroupsController.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class GroupsController extends BaseController
15
{
16
    public $controller_title = 'strgroups';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render(): void
22
    {
23
        $this->printHeader();
24
        $this->printBody();
25
26
        switch ($this->action) {
27
            case 'add_member':
28
                $this->doAddMember();
29
30
                break;
31
            case 'drop_member':
32
                if (isset($_REQUEST['drop'])) {
33
                    $this->doDropMember(false);
34
                } else {
35
                    $this->doProperties();
36
                }
37
38
                break;
39
            case 'confirm_drop_member':
40
                $this->doDropMember(true);
41
42
                break;
43
            case 'save_create':
44
                if (isset($_REQUEST['cancel'])) {
45
                    $this->doDefault();
46
                } else {
47
                    $this->doSaveCreate();
48
                }
49
50
                break;
51
            case 'create':
52
                $this->doCreate();
53
54
                break;
55
            case 'drop':
56
                if (isset($_REQUEST['drop'])) {
57
                    $this->doDrop(false);
58
                } else {
59
                    $this->doDefault();
60
                }
61
62
                break;
63
            case 'confirm_drop':
64
                $this->doDrop(true);
65
66
                break;
67
            /*case 'save_edit':
68
            $this->doSaveEdit();
69
70
            break;
71
            case 'edit':
72
            $this->doEdit();
73
74
            break;*/
75
            case 'properties':
76
                $this->doProperties();
77
78
                break;
79
80
            default:
81
                $this->doDefault();
82
83
                break;
84
        }
85
86
        $this->printFooter();
87
    }
88
89
    /**
90
     * Show default list of groups in the database.
91
     *
92
     * @param mixed $msg
93
     */
94
    public function doDefault($msg = ''): void
95
    {
96
        $data = $this->misc->getDatabaseAccessor();
97
98
        $this->printTrail('server');
99
        $this->printTabs('server', 'groups');
100
        $this->printMsg($msg);
101
102
        $groups = $data->getGroups();
103
104
        $columns = [
105
            'group' => [
106
                'title' => $this->lang['strgroup'],
107
                'field' => Decorator::field('groname'),
108
                'url' => "groups?action=properties&amp;{$this->misc->href}&amp;",
109
                'vars' => ['group' => 'groname'],
110
            ],
111
            'actions' => [
112
                'title' => $this->lang['stractions'],
113
            ],
114
        ];
115
116
        $actions = [
117
            'drop' => [
118
                'content' => $this->lang['strdrop'],
119
                'attr' => [
120
                    'href' => [
121
                        'url' => 'groups',
122
                        'urlvars' => [
123
                            'action' => 'confirm_drop',
124
                            'group' => Decorator::field('groname'),
125
                        ],
126
                    ],
127
                ],
128
            ],
129
        ];
130
131
        echo $this->printTable($groups, $columns, $actions, 'groups-properties', $this->lang['strnogroups']);
0 ignored issues
show
It seems like $groups can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
        echo $this->printTable(/** @scrutinizer ignore-type */ $groups, $columns, $actions, 'groups-properties', $this->lang['strnogroups']);
Loading history...
132
133
        $this->printNavLinks(['create' => [
134
            'attr' => [
135
                'href' => [
136
                    'url' => 'groups',
137
                    'urlvars' => [
138
                        'action' => 'create',
139
                        'server' => $_REQUEST['server'],
140
                    ],
141
                ],
142
            ],
143
            'content' => $this->lang['strcreategroup'],
144
        ]], 'groups-groups', \get_defined_vars());
145
    }
146
147
    /**
148
     * Add user to a group.
149
     */
150
    public function doAddMember(): void
151
    {
152
        $data = $this->misc->getDatabaseAccessor();
153
154
        $status = $data->addGroupMember($_REQUEST['group'], $_REQUEST['user']);
155
156
        if (0 === $status) {
157
            $this->doProperties($this->lang['strmemberadded']);
158
        } else {
159
            $this->doProperties($this->lang['strmemberaddedbad']);
160
        }
161
    }
162
163
    /**
164
     * Show confirmation of drop user from group and perform actual drop.
165
     *
166
     * @param mixed $confirm
167
     * @param mixed $msg
168
     */
169
    public function doDropMember($confirm, $msg = ''): void
170
    {
171
        $data = $this->misc->getDatabaseAccessor();
172
173
        if ($msg) {
174
            $this->printMsg($msg);
175
        }
176
177
        if ($confirm) {
178
            $this->printTrail('group');
179
            $this->printTitle($this->lang['strdropmember'], 'pg.group.alter');
180
181
            echo '<p>', \sprintf($this->lang['strconfdropmember'], $this->misc->printVal($_REQUEST['user']), $this->misc->printVal($_REQUEST['group'])), '</p>' . \PHP_EOL;
182
183
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/groups" method="post">' . \PHP_EOL;
184
            echo $this->view->form;
185
            echo '<input type="hidden" name="action" value="drop_member" />' . \PHP_EOL;
186
            echo '<input type="hidden" name="group" value="', \htmlspecialchars($_REQUEST['group']), '" />' . \PHP_EOL;
187
            echo '<input type="hidden" name="user" value="', \htmlspecialchars($_REQUEST['user']), '" />' . \PHP_EOL;
188
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
189
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
190
            echo '</form>' . \PHP_EOL;
191
        } else {
192
            $status = $data->dropGroupMember($_REQUEST['group'], $_REQUEST['user']);
193
194
            if (0 === $status) {
195
                $this->doProperties($this->lang['strmemberdropped']);
196
            } else {
197
                $this->doDropMember(true, $this->lang['strmemberdroppedbad']);
198
            }
199
        }
200
    }
201
202
    /**
203
     * Show read only properties for a group.
204
     *
205
     * @param mixed $msg
206
     */
207
    public function doProperties($msg = ''): void
208
    {
209
        $data = $this->misc->getDatabaseAccessor();
210
211
        $this->coalesceArr($_POST, 'user', '');
212
213
        $this->printTrail('group');
214
        $this->printTitle($this->lang['strproperties'], 'pg.group');
215
        $this->printMsg($msg);
216
217
        $groupdata = $data->getGroup($_REQUEST['group']);
218
        $users = $data->getUsers();
219
220
        if (0 < $groupdata->recordCount()) {
221
            $columns = [
222
                'members' => [
223
                    'title' => $this->lang['strmembers'],
224
                    'field' => Decorator::field('usename'),
225
                ],
226
                'actions' => [
227
                    'title' => $this->lang['stractions'],
228
                ],
229
            ];
230
231
            $actions = [
232
                'drop' => [
233
                    'content' => $this->lang['strdrop'],
234
                    'attr' => [
235
                        'href' => [
236
                            'url' => 'groups',
237
                            'urlvars' => [
238
                                'action' => 'confirm_drop_member',
239
                                'group' => $_REQUEST['group'],
240
                                'user' => Decorator::field('usename'),
241
                            ],
242
                        ],
243
                    ],
244
                ],
245
            ];
246
247
            echo $this->printTable($groupdata, $columns, $actions, 'groups-members', $this->lang['strnousers']);
0 ignored issues
show
It seems like $groupdata can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

247
            echo $this->printTable(/** @scrutinizer ignore-type */ $groupdata, $columns, $actions, 'groups-members', $this->lang['strnousers']);
Loading history...
248
        }
249
250
        // Display form for adding a user to the group
251
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/groups" method="post">' . \PHP_EOL;
252
        echo '<select name="user">';
253
254
        while (!$users->EOF) {
255
            $uname = $this->misc->printVal($users->fields['usename']);
256
            echo "<option value=\"{$uname}\"",
257
            ($uname === $_POST['user']) ? ' selected="selected"' : '', ">{$uname}</option>" . \PHP_EOL;
258
            $users->moveNext();
259
        }
260
        echo '</select>' . \PHP_EOL;
261
        echo "<input type=\"submit\" value=\"{$this->lang['straddmember']}\" />" . \PHP_EOL;
262
        echo $this->view->form;
263
        echo '<input type="hidden" name="group" value="', \htmlspecialchars($_REQUEST['group']), '" />' . \PHP_EOL;
264
        echo '<input type="hidden" name="action" value="add_member" />' . \PHP_EOL;
265
        echo '</form>' . \PHP_EOL;
266
267
        $this->printNavLinks(['showall' => [
268
            'attr' => [
269
                'href' => [
270
                    'url' => 'groups',
271
                    'urlvars' => [
272
                        'server' => $_REQUEST['server'],
273
                    ],
274
                ],
275
            ],
276
            'content' => $this->lang['strshowallgroups'],
277
        ]], 'groups-properties', \get_defined_vars());
278
    }
279
280
    /**
281
     * Show confirmation of drop and perform actual drop.
282
     *
283
     * @param mixed $confirm
284
     */
285
    public function doDrop($confirm): void
286
    {
287
        $data = $this->misc->getDatabaseAccessor();
288
289
        if ($confirm) {
290
            $this->printTrail('group');
291
            $this->printTitle($this->lang['strdrop'], 'pg.group.drop');
292
293
            echo '<p>', \sprintf($this->lang['strconfdropgroup'], $this->misc->printVal($_REQUEST['group'])), '</p>' . \PHP_EOL;
294
295
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/groups" method="post">' . \PHP_EOL;
296
            echo $this->view->form;
297
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
298
            echo '<input type="hidden" name="group" value="', \htmlspecialchars($_REQUEST['group']), '" />' . \PHP_EOL;
299
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
300
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
301
            echo '</form>' . \PHP_EOL;
302
        } else {
303
            $status = $data->dropGroup($_REQUEST['group']);
304
305
            if (0 === $status) {
306
                $this->doDefault($this->lang['strgroupdropped']);
307
            } else {
308
                $this->doDefault($this->lang['strgroupdroppedbad']);
309
            }
310
        }
311
    }
312
313
    /**
314
     * Displays a screen where they can enter a new group.
315
     *
316
     * @param mixed $msg
317
     */
318
    public function doCreate($msg = ''): void
319
    {
320
        $data = $this->misc->getDatabaseAccessor();
321
        $this->coalesceArr($_POST, 'name', '');
322
323
        $this->coalesceArr($_POST, 'members', []);
324
325
        // Fetch a list of all users in the cluster
326
        $users = $data->getUsers();
327
328
        $this->printTrail('server');
329
        $this->printTitle($this->lang['strcreategroup'], 'pg.group.create');
330
        $this->printMsg($msg);
331
332
        echo '<form action="" method="post">' . \PHP_EOL;
333
        echo $this->view->form;
334
        echo '<table>' . \PHP_EOL;
335
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
336
        echo "\t\t<td class=\"data\"><input size=\"32\" maxlength=\"{$data->_maxNameLen}\" name=\"name\" value=\"", \htmlspecialchars($_POST['name']), "\" /></td>\n\t</tr>" . \PHP_EOL;
337
338
        if (0 < $users->recordCount()) {
339
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>" . \PHP_EOL;
340
341
            echo "\t\t<td class=\"data\">" . \PHP_EOL;
342
            echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", \min(40, $users->recordCount()), '">' . \PHP_EOL;
343
344
            while (!$users->EOF) {
345
                $username = $users->fields['usename'];
346
                echo "\t\t\t\t<option value=\"{$username}\"",
347
                (\in_array($username, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($username), '</option>' . \PHP_EOL;
348
                $users->moveNext();
349
            }
350
            echo "\t\t\t</select>" . \PHP_EOL;
351
            echo "\t\t</td>\n\t</tr>" . \PHP_EOL;
352
        }
353
        echo '</table>' . \PHP_EOL;
354
        echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL;
355
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
356
        echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
357
        echo '</form>' . \PHP_EOL;
358
    }
359
360
    /**
361
     * Actually creates the new group in the database.
362
     */
363
    public function doSaveCreate(): void
364
    {
365
        $data = $this->misc->getDatabaseAccessor();
366
367
        $this->coalesceArr($_POST, 'members', []);
368
369
        // Check form vars
370
        if ('' === \trim($_POST['name'])) {
371
            $this->doCreate($this->lang['strgroupneedsname']);
372
        } else {
373
            $status = $data->createGroup($_POST['name'], $_POST['members']);
374
375
            if (0 === $status) {
376
                $this->doDefault($this->lang['strgroupcreated']);
377
            } else {
378
                $this->doCreate($this->lang['strgroupcreatedbad']);
379
            }
380
        }
381
    }
382
}
383