Skip to content

Commit 2a8ecaf

Browse files
Merge pull request #18958 from kamil-tekiela/UserGroupLevel
Refactor getMenuTabList() and add enum UserGroupLevel
2 parents 559226d + ef34bd1 commit 2a8ecaf

6 files changed

Lines changed: 42 additions & 74 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,11 +1465,6 @@ parameters:
14651465
count: 2
14661466
path: src/ConfigStorage/Relation.php
14671467

1468-
-
1469-
message: "#^Argument of an invalid type array\\|null supplied for foreach, only iterables are supported\\.$#"
1470-
count: 3
1471-
path: src/ConfigStorage/UserGroups.php
1472-
14731468
-
14741469
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
14751470
count: 1

psalm-baseline.xml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -868,24 +868,6 @@
868868
<code>DatabaseInterface::getInstance()</code>
869869
<code>DatabaseInterface::getInstance()</code>
870870
</DeprecatedMethod>
871-
<MixedArgumentTypeCoercion>
872-
<code>$tabNames</code>
873-
</MixedArgumentTypeCoercion>
874-
<MixedAssignment>
875-
<code>$tab</code>
876-
<code>$tab</code>
877-
<code><![CDATA[$tabDetail['tab']]]></code>
878-
<code><![CDATA[$tabDetail['tab_name']]]></code>
879-
<code>$tabGroupName</code>
880-
<code>$tabName</code>
881-
<code>$tabName</code>
882-
<code>$tabNames[]</code>
883-
</MixedAssignment>
884-
<MixedOperand>
885-
<code>$tab</code>
886-
<code>$tab</code>
887-
<code>$tabGroupName</code>
888-
</MixedOperand>
889871
<PossiblyNullArgument>
890872
<code>$key</code>
891873
</PossiblyNullArgument>
@@ -894,11 +876,6 @@
894876
<code>$userGroups</code>
895877
<code>$userGroups[$groupName]</code>
896878
</PossiblyNullArrayOffset>
897-
<PossiblyNullIterator>
898-
<code>$tabs</code>
899-
<code>$tabs</code>
900-
<code>$tabs</code>
901-
</PossiblyNullIterator>
902879
<RiskyTruthyFalsyComparison>
903880
<code><![CDATA[$result->numRows()]]></code>
904881
</RiskyTruthyFalsyComparison>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\ConfigStorage;
6+
7+
enum UserGroupLevel: string
8+
{
9+
case Server = 'server';
10+
case Database = 'db';
11+
case Table = 'table';
12+
}

