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

src/controllers/GroupsController.php (6 issues)

1
<?php
2
0 ignored issues
show
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
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class GroupsController extends BaseController
15
{
16
    public $controller_name = 'GroupsController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $this->printHeader($lang['strgroups']);
24
        $this->printBody();
25
26
        switch ($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
            default:
80
                $this->doDefault();
81
82
                break;
83
        }
84
85
        $this->printFooter();
86
    }
87
88
    /**
89
     * Show default list of groups in the database.
90
     *
91
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
92
     */
93
    public function doDefault($msg = '')
94
    {
95
        $conf = $this->conf;
96
97
        $lang = $this->lang;
98
        $data = $this->misc->getDatabaseAccessor();
99
100
        $this->printTrail('server');
101
        $this->printTabs('server', 'groups');
102
        $this->printMsg($msg);
103
104
        $groups = $data->getGroups();
105
106
        $columns = [
107
            'group' => [
108
                'title' => $lang['strgroup'],
109
                'field' => Decorator::field('groname'),
110
                'url'   => "groups.php?action=properties&amp;{$this->misc->href}&amp;",
111
                'vars'  => ['group' => 'groname'],
112
            ],
113
            'actions' => [
114
                'title' => $lang['stractions'],
115
            ],
116
        ];
117
118
        $actions = [
119
            'drop' => [
120
                'content' => $lang['strdrop'],
121
                'attr'    => [
122
                    'href' => [
123
                        'url'     => 'groups.php',
124
                        'urlvars' => [
125
                            'action' => 'confirm_drop',
126
                            'group'  => Decorator::field('groname'),
127
                        ],
128
                    ],
129
                ],
130
            ],
131
        ];
132
133
        echo $this->printTable($groups, $columns, $actions, 'groups-properties', $lang['strnogroups']);
134
135
        $this->printNavLinks(['create' => [
136
            'attr' => [
137
                'href' => [
138
                    'url'     => 'groups.php',
139
                    'urlvars' => [
140
                        'action' => 'create',
141
                        'server' => $_REQUEST['server'],
142
                    ],
143
                ],
144
            ],
145
            'content' => $lang['strcreategroup'],
146
        ]], 'groups-groups', get_defined_vars());
147
    }
148
149
    /**
150
     * Add user to a group.
151
     */
152
    public function doAddMember()
153
    {
154
        $conf = $this->conf;
155
156
        $lang = $this->lang;
157
        $data = $this->misc->getDatabaseAccessor();
158
159
        $status = $data->addGroupMember($_REQUEST['group'], $_REQUEST['user']);
160
        if (0 == $status) {
161
            $this->doProperties($lang['strmemberadded']);
162
        } else {
163
            $this->doProperties($lang['strmemberaddedbad']);
164
        }
165
    }
166
167
    /**
168
     * Show confirmation of drop user from group and perform actual drop.
169
     *
170
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
171
     */
172
    public function doDropMember($confirm)
173
    {
174
        $conf = $this->conf;
175
176
        $lang = $this->lang;
177
        $data = $this->misc->getDatabaseAccessor();
178
179
        if ($confirm) {
180
            $this->printTrail('group');
181
            $this->printTitle($lang['strdropmember'], 'pg.group.alter');
182
183
            echo '<p>', sprintf($lang['strconfdropmember'], $this->misc->printVal($_REQUEST['user']), $this->misc->printVal($_REQUEST['group'])), "</p>\n";
184
185
            echo '<form action="'.\SUBFOLDER."/src/views/groups.php\" method=\"post\">\n";
186
            echo $this->misc->form;
187
            echo "<input type=\"hidden\" name=\"action\" value=\"drop_member\" />\n";
188
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
189
            echo '<input type="hidden" name="user" value="', htmlspecialchars($_REQUEST['user']), "\" />\n";
190
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
191
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
192
            echo "</form>\n";
193
        } else {
194
            $status = $data->dropGroupMember($_REQUEST['group'], $_REQUEST['user']);
195
            if (0 == $status) {
196
                $this->doProperties($lang['strmemberdropped']);
197
            } else {
198
                $this->doDropMember(true, $lang['strmemberdroppedbad']);
199
            }
200
        }
201
    }
202
203
    /**
204
     * Show read only properties for a group.
205
     *
206
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
207
     */
208
    public function doProperties($msg = '')
