Passed
Push — develop ( 51bd2c...501708 )
by Felipe
05:51
created

GroupsController   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 360
rs 8.3999
c 0
b 0
f 0
wmc 38

8 Methods

Rating   Name   Duplication   Size   Complexity  
C render() 0 65 14
A doDrop() 0 23 3
A doDefault() 0 51 1
B doDropMember() 0 24 3
B doCreate() 0 42 6
A doSaveCreate() 0 17 4
B doProperties() 0 72 5
A doAddMember() 0 9 2
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.40
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_name = 'GroupsController';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        $this->printHeader($this->lang['strgroups']);
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' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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());
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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
     */
168
    public function doDropMember($confirm)
169
    {
170
        $data = $this->misc->getDatabaseAccessor();
171
172
        if ($confirm) {
173
            $this->printTrail('group');
174
            $this->printTitle($this->lang['strdropmember'], 'pg.group.alter');
175
176
            echo '<p>', sprintf($this->lang['strconfdropmember'], $this->misc->printVal($_REQUEST['user']), $this->misc->printVal($_REQUEST['group'])), "</p>\n";
177
178
            echo '<form action="'.\SUBFOLDER."/src/views/groups\" method=\"post\">\n";
179
            echo $this->misc->form;
180
            echo "<input type=\"hidden\" name=\"action\" value=\"drop_member\" />\n";
181
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
182
            echo '<input type="hidden" name="user" value="', htmlspecialchars($_REQUEST['user']), "\" />\n";
183
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
184
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
185
            echo "</form>\n";
186
        } else {
187
            $status = $data->dropGroupMember($_REQUEST['group'], $_REQUEST['user']);
188
            if (0 == $status) {
189
                $this->doProperties($this->lang['strmemberdropped']);
190
            } else {
191
                $this->doDropMember(true, $this->lang['strmemberdroppedbad']);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\Controller\Gr...troller::doDropMember() has too many arguments starting with $this->lang['strmemberdroppedbad']. ( Ignorable by Annotation )

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

191
                $this->/** @scrutinizer ignore-call */ 
192
                       doDropMember(true, $this->lang['strmemberdroppedbad']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
192
            }
193
        }
194
    }
195
196
    /**
197
     * Show read only properties for a group.
198
     *
199
     * @param mixed $msg
200
     */
201
    public function doProperties($msg = '')
202
    {
203
        $data = $this->misc->getDatabaseAccessor();
204
205
        if (!isset($_POST['user'])) {
206
            $_POST['user'] = '';
207
        }
208
209
        $this->printTrail('group');
210
        $this->printTitle($this->lang['strproperties'], 'pg.group');
211
        $this->printMsg($msg);
212
213
        $groupdata = $data->getGroup($_REQUEST['group']);
214
        $users     = $data->getUsers();
215
216
        if ($groupdata->recordCount() > 0) {
217
            $columns = [
218
                'members' => [
219
                    'title' => $this->lang['strmembers'],
220
                    'field' => Decorator::field('usename'),
221
                ],
222
                'actions' => [
223
                    'title' => $this->lang['stractions'],
224
                ],
225
            ];
226
227
            $actions = [
228
                'drop' => [
229
                    'content' => $this->lang['strdrop'],
230
                    'attr'    => [
231
                        'href' => [
232
                            'url'     => 'groups',
233
                            'urlvars' => [
234
                                'action' => 'confirm_drop_member',
235
                                'group'  => $_REQUEST['group'],
236
                                'user'   => Decorator::field('usename'),
237
                            ],
238
                        ],
239
                    ],
240
                ],
241
            ];
242
243
            echo $this->printTable($groupdata, $columns, $actions, 'groups-members', $this->lang['strnousers']);
244
        }
245
246
        // Display form for adding a user to the group
247
        echo '<form action="'.\SUBFOLDER."/src/views/groups\" method=\"post\">\n";
248
        echo '<select name="user">';
249
        while (!$users->EOF) {
250
            $uname = $this->misc->printVal($users->fields['usename']);
251
            echo "<option value=\"{$uname}\"",
252
            ($uname == $_POST['user']) ? ' selected="selected"' : '', ">{$uname}</option>\n";
253
            $users->moveNext();
254
        }
255
        echo "</select>\n";
256
        echo "<input type=\"submit\" value=\"{$this->lang['straddmember']}\" />\n";
257
        echo $this->misc->form;
258
        echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
259
        echo "<input type=\"hidden\" name=\"action\" value=\"add_member\" />\n";
260
        echo "</form>\n";
261
262
        $this->printNavLinks(['showall' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
263
            'attr'    => [
264
                'href' => [
265
                    'url'     => 'groups',
266
                    'urlvars' => [
267
                        'server' => $_REQUEST['server'],
268
                    ],
269
                ],
270
            ],
271
            'content' => $this->lang['strshowallgroups'],
272
        ]], 'groups-properties', get_defined_vars());
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
273
    }