src/ConfigStorage/UserGroups.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public static function getHtmlForUserGroupsTable(ConfigurableMenusFeature $confi
9393
foreach ($userGroups as $groupName => $tabs) {
9494
$userGroupVal = [];
9595
$userGroupVal['name'] = $groupName;
96-
$userGroupVal['serverTab'] = self::getAllowedTabNames($tabs, 'server');
97-
$userGroupVal['dbTab'] = self::getAllowedTabNames($tabs, 'db');
98-
$userGroupVal['tableTab'] = self::getAllowedTabNames($tabs, 'table');
96+
$userGroupVal['serverTab'] = self::getAllowedTabNames($tabs, UserGroupLevel::Server);
97+
$userGroupVal['dbTab'] = self::getAllowedTabNames($tabs, UserGroupLevel::Database);
98+
$userGroupVal['tableTab'] = self::getAllowedTabNames($tabs, UserGroupLevel::Table);
9999
$userGroupVal['userGroupUrl'] = Url::getFromRoute('/server/user-groups');
100100
$userGroupVal['viewUsersUrl'] = Url::getCommon(
101101
['viewUsers' => 1, 'userGroup' => $groupName],
@@ -132,17 +132,16 @@ public static function getHtmlForUserGroupsTable(ConfigurableMenusFeature $confi
132132
* Returns the list of allowed menu tab names
133133
* based on a data row from usergroup table.
134134
*
135-
* @param mixed[] $row row of usergroup table
136-
* @param string $level 'server', 'db' or 'table'
135+
* @param mixed[] $row row of usergroup table
137136
*
138137
* @return string comma separated list of allowed menu tab names
139138
*/
140-
public static function getAllowedTabNames(array $row, string $level): string
139+
public static function getAllowedTabNames(array $row, UserGroupLevel $level): string
141140
{
142141
$tabNames = [];
143142
$tabs = Util::getMenuTabList($level);
144143
foreach ($tabs as $tab => $tabName) {
145-
if (isset($row[$level . '_' . $tab]) && $row[$level . '_' . $tab] !== 'Y') {
144+
if (isset($row[$level->value . '_' . $tab]) && $row[$level->value . '_' . $tab] !== 'Y') {
146145
continue;
147146
}
148147

@@ -227,17 +226,17 @@ public static function getHtmlToEditUserGroup(
227226

228227
$tabList = self::getTabList(
229228
__('Server-level tabs'),
230-
'server',
229+
UserGroupLevel::Server,
231230
$allowedTabs['server'],
232231
);
233232
$tabList .= self::getTabList(
234233
__('Database-level tabs'),
235-
'db',
234+
UserGroupLevel::Database,
236235
$allowedTabs['db'],
237236
);
238237
$tabList .= self::getTabList(
239238
__('Table-level tabs'),
240-
'table',
239+
UserGroupLevel::Table,
241240
$allowedTabs['table'],
242241
);
243242

@@ -257,12 +256,11 @@ public static function getHtmlToEditUserGroup(
257256
* tabs of 'server', 'db' or 'table' levels.
258257
*
259258
* @param string $title title of the checkbox group
260-
* @param string $level 'server', 'db' or 'table'
261259
* @param mixed[] $selected array of selected allowed tabs
262260
*
263261
* @return string HTML for checkbox groups
264262
*/
265-
public static function getTabList(string $title, string $level, array $selected): string
263+
public static function getTabList(string $title, UserGroupLevel $level, array $selected): string
266264
{
267265
$tabs = Util::getMenuTabList($level);
268266
$tabDetails = [];
@@ -278,7 +276,7 @@ public static function getTabList(string $title, string $level, array $selected)
278276

279277
return $template->render('server/user_groups/tab_list', [
280278
'title' => $title,
281-
'level' => $level,
279+
'level' => $level->value,
282280
'tab_details' => $tabDetails,
283281
]);
284282
}
@@ -294,7 +292,6 @@ public static function edit(
294292
string $userGroup,
295293
bool $new = false,
296294
): void {
297-
$tabs = Util::getMenuTabList();
298295
$groupTable = Util::backquote($configurableMenusFeature->database)
299296
. '.' . Util::backquote($configurableMenusFeature->userGroups);
300297

@@ -309,14 +306,13 @@ public static function edit(
309306
. '(`usergroup`, `tab`, `allowed`)'
310307
. ' VALUES ';
311308
$first = true;
312-
/** @var array<string, string> $tabGroup */
313-
foreach ($tabs as $tabGroupName => $tabGroup) {
314-
foreach (array_keys($tabGroup) as $tab) {
309+
foreach (UserGroupLevel::cases() as $tabGroupName) {
310+
foreach (array_keys(Util::getMenuTabList($tabGroupName)) as $tab) {
315311
if (! $first) {
316312
$sqlQuery .= ', ';
317313
}
318314

319-
$tabName = $tabGroupName . '_' . $tab;
315+
$tabName = $tabGroupName->value . '_' . $tab;
320316
$allowed = isset($_POST[$tabName]) && $_POST[$tabName] === 'Y';
321317
$sqlQuery .= '(' . $dbi->quoteString($userGroup, ConnectionType::ControlUser)
322318
. ', ' . $dbi->quoteString($tabName, ConnectionType::ControlUser) . ", '"

src/Menu.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace PhpMyAdmin;
99

1010
use PhpMyAdmin\ConfigStorage\Relation;
11+
use PhpMyAdmin\ConfigStorage\UserGroupLevel;
1112
use PhpMyAdmin\Dbal\ConnectionType;
1213
use PhpMyAdmin\Query\Utilities;
1314
use PhpMyAdmin\Routing\Routing;
@@ -72,14 +73,14 @@ private function getMenu(): array
7273
$tabs = $this->getTableTabs();
7374
$urlParams['db'] = $this->db;
7475
$urlParams['table'] = $this->table;
75-
$level = 'table';
76+
$level = UserGroupLevel::Table;
7677
} elseif ($this->db !== '') {
7778
$tabs = $this->getDbTabs();
7879
$urlParams['db'] = $this->db;
79-
$level = 'db';
80+
$level = UserGroupLevel::Database;
8081
} else {
8182
$tabs = $this->getServerTabs();
82-
$level = 'server';
83+
$level = UserGroupLevel::Server;
8384
}
8485

8586
$allowedTabs = $this->getAllowedTabs($level);
@@ -92,18 +93,16 @@ private function getMenu(): array
9293
/**
9394
* Returns a list of allowed tabs for the current user for the given level
9495
*
95-
* @param string $level 'server', 'db' or 'table' level
96-
*
9796
* @return mixed[] list of allowed tabs
9897
*/
99-
private function getAllowedTabs(string $level): array
98+
private function getAllowedTabs(UserGroupLevel $level): array
10099
{
101-
$cacheKey = 'menu-levels-' . $level;
100+
$cacheKey = 'menu-levels-' . $level->value;
102101
if (SessionCache::has($cacheKey)) {
103102
return SessionCache::get($cacheKey);
104103
}
105104

106-
$allowedTabs = Util::getMenuTabList($level) ?? [];
105+
$allowedTabs = Util::getMenuTabList($level);
107106
$configurableMenusFeature = $this->relation->getRelationParameters()->configurableMenusFeature;
108107
if ($configurableMenusFeature !== null) {
109108
$groupTable = Util::backquote($configurableMenusFeature->database)
@@ -114,7 +113,7 @@ private function getAllowedTabs(string $level): array
114113
$config = Config::getInstance();
115114
$sqlQuery = 'SELECT `tab` FROM ' . $groupTable
116115
. " WHERE `allowed` = 'N'"
117-
. " AND `tab` LIKE '" . $level . "%'"
116+
. " AND `tab` LIKE '" . $level->value . "%'"
118117
. ' AND `usergroup` = (SELECT usergroup FROM '
119118
. $userTable . ' WHERE `username` = '
120119
. $this->dbi->quoteString($config->selectedServer['user'], ConnectionType::ControlUser) . ')';

src/Util.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin;
66

7+
use PhpMyAdmin\ConfigStorage\UserGroupLevel;
78
use PhpMyAdmin\Html\Generator;
89
use PhpMyAdmin\Http\ServerRequest;
910
use PhpMyAdmin\Query\Compatibility;
@@ -1457,14 +1458,12 @@ public static function parseEnumSetValues(string $definition, bool $escapeHtml =
14571458
/**
14581459
* Return the list of tabs for the menu with corresponding names
14591460
*
1460-
* @param string|null $level 'server', 'db' or 'table' level
1461-
*
1462-
* @return mixed[]|null list of tabs for the menu
1461+
* @return array<string, string> list of tabs for the menu
14631462
*/
1464-
public static function getMenuTabList(string|null $level = null): array|null
1463+
public static function getMenuTabList(UserGroupLevel $level): array
14651464
{
1466-
$tabList = [
1467-
'server' => [
1465+
return match ($level) {
1466+
UserGroupLevel::Server => [
14681467
'databases' => __('Databases'),
14691468
'sql' => __('SQL'),
14701469
'status' => __('Status'),
@@ -1479,7 +1478,7 @@ public static function getMenuTabList(string|null $level = null): array|null
14791478
'plugins' => __('Plugins'),
14801479
'engine' => __('Engines'),
14811480
],
1482-
'db' => [
1481+
UserGroupLevel::Database => [
14831482
'structure' => __('Structure'),
14841483
'sql' => __('SQL'),
14851484
'search' => __('Search'),
@@ -1495,7 +1494,7 @@ public static function getMenuTabList(string|null $level = null): array|null
14951494
'designer' => __('Designer'),
14961495
'central_columns' => __('Central columns'),
14971496
],
1498-
'table' => [
1497+
UserGroupLevel::Table => [
14991498
'browse' => __('Browse'),
15001499
'structure' => __('Structure'),
15011500
'sql' => __('SQL'),
@@ -1508,17 +1507,7 @@ public static function getMenuTabList(string|null $level = null): array|null
15081507
'tracking' => __('Tracking'),
15091508
'triggers' => __('Triggers'),
15101509
],
1511-
];
1512-
1513-
if ($level === null) {
1514-
return $tabList;
1515-
}
1516-
1517-
if (array_key_exists($level, $tabList)) {
1518-
return $tabList[$level];
1519-
}
1520-
1521-
return null;
1510+
};
15221511
}
15231512

15241513
/**

0 commit comments

Comments
 (0)