Passed
Push — develop ( c6c0b1...90e9a5 )
by Felipe
10:30 queued 02:43
created

GroupsController::doAddMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class GroupsController extends BaseController
17
{
18
    public $controller_title = 'strgroups';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        $this->printHeader();
26
        $this->printBody();
27
28
        switch ($this->action) {
29
            case 'add_member':
30
                $this->doAddMember();
31
32
                break;
33
            case 'drop_member':
34
                if (isset($_REQUEST['drop'])) {
35
                    $this->doDropMember(false);
36
                } else {
37
                    $this->doProperties();
38
                }
39
40
                break;
41
            case 'confirm_drop_member':
42
                $this->doDropMember(true);
43
44
                break;
45
            case 'save_create':
46
                if (isset($_REQUEST['cancel'])) {
47
                    $this->doDefault();
48
                } else {
49
                    $this->doSaveCreate();
50
                }
51
52
                break;
53
            case 'create':
54
                $this->doCreate();
55
56
                break;
57
            case 'drop':
58
                if (isset($_REQUEST['drop'])) {
59
                    $this->doDrop(false);
60
                } else {
61
                    $this->doDefault();
62
                }
63
64
                break;
65
            case 'confirm_drop':
66
                $this->doDrop(true);
67
68
                break;
69
            case 'save_edit':
70
                $this->doSaveEdit();
0 ignored issues
show
Bug introduced by
The method doSaveEdit() does not exist on PHPPgAdmin\Controller\GroupsController. ( Ignorable by Annotation )

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

70
                $this->/** @scrutinizer ignore-call */ 
71
                       doSaveEdit();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
72
                break;
73
            case 'edit':
74
                $this->doEdit();
0 ignored issues
show
Bug introduced by
The method doEdit() does not exist on PHPPgAdmin\Controller\GroupsController. ( Ignorable by Annotation )

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

74
                $this->/** @scrutinizer ignore-call */ 
75
                       doEdit();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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