Merge pull request #18960 from kamil-tekiela/Refactor-Privileges

Refactor privileges with better return types
This commit is contained in:
Maurício Meneghini Fauth 2024-01-31 17:04:13 -03:00 committed by GitHub
commit ac325d20ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 113 deletions

View File

@ -13612,7 +13612,7 @@ parameters:
-
message: "#^Cannot access offset 'Host' on mixed\\.$#"
count: 4
count: 2
path: src/Server/Privileges.php
-
@ -13642,7 +13642,7 @@ parameters:
-
message: "#^Cannot access offset 'User' on mixed\\.$#"
count: 5
count: 3
path: src/Server/Privileges.php
-
@ -13677,12 +13677,12 @@ parameters:
-
message: "#^Cannot cast mixed to string\\.$#"
count: 8
count: 6
path: src/Server/Privileges.php
-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 18
count: 15
path: src/Server/Privileges.php
-
@ -13797,7 +13797,7 @@ parameters:
-
message: "#^Parameter \\#1 \\$query of method PhpMyAdmin\\\\DatabaseInterface\\:\\:tryQuery\\(\\) expects string, mixed given\\.$#"
count: 3
count: 1
path: src/Server/Privileges.php
-
@ -13805,11 +13805,6 @@ parameters:
count: 2
path: src/Server/Privileges.php
-
message: "#^Parameter \\#1 \\$row of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:getSpecificPrivilege\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#1 \\$str of method PhpMyAdmin\\\\DatabaseInterface\\:\\:quoteString\\(\\) expects string, mixed given\\.$#"
count: 4
@ -13845,11 +13840,6 @@ parameters:
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#2 \\$realSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#"
count: 6
@ -13875,11 +13865,6 @@ parameters:
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#3 \\$sqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#3 \\$subject of function preg_replace expects array\\|string, mixed given\\.$#"
count: 2
@ -13910,16 +13895,6 @@ parameters:
count: 2
path: src/Server/Privileges.php
-
message: "#^Parameter \\#7 \\$alterRealSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Server/Privileges.php
-
message: "#^Parameter \\#8 \\$alterSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Server/Privileges.php
-
message: "#^Possibly invalid array key type array\\<int, string\\>\\|string\\|null\\.$#"
count: 1
@ -17167,7 +17142,7 @@ parameters:
-
message: "#^Cannot call method getMessage\\(\\) on mixed\\.$#"
count: 6
count: 2
path: tests/classes/Server/PrivilegesTest.php
-
@ -17175,16 +17150,6 @@ parameters:
count: 1
path: tests/classes/Server/PrivilegesTest.php
-
message: "#^Parameter \\#2 \\$realSqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: tests/classes/Server/PrivilegesTest.php
-
message: "#^Parameter \\#3 \\$sqlQuery of method PhpMyAdmin\\\\Server\\\\Privileges\\:\\:addUserAndCreateDatabase\\(\\) expects string, mixed given\\.$#"
count: 1
path: tests/classes/Server/PrivilegesTest.php
-
message: "#^Cannot access offset 'host' on mixed\\.$#"
count: 1

View File