209
    {
210
        $conf = $this->conf;
211
212
        $lang = $this->lang;
213
        $data = $this->misc->getDatabaseAccessor();
214
215
        if (!isset($_POST['user'])) {
216
            $_POST['user'] = '';
217
        }
218
219
        $this->printTrail('group');
220
        $this->printTitle($lang['strproperties'], 'pg.group');
221
        $this->printMsg($msg);
222
223
        $groupdata = $data->getGroup($_REQUEST['group']);
224
        $users     = $data->getUsers();
225
226
        if ($groupdata->recordCount() > 0) {
227
            $columns = [
228
                'members' => [
229
                    'title' => $lang['strmembers'],
230
                    'field' => Decorator::field('usename'),
231
                ],
232
                'actions' => [
233
                    'title' => $lang['stractions'],
234
                ],
235
            ];
236
237
            $actions = [
238
                'drop' => [
239
                    'content' => $lang['strdrop'],
240
                    'attr'    => [
241
                        'href' => [
242
                            'url'     => 'groups.php',
243
                            'urlvars' => [
244
                                'action' => 'confirm_drop_member',
245
                                'group'  => $_REQUEST['group'],
246
                                'user'   => Decorator::field('usename'),
247
                            ],
248
                        ],
249
                    ],
250
                ],
251
            ];
252
253
            echo $this->printTable($groupdata, $columns, $actions, 'groups-members', $lang['strnousers']);
254
        }
255
256
        // Display form for adding a user to the group
257
        echo '<form action="'.\SUBFOLDER."/src/views/groups.php\" method=\"post\">\n";
258
        echo '<select name="user">';
259
        while (!$users->EOF) {
260
            $uname = $this->misc->printVal($users->fields['usename']);
261
            echo "<option value=\"{$uname}\"",
262
            ($uname == $_POST['user']) ? ' selected="selected"' : '', ">{$uname}</option>\n";
263
            $users->moveNext();
264
        }
265
        echo "</select>\n";
266
        echo "<input type=\"submit\" value=\"{$lang['straddmember']}\" />\n";
267
        echo $this->misc->form;
268
        echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
269
        echo "<input type=\"hidden\" name=\"action\" value=\"add_member\" />\n";
270
        echo "</form>\n";
271
272
        $this->printNavLinks(['showall' => [
273
            'attr' => [
274
                'href' => [
275
                    'url'     => 'groups.php',
276
                    'urlvars' => [
277
                        'server' => $_REQUEST['server'],
278
                    ],
279
                ],
280
            ],
281
            'content' => $lang['strshowallgroups'],
282
        ]], 'groups-properties', get_defined_vars());
283
    }
284
285
    /**
286
     * Show confirmation of drop and perform actual drop.
287
     *
288
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
289
     */
290
    public function doDrop($confirm)
291
    {
292
        $conf = $this->conf;
293
294
        $lang = $this->lang;
295
        $data = $this->misc->getDatabaseAccessor();
296
297
        if ($confirm) {
298
            $this->printTrail('group');
299
            $this->printTitle($lang['strdrop'], 'pg.group.drop');
300
301
            echo '<p>', sprintf($lang['strconfdropgroup'], $this->misc->printVal($_REQUEST['group'])), "</p>\n";
302
303
            echo '<form action="'.\SUBFOLDER."/src/views/groups.php\" method=\"post\">\n";
304
            echo $this->misc->form;
305
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
306
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
307
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
308
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
309
            echo "</form>\n";
310
        } else {
311
            $status = $data->dropGroup($_REQUEST['group']);
312
            if (0 == $status) {
313
                $this->doDefault($lang['strgroupdropped']);
314
            } else {
315
                $this->doDefault($lang['strgroupdroppedbad']);
316
            }
317
        }
318
    }
319
320
    /**
321
     * Displays a screen where they can enter a new group.
322
     *
323
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
324
     */
325
    public function doCreate($msg = '')
326
    {
327
        $conf = $this->conf;
328
329
        $lang = $this->lang;
330
        $data = $this->misc->getDatabaseAccessor();
331
        if (!isset($_POST['name'])) {
332
            $_POST['name'] = '';
333
        }
334
335
        if (!isset($_POST['members'])) {
336
            $_POST['members'] = [];
337
        }
338
339
        // Fetch a list of all users in the cluster
340
        $users = $data->getUsers();
341
342
        $this->printTrail('server');
343
        $this->printTitle($lang['strcreategroup'], 'pg.group.create');
344
        $this->printMsg($msg);
345
346
        echo "<form action=\"\" method=\"post\">\n";
347
        echo $this->misc->form;
348
        echo "<table>\n";
349
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
350
        echo "\t\t<td class=\"data\"><input size=\"32\" maxlength=\"{$data->_maxNameLen}\" name=\"name\" value=\"", htmlspecialchars($_POST['name']), "\" /></td>\n\t</tr>\n";
351
        if ($users->recordCount() > 0) {
352
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strmembers']}</th>\n";
353
354
            echo "\t\t<td class=\"data\">\n";
355
            echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(40, $users->recordCount()), "\">\n";
356
            while (!$users->EOF) {
357
                $username = $users->fields['usename'];
358
                echo "\t\t\t\t<option value=\"{$username}\"",
359
                (in_array($username, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($username), "</option>\n";
360
                $users->moveNext();
361
            }
362
            echo "\t\t\t</select>\n";
363
            echo "\t\t</td>\n\t</tr>\n";
364
        }
365
        echo "</table>\n";
366
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
367
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
368
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
369
        echo "</form>\n";
370
    }
371
372
    /**
373
     * Actually creates the new group in the database.
374
     */
375
    public function doSaveCreate()
376
    {
377
        $conf = $this->conf;
378
379
        $lang = $this->lang;
380
        $data = $this->misc->getDatabaseAccessor();
381
382
        if (!isset($_POST['members'])) {
383
            $_POST['members'] = [];
384
        }
385
386
        // Check form vars
387
        if ('' == trim($_POST['name'])) {
388
            $this->doCreate($lang['strgroupneedsname']);
389
        } else {
390
            $status = $data->createGroup($_POST['name'], $_POST['members']);
391
            if (0 == $status) {
392
                $this->doDefault($lang['strgroupcreated']);
393
            } else {
394
                $this->doCreate($lang['strgroupcreatedbad']);
395
            }
396
        }
397
    }
398
}
399