274
275
    /**
276
     * Show confirmation of drop and perform actual drop.
277
     *
278
     * @param mixed $confirm
279
     */
280
    public function doDrop($confirm)
281
    {
282
        $data = $this->misc->getDatabaseAccessor();
283
284
        if ($confirm) {
285
            $this->printTrail('group');
286
            $this->printTitle($this->lang['strdrop'], 'pg.group.drop');
287
288
            echo '<p>', sprintf($this->lang['strconfdropgroup'], $this->misc->printVal($_REQUEST['group'])), "</p>\n";
289
290
            echo '<form action="'.\SUBFOLDER."/src/views/groups\" method=\"post\">\n";
291
            echo $this->misc->form;
292
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
293
            echo '<input type="hidden" name="group" value="', htmlspecialchars($_REQUEST['group']), "\" />\n";
294
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
295
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
296
            echo "</form>\n";
297
        } else {
298
            $status = $data->dropGroup($_REQUEST['group']);
299
            if (0 == $status) {
300
                $this->doDefault($this->lang['strgroupdropped']);
301
            } else {
302
                $this->doDefault($this->lang['strgroupdroppedbad']);
303
            }
304
        }
305
    }
306
307
    /**
308
     * Displays a screen where they can enter a new group.
309
     *
310
     * @param mixed $msg
311
     */
312
    public function doCreate($msg = '')
313
    {
314
        $data = $this->misc->getDatabaseAccessor();
315
        if (!isset($_POST['name'])) {
316
            $_POST['name'] = '';
317
        }
318
319
        if (!isset($_POST['members'])) {
320
            $_POST['members'] = [];
321
        }
322
323
        // Fetch a list of all users in the cluster
324
        $users = $data->getUsers();
325
326
        $this->printTrail('server');
327
        $this->printTitle($this->lang['strcreategroup'], 'pg.group.create');
328
        $this->printMsg($msg);
329
330
        echo "<form action=\"\" method=\"post\">\n";
331
        echo $this->misc->form;
332
        echo "<table>\n";
333
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n";
334
        echo "\t\t<td class=\"data\"><input size=\"32\" maxlength=\"{$data->_maxNameLen}\" name=\"name\" value=\"", htmlspecialchars($_POST['name']), "\" /></td>\n\t</tr>\n";
335
        if ($users->recordCount() > 0) {
336
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strmembers']}</th>\n";
337
338
            echo "\t\t<td class=\"data\">\n";
339
            echo "\t\t\t<select name=\"members[]\" multiple=\"multiple\" size=\"", min(40, $users->recordCount()), "\">\n";
340
            while (!$users->EOF) {
341
                $username = $users->fields['usename'];
342
                echo "\t\t\t\t<option value=\"{$username}\"",
343
                (in_array($username, $_POST['members'], true) ? ' selected="selected"' : ''), '>', $this->misc->printVal($username), "</option>\n";
344
                $users->moveNext();
345
            }
346
            echo "\t\t\t</select>\n";
347
            echo "\t\t</td>\n\t</tr>\n";
348
        }
349
        echo "</table>\n";
350
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
351
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
352
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
353
        echo "</form>\n";
354
    }
355
356
    /**
357
     * Actually creates the new group in the database.
358
     */
359
    public function doSaveCreate()
360
    {
361
        $data = $this->misc->getDatabaseAccessor();
362
363
        if (!isset($_POST['members'])) {
364
            $_POST['members'] = [];
365
        }
366
367
        // Check form vars
368
        if ('' == trim($_POST['name'])) {
369
            $this->doCreate($this->lang['strgroupneedsname']);
370
        } else {
371
            $status = $data->createGroup($_POST['name'], $_POST['members']);
372
            if (0 == $status) {
373
                $this->doDefault($this->lang['strgroupcreated']);
374
            } else {
375
                $this->doCreate($this->lang['strgroupcreatedbad']);
376
            }
377
        }
378
    }
379
}
380