@ -2796,12 +2796,10 @@
</PossiblyUnusedParam>
</file>
<file src="src/Controllers/Server/PrivilegesController.php">
<InvalidArgument>
<code><![CDATA[$GLOBALS['sql_query']]]></code>
</InvalidArgument>
<InvalidArrayOffset>
<code><![CDATA[$GLOBALS['dbname']]]></code>
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
<code><![CDATA[$GLOBALS['sql_query'][$key]]]></code>
</InvalidArrayOffset>
<MixedArgument>
<code><![CDATA[$GLOBALS['message']]]></code>
@ -2828,6 +2826,9 @@
<code><![CDATA[$GLOBALS['message']]]></code>
<code>$dbName</code>
</MixedAssignment>
<PossiblyInvalidArgument>
<code><![CDATA[$GLOBALS['sql_query']]]></code>
</PossiblyInvalidArgument>
<RiskyTruthyFalsyComparison>
<code><![CDATA[empty($GLOBALS['message'])]]></code>
<code><![CDATA[empty($GLOBALS['message'])]]></code>
@ -2835,9 +2836,6 @@
<code>empty($routinename)</code>
<code>empty($routinename)</code>
</RiskyTruthyFalsyComparison>
<UnusedVariable>
<code>$key</code>
</UnusedVariable>
</file>
<file src="src/Controllers/Server/ReplicationController.php">
<InvalidArrayOffset>
@ -11014,18 +11012,12 @@
</InvalidArrayOffset>
<MixedArgument>
<code><![CDATA[$GLOBALS['dbname']]]></code>
<code>$alterRealSqlQuery</code>
<code>$alterSqlQuery</code>
<code>$createUserReal</code>
<code>$paramDbName</code>
<code>$paramDbName</code>
<code>$paramRoutineName</code>
<code>$paramRoutineName</code>
<code>$paramTableName</code>
<code>$paramTableName</code>
<code>$passwordSetReal</code>
<code>$privilege</code>
<code>$realSqlQuery</code>
<code>$row</code>
<code>$row</code>
<code><![CDATA[$row1['Type']]]></code>
@ -11042,7 +11034,6 @@
<code><![CDATA[$row['Table_priv']]]></code>
<code>$sqlQuery</code>
<code>$sqlQuery</code>
<code>$sqlQuery</code>
<code>$user</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
@ -11057,10 +11048,6 @@
<code><![CDATA[$host['User']]]></code>
<code><![CDATA[$host['User']]]></code>
<code><![CDATA[$host['privs']]]></code>
<code><![CDATA[$privilege['Host']]]></code>
<code><![CDATA[$privilege['Host']]]></code>
<code><![CDATA[$privilege['User']]]></code>
<code><![CDATA[$privilege['User']]]></code>
<code><![CDATA[$row['Db']]]></code>
<code><![CDATA[$row['Grant_priv']]]></code>
<code><![CDATA[$row['Proc_priv']]]></code>
@ -11100,18 +11087,9 @@
<code>$paramDbName</code>
<code>$paramRoutineName</code>
<code>$paramTableName</code>
<code>$privilege</code>
<code><![CDATA[$privilege['routine']]]></code>
<code><![CDATA[$privilege['table']]]></code>
<code>$queriesForDisplay[$tmpCount - 1]</code>
<code>$queriesForDisplay[$tmpCount - 1]</code>
<code>$queriesForDisplay[$tmpCount - 2]</code>
<code>$queriesForDisplay[$tmpCount - 2]</code>
<code>$queriesForDisplay[$tmpCount - 3]</code>
<code>$queries[$tmpCount]</code>
<code>$queries[]</code>
<code>$queries[]</code>
<code>$queries[]</code>
<code>$row</code>
<code><![CDATA[$row['password']]]></code>
<code><![CDATA[$row['password']]]></code>
@ -11128,12 +11106,8 @@
<code>string|null</code>
</MixedInferredReturnType>
<MixedOperand>
<code>$createUserShow</code>
<code>$oneGrant</code>
<code>$origValue</code>
<code><![CDATA[$privilege['Host']]]></code>
<code><![CDATA[$privilege['User']]]></code>
<code>$sqlQuery</code>
</MixedOperand>
<MixedReturnStatement>
<code><![CDATA[$row['password']]]></code>
@ -11234,6 +11208,8 @@
<code>$alterUserQuery</code>
<code>$alterUserQuery</code>
<code>$alterUserQuery</code>
<code><![CDATA[$privilege['Host']]]></code>
<code><![CDATA[$privilege['User']]]></code>
</PossiblyNullOperand>
<PossiblyUndefinedArrayOffset>
<code>$row1[0]</code>
@ -15442,17 +15418,9 @@
<code>Config::getInstance()</code>
<code>Config::getInstance()</code>
</DeprecatedMethod>
<MixedArgument>
<code>$realSqlQuery</code>
<code>$sqlQuery</code>
</MixedArgument>
<MixedMethodCall>
<code>getMessage</code>
<code>getMessage</code>
<code>getMessage</code>
<code>getMessage</code>
<code>getMessage</code>
<code>getMessage</code>
</MixedMethodCall>
<PossiblyUnusedMethod>
<code>providerUnEscapeMysqlWildcards</code>

View File

@ -910,7 +910,7 @@ class Privileges
* @param string $hostname host name
* @param string $itemType item type
*
* @return mixed[] ($message, $sql_query)
* @return array{Message, string} ($message, $sql_query)
*/
public function getMessageAndSqlQueryForPrivilegesRevoke(
string $dbname,
@ -1124,7 +1124,7 @@ class Privileges
return $privilege;
}
/** @return mixed[] */
/** @return array<int, array<string|null>> */
private function getGlobalAndDatabasePrivileges(DatabaseName $db): array
{
$listOfPrivileges = '`Select_priv`,
@ -1185,7 +1185,7 @@ class Privileges
return $result->fetchAllAssoc();
}
/** @return mixed[] */
/** @return array<int, array<string|null>> */
private function getTablePrivileges(DatabaseName $db, TableName $table): array
{
$query = '
@ -1207,7 +1207,7 @@ class Privileges
return $result->fetchAllAssoc();
}
/** @return mixed[] */
/** @return array<int, array<string|null>> */
private function getRoutinesPrivileges(DatabaseName $db): array
{
$query = '
@ -1930,7 +1930,7 @@ class Privileges
/**
* Update the privileges and return the success or error message
*
* @return mixed[] success message or error message for update
* @return array{string, Message} success message or error message for update
*/
public function updatePrivileges(
string $username,
@ -2309,21 +2309,19 @@ class Privileges
if (empty($_POST['change_copy'])) {
$error = false;
if ($createUserReal !== null) {
if (! $this->dbi->tryQuery($createUserReal)) {
$error = true;
}
if (isset($_POST['authentication_plugin']) && ! empty($passwordSetReal)) {
$this->setProperPasswordHashing($_POST['authentication_plugin']);
if ($this->dbi->tryQuery($passwordSetReal)) {
$sqlQuery .= $passwordSetShow;
}
}
$sqlQuery = $createUserShow . $sqlQuery;
if (! $this->dbi->tryQuery($createUserReal)) {
$error = true;
}
if (isset($_POST['authentication_plugin']) && $passwordSetReal !== '') {
$this->setProperPasswordHashing($_POST['authentication_plugin']);
if ($this->dbi->tryQuery($passwordSetReal)) {
$sqlQuery .= $passwordSetShow;
}
}
$sqlQuery = $createUserShow . $sqlQuery;
[$sqlQuery, $message] = $this->addUserAndCreateDatabase(
$error,
$realSqlQuery,
@ -2354,13 +2352,10 @@ class Privileges
$oldUserGroup = $_POST['old_usergroup'] ?? '';
$this->setUserGroup($_POST['username'], $oldUserGroup);
if ($createUserReal !== null) {
$queries[] = $createUserReal;
}
$queries[] = $createUserReal;
$queries[] = $realSqlQuery;
if (isset($_POST['authentication_plugin']) && ! empty($passwordSetReal)) {
if (isset($_POST['authentication_plugin']) && $passwordSetReal !== '') {
$this->setProperPasswordHashing($_POST['authentication_plugin']);
$queries[] = $passwordSetReal;
@ -2370,11 +2365,9 @@ class Privileges
// $queries_for_display, at the same position occupied
// by the real query in $queries
$tmpCount = count($queries);
if (isset($createUserReal)) {
$queriesForDisplay[$tmpCount - 2] = $createUserShow;
}
$queriesForDisplay[$tmpCount - 2] = $createUserShow;
if (! empty($passwordSetReal)) {
if ($passwordSetReal !== '') {
$queriesForDisplay[$tmpCount - 3] = $createUserShow;
$queriesForDisplay[$tmpCount - 2] = $sqlQuery;
$queriesForDisplay[$tmpCount - 1] = $passwordSetShow;
@ -3017,8 +3010,7 @@ class Privileges
* @param string $hostname host name
* @param string $password password
*
* @return mixed[] ($create_user_real, $create_user_show, $real_sql_query, $sql_query
* $password_set_real, $password_set_show, $alter_real_sql_query, $alter_sql_query)
* @return array{string, string, string, string, string, string, string, string}
*/
public function getSqlQueriesForDisplayAndAddUser(string $username, string $hostname, string $password): array
{
@ -3079,7 +3071,7 @@ class Privileges
(Compatibility::isMySqlOrPerconaDb() && $serverVersion >= 50706)
|| (Compatibility::isMariaDb() && $serverVersion >= 50200)
) {
$passwordSetReal = null;
$passwordSetReal = '';
// Required for binding '%' with '%s'
$createUserStmt = str_replace('%', '%%', $createUserStmt);
@ -3189,10 +3181,10 @@ class Privileges
|| (Compatibility::isMariaDb()
&& $serverVersion >= 50200)
) {
$passwordSetReal = null;
$passwordSetShow = null;
$passwordSetReal = '';
$passwordSetShow = '';
} else {
if ($passwordSetReal !== null) {
if ($passwordSetReal !== '') {
$passwordSetReal .= ';';
}
@ -3239,7 +3231,7 @@ class Privileges
* @param string $database Database name
* @param string $routine Routine name
*
* @return mixed[]
* @return array<string, string>
*/
private function getRoutinePrivileges(
string $username,