Skip to content

Commit cc1733a

Browse files
Merge pull request #17563 from MauricioFauth/privileges-controllers-extraction
Extract Database and Table controllers from Server\PrivilegesController class
2 parents 6201c7f + d58057d commit cc1733a

18 files changed

Lines changed: 141 additions & 159 deletions

File tree

libraries/classes/Controllers/Database/PrivilegesController.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
namespace PhpMyAdmin\Controllers\Database;
99

10+
use PhpMyAdmin\CheckUserPrivileges;
1011
use PhpMyAdmin\Controllers\AbstractController;
1112
use PhpMyAdmin\DatabaseInterface;
13+
use PhpMyAdmin\Message;
1214
use PhpMyAdmin\ResponseRenderer;
1315
use PhpMyAdmin\Server\Privileges;
1416
use PhpMyAdmin\Template;
1517
use PhpMyAdmin\Util;
1618

19+
use function __;
1720
use function mb_strtolower;
21+
use function ob_get_clean;
22+
use function ob_start;
1823

1924
/**
2025
* Controller for database privileges
@@ -38,26 +43,70 @@ public function __construct(
3843
$this->dbi = $dbi;
3944
}
4045

41-
/**
42-
* @param string[] $params Request parameters
43-
* @psalm-param array{checkprivsdb: string} $params
44-
*/
45-
public function __invoke(array $params): string
46+
public function __invoke(): void
4647
{
4748
$GLOBALS['text_dir'] = $GLOBALS['text_dir'] ?? null;
49+
50+
$checkUserPrivileges = new CheckUserPrivileges($this->dbi);
51+
$checkUserPrivileges->getPrivileges();
52+
53+
$this->addScriptFiles(['server/privileges.js', 'vendor/zxcvbn-ts.js']);
54+
55+
/**
56+
* Checks if the user is allowed to do what they try to...
57+
*/
58+
$isGrantUser = $this->dbi->isGrantUser();
59+
$isCreateUser = $this->dbi->isCreateUser();
60+
61+
if (! $this->dbi->isSuperUser() && ! $isGrantUser && ! $isCreateUser) {
62+
$this->render('server/sub_page_header', [
63+
'type' => 'privileges',
64+
'is_image' => false,
65+
]);
66+
$this->response->addHTML(
67+
Message::error(__('No Privileges'))
68+
->getDisplay()
69+
);
70+
71+
return;
72+
}
73+
74+
if (! $isGrantUser && ! $isCreateUser) {
75+
$this->response->addHTML(Message::notice(
76+
__('You do not have the privileges to administrate the users!')
77+
)->getDisplay());
78+
}
79+
80+
// Gets the database structure
81+
$GLOBALS['sub_part'] = '_structure';
82+
ob_start();
83+
84+
[
85+
$GLOBALS['tables'],
86+
$GLOBALS['num_tables'],
87+
$GLOBALS['total_num_tables'],
88+
$GLOBALS['sub_part'],,,
89+
$GLOBALS['tooltip_truename'],
90+
$GLOBALS['tooltip_aliasname'],
91+
$GLOBALS['pos'],
92+
] = Util::getDbInfo($GLOBALS['db'], $GLOBALS['sub_part']);
93+
94+
$content = ob_get_clean();
95+
$this->response->addHTML($content . "\n");
96+
4897
$scriptName = Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabDatabase'], 'database');
4998

50-
$db = $params['checkprivsdb'];
99+
$db = $GLOBALS['db'];
51100
if ($this->dbi->getLowerCaseNames() === '1') {
52-
$db = mb_strtolower($params['checkprivsdb']);
101+
$db = mb_strtolower($GLOBALS['db']);
53102
}
54103

55104
$privileges = [];
56105
if ($this->dbi->isSuperUser()) {
57106
$privileges = $this->privileges->getAllPrivileges($db);
58107
}
59108

60-
return $this->template->render('database/privileges/index', [
109+
$this->render('database/privileges/index', [
61110
'is_superuser' => $this->dbi->isSuperUser(),
62111
'db' => $db,
63112
'database_url' => $scriptName,
@@ -66,5 +115,6 @@ public function __invoke(array $params): string
66115
'is_grantuser' => $this->dbi->isGrantUser(),
67116
'privileges' => $privileges,
68117
]);
118+
$this->render('export_modal');
69119
}
70120
}

libraries/classes/Controllers/Server/PrivilegesController.php

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpMyAdmin\ConfigStorage\Relation;
99
use PhpMyAdmin\ConfigStorage\RelationCleanup;
1010
use PhpMyAdmin\Controllers\AbstractController;
11-
use PhpMyAdmin\Controllers\Database\PrivilegesController as DatabaseController;
12-
use PhpMyAdmin\Controllers\Table\PrivilegesController as TableController;
1311
use PhpMyAdmin\Core;
1412
use PhpMyAdmin\DatabaseInterface;
1513
use PhpMyAdmin\Html\Generator;
@@ -26,8 +24,6 @@
2624
use function implode;
2725
use function is_array;
2826
use function is_string;
29-
use function ob_get_clean;
30-
use function ob_start;
3127
use function str_replace;
3228
use function urlencode;
3329

@@ -102,15 +98,7 @@ public function __invoke(): void
10298
new Plugins($this->dbi)
10399
);
104100

105-
$databaseController = new DatabaseController($this->response, $this->template, $serverPrivileges, $this->dbi);
106-
107-
$tableController = new TableController($this->response, $this->template, $serverPrivileges, $this->dbi);
108-
109-
if (
110-
(isset($_GET['viewing_mode'])
111-
&& $_GET['viewing_mode'] === 'server')
112-
&& $relationParameters->configurableMenusFeature !== null
113-
) {
101+
if ($relationParameters->configurableMenusFeature !== null) {
114102
$this->response->addHTML('<div class="container-fluid">');
115103
$this->render('server/privileges/subnav', [
116104
'active' => 'privileges',
@@ -371,26 +359,7 @@ public function __invoke(): void
371359
/**
372360
* Displays the links
373361
*/
374-
if (isset($_GET['viewing_mode']) && $_GET['viewing_mode'] === 'db') {
375-
$GLOBALS['db'] = $_REQUEST['db'] = $_GET['checkprivsdb'];
376-
377-
// Gets the database structure
378-
$GLOBALS['sub_part'] = '_structure';
379-
ob_start();
380-
381-
[
382-
$GLOBALS['tables'],
383-
$GLOBALS['num_tables'],
384-
$GLOBALS['total_num_tables'],
385-
$GLOBALS['sub_part'],,,
386-
$GLOBALS['tooltip_truename'],
387-
$GLOBALS['tooltip_aliasname'],
388-
$GLOBALS['pos'],
389-
] = Util::getDbInfo($GLOBALS['db'], $GLOBALS['sub_part']);
390-
391-
$content = ob_get_clean();
392-
$this->response->addHTML($content . "\n");
393-
} elseif (! empty($GLOBALS['message'])) {
362+
if (! empty($GLOBALS['message'])) {
394363
$this->response->addHTML(Generator::getMessage($GLOBALS['message']));
395364
unset($GLOBALS['message']);
396365
}
@@ -420,22 +389,6 @@ public function __invoke(): void
420389
$this->response->addHTML($serverPrivileges->getHtmlForAddUser(
421390
Util::escapeMysqlWildcards(is_string($GLOBALS['dbname']) ? $GLOBALS['dbname'] : '')
422391
));
423-
} elseif (isset($_GET['checkprivsdb']) && is_string($_GET['checkprivsdb'])) {
424-
if (isset($_GET['checkprivstable']) && is_string($_GET['checkprivstable'])) {
425-
$this->response->addHTML($tableController([
426-
'checkprivsdb' => $_GET['checkprivsdb'],
427-
'checkprivstable' => $_GET['checkprivstable'],
428-
]));
429-
$this->render('export_modal');
430-
} elseif ($this->response->isAjax() === true && empty($_REQUEST['ajax_page_request'])) {
431-
$GLOBALS['message'] = Message::success(__('User has been added.'));
432-
$this->response->addJSON('message', $GLOBALS['message']);
433-
434-
return;
435-
} else {
436-
$this->response->addHTML($databaseController(['checkprivsdb' => $_GET['checkprivsdb']]));
437-
$this->render('export_modal');
438-
}
439392
} else {
440393
if (isset($GLOBALS['dbname']) && ! is_array($GLOBALS['dbname'])) {
441394
$GLOBALS['url_dbname'] = urlencode(
@@ -488,11 +441,7 @@ public function __invoke(): void
488441
}
489442
}
490443

491-
if (
492-
! isset($_GET['viewing_mode'])
493-
|| $_GET['viewing_mode'] !== 'server'
494-
|| $relationParameters->configurableMenusFeature === null
495-
) {
444+
if ($relationParameters->configurableMenusFeature === null) {
496445
return;
497446
}
498447

libraries/classes/Controllers/Table/PrivilegesController.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
namespace PhpMyAdmin\Controllers\Table;
99

10+
use PhpMyAdmin\CheckUserPrivileges;
1011
use PhpMyAdmin\Controllers\AbstractController;
1112
use PhpMyAdmin\DatabaseInterface;
13+
use PhpMyAdmin\Message;
1214
use PhpMyAdmin\ResponseRenderer;
1315
use PhpMyAdmin\Server\Privileges;
1416
use PhpMyAdmin\Template;
1517
use PhpMyAdmin\Util;
1618

19+
use function __;
1720
use function mb_strtolower;
1821

1922
/**
@@ -38,28 +41,55 @@ public function __construct(
3841
$this->dbi = $dbi;
3942
}
4043

41-
/**
42-
* @param string[] $params Request parameters
43-
* @psalm-param array{checkprivsdb: string, checkprivstable: string} $params
44-
*/
45-
public function __invoke(array $params): string
44+
public function __invoke(): void
4645
{
4746
$GLOBALS['text_dir'] = $GLOBALS['text_dir'] ?? null;
47+
48+
$checkUserPrivileges = new CheckUserPrivileges($this->dbi);
49+
$checkUserPrivileges->getPrivileges();
50+
51+
$this->addScriptFiles(['server/privileges.js', 'vendor/zxcvbn-ts.js']);
52+
53+
/**
54+
* Checks if the user is allowed to do what they try to...
55+
*/
56+
$isGrantUser = $this->dbi->isGrantUser();
57+
$isCreateUser = $this->dbi->isCreateUser();
58+
59+
if (! $this->dbi->isSuperUser() && ! $isGrantUser && ! $isCreateUser) {
60+
$this->render('server/sub_page_header', [
61+
'type' => 'privileges',
62+
'is_image' => false,
63+
]);
64+
$this->response->addHTML(
65+
Message::error(__('No Privileges'))
66+
->getDisplay()
67+
);
68+
69+
return;
70+
}
71+
72+
if (! $isGrantUser && ! $isCreateUser) {
73+
$this->response->addHTML(Message::notice(
74+
__('You do not have the privileges to administrate the users!')
75+
)->getDisplay());
76+
}
77+
4878
$scriptName = Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabTable'], 'table');
4979

50-
$db = $params['checkprivsdb'];
51-
$table = $params['checkprivstable'];
80+
$db = $GLOBALS['db'];
81+
$table = $GLOBALS['table'];
5282
if ($this->dbi->getLowerCaseNames() === '1') {
53-
$db = mb_strtolower($params['checkprivsdb']);
54-
$table = mb_strtolower($params['checkprivstable']);
83+
$db = mb_strtolower($GLOBALS['db']);
84+
$table = mb_strtolower($GLOBALS['table']);
5585
}
5686

5787
$privileges = [];
5888
if ($this->dbi->isSuperUser()) {
5989
$privileges = $this->privileges->getAllPrivileges($db, $table);
6090
}
6191

62-
return $this->template->render('table/privileges/index', [
92+
$this->render('table/privileges/index', [
6393
'db' => $db,
6494
'table' => $table,
6595
'is_superuser' => $this->dbi->isSuperUser(),
@@ -69,5 +99,6 @@ public function __invoke(array $params): string
6999
'is_grantuser' => $this->dbi->isGrantUser(),
70100
'privileges' => $privileges,
71101
]);
102+
$this->render('export_modal');
72103
}
73104
}

libraries/classes/Footer.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,6 @@ public function getSelfUrl(): string
153153

154154
$params['server'] = $GLOBALS['server'];
155155

156-
// needed for server privileges tabs
157-
if (isset($_GET['viewing_mode']) && in_array($_GET['viewing_mode'], ['server', 'db', 'table'])) {
158-
$params['viewing_mode'] = $_GET['viewing_mode'];
159-
}
160-
161-
/**
162-
* @todo coming from /server/privileges, here $db is not set,
163-
* add the following condition below when that is fixed
164-
* && $_GET['checkprivsdb'] == $db
165-
*/
166-
if (isset($_GET['checkprivsdb'])) {
167-
$params['checkprivsdb'] = $_GET['checkprivsdb'];
168-
}
169-
170-
/**
171-
* @todo coming from /server/privileges, here $table is not set,
172-
* add the following condition below when that is fixed
173-
* && $_REQUEST['checkprivstable'] == $table
174-
*/
175-
if (isset($_GET['checkprivstable'])) {
176-
$params['checkprivstable'] = $_GET['checkprivstable'];
177-
}
178-
179156
if (isset($_REQUEST['single_table']) && in_array($_REQUEST['single_table'], [true, false])) {
180157
$params['single_table'] = $_REQUEST['single_table'];
181158
}

libraries/classes/Menu.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,11 @@ private function getTableTabs(): array
286286
}
287287

288288
if (($isSuperUser || $isCreateOrGrantUser) && ! $isSystemSchema) {
289-
$tabs['privileges']['route'] = '/server/privileges';
290-
$tabs['privileges']['args']['checkprivsdb'] = $this->db;
291-
$tabs['privileges']['args']['checkprivstable'] = $this->table;
289+
$tabs['privileges']['route'] = '/table/privileges';
292290
// stay on table view
293-
$tabs['privileges']['args']['viewing_mode'] = 'table';
294291
$tabs['privileges']['text'] = __('Privileges');
295292
$tabs['privileges']['icon'] = 's_rights';
296-
$tabs['privileges']['active'] = $route === '/server/privileges';
293+
$tabs['privileges']['active'] = $route === '/table/privileges';
297294
}
298295

299296
/**
@@ -397,13 +394,11 @@ private function getDbTabs(): array
397394
$tabs['operation']['active'] = $route === '/database/operations';
398395

399396
if ($isSuperUser || $isCreateOrGrantUser) {
400-
$tabs['privileges']['route'] = '/server/privileges';
401-
$tabs['privileges']['args']['checkprivsdb'] = $this->db;
397+
$tabs['privileges']['route'] = '/database/privileges';
402398
// stay on database view
403-
$tabs['privileges']['args']['viewing_mode'] = 'db';
404399
$tabs['privileges']['text'] = __('Privileges');
405400
$tabs['privileges']['icon'] = 's_rights';
406-
$tabs['privileges']['active'] = $route === '/server/privileges';
401+
$tabs['privileges']['active'] = $route === '/database/privileges';
407402
}
408403

409404
$tabs['routines']['route'] = '/database/routines';
@@ -503,7 +498,6 @@ private function getServerTabs(): array
503498
'/server/privileges',
504499
'/server/user-groups',
505500
]);
506-
$tabs['rights']['args']['viewing_mode'] = 'server';
507501
}
508502

509503
$tabs['export']['icon'] = 'b_export';

libraries/classes/Server/Privileges.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,6 @@ public function getHtmlForInitials(array $arrayInitials)
21012101
return $this->template->render('server/privileges/initials_row', [
21022102
'array_initials' => $arrayInitials,
21032103
'initial' => $_GET['initial'] ?? null,
2104-
'viewing_mode' => $_GET['viewing_mode'] ?? null,
21052104
]);
21062105
}
21072106

@@ -2901,20 +2900,16 @@ public function getAddUserHtmlFieldset($db = '', $table = '')
29012900
return '';
29022901
}
29032902

2904-
$relParams = [];
29052903
$urlParams = ['adduser' => 1];
29062904
if (! empty($db)) {
2907-
$urlParams['dbname'] = $relParams['checkprivsdb'] = $db;
2905+
$urlParams['dbname'] = $db;
29082906
}
29092907

29102908
if (! empty($table)) {
2911-
$urlParams['tablename'] = $relParams['checkprivstable'] = $table;
2909+
$urlParams['tablename'] = $table;
29122910
}
29132911

2914-
return $this->template->render('server/privileges/add_user_fieldset', [
2915-
'url_params' => $urlParams,
2916-
'rel_params' => $relParams,
2917-
]);
2912+
return $this->template->render('server/privileges/add_user_fieldset', ['url_params' => $urlParams]);
29182913
}
29192914

29202915
/**

0 commit comments

Comments
 (0)