Skip to content

Commit

Permalink
Fix type issues in PrivilegesController
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
  • Loading branch information
kamil-tekiela committed Dec 1, 2021
1 parent f4493bb commit 9245cec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 50 deletions.
23 changes: 12 additions & 11 deletions libraries/classes/Controllers/Server/PrivilegesController.php
Expand Up @@ -25,6 +25,7 @@
use function header;
use function implode;
use function is_array;
use function is_string;
use function ob_get_clean;
use function ob_start;
use function str_replace;
Expand Down Expand Up @@ -201,8 +202,8 @@ public function __invoke(): void
$_add_user_error,
] = $serverPrivileges->addUser(
$dbname ?? null,
$username ?? null,
$hostname ?? null,
$username ?? '',
$hostname ?? '',
$password ?? null,
$relationParameters->menuswork
);
Expand All @@ -220,12 +221,12 @@ public function __invoke(): void
/**
* Changes / copies a user, part III
*/
if (isset($_POST['change_copy'])) {
if (isset($_POST['change_copy']) && $username !== null && $hostname !== null) {
$queries = $serverPrivileges->getDbSpecificPrivsQueriesForChangeOrCopyUser($queries, $username, $hostname);
}

$itemType = '';
if (! empty($routinename)) {
if (! empty($routinename) && is_string($dbname)) {
$itemType = $serverPrivileges->getRoutineType($dbname, $routinename);
}

Expand Down Expand Up @@ -263,7 +264,7 @@ public function __invoke(): void
! empty($_POST['changeUserGroup']) && $relationParameters->menuswork
&& $this->dbi->isSuperUser() && $this->dbi->isCreateUser()
) {
$serverPrivileges->setUserGroup($username, $_POST['userGroup']);
$serverPrivileges->setUserGroup($username ?? '', $_POST['userGroup']);
$message = Message::success();
}

Expand All @@ -272,10 +273,10 @@ public function __invoke(): void
*/
if (isset($_POST['revokeall'])) {
[$message, $sql_query] = $serverPrivileges->getMessageAndSqlQueryForPrivilegesRevoke(
($dbname ?? ''),
(is_string($dbname) ? $dbname : ''),
($tablename ?? ($routinename ?? '')),
$username,
$hostname,
$username ?? '',
$hostname ?? '',
$itemType
);
}
Expand All @@ -284,7 +285,7 @@ public function __invoke(): void
* Updates the password
*/
if (isset($_POST['change_pw'])) {
$message = $serverPrivileges->updatePassword($errorUrl, $username, $hostname);
$message = $serverPrivileges->updatePassword($errorUrl, $username ?? '', $hostname ?? '');
}

/**
Expand Down Expand Up @@ -392,7 +393,7 @@ public function __invoke(): void
if (isset($_GET['adduser']) || $_add_user_error === true) {
// Add user
$this->response->addHTML(
$serverPrivileges->getHtmlForAddUser(Util::escapeMysqlWildcards($dbname ?? ''))
$serverPrivileges->getHtmlForAddUser(Util::escapeMysqlWildcards(is_string($dbname) ? $dbname : ''))
);
} elseif (isset($_GET['checkprivsdb'])) {
if (isset($_GET['checkprivstable'])) {
Expand Down Expand Up @@ -437,7 +438,7 @@ public function __invoke(): void
$serverPrivileges->getHtmlForRoutineSpecificPrivileges(
$username,
$hostname ?? '',
$dbname,
is_string($dbname) ? $dbname : '',
$routinename,
Util::escapeMysqlWildcards($url_dbname ?? '')
)
Expand Down
30 changes: 14 additions & 16 deletions libraries/classes/Server/Privileges.php
Expand Up @@ -515,7 +515,7 @@ public function getGrantsArray()
*
* @return string sql query
*/
public function getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname)
public function getSqlQueryForDisplayPrivTable(string $db, string $table, string $username, string $hostname)
{
if ($db === '*') {
return 'SELECT * FROM `mysql`.`user`'
Expand Down Expand Up @@ -721,10 +721,10 @@ public function getHtmlToDisplayPrivilegesTable(
* @return string
*/
public function getHtmlForRoutineSpecificPrivileges(
$username,
$hostname,
$db,
$routine,
string $username,
string $hostname,
string $db,
string $routine,
$urlDbname
) {
$privileges = $this->getRoutinePrivileges($username, $hostname, $db, $routine);
Expand Down Expand Up @@ -1111,8 +1111,8 @@ public function updatePassword($errorUrl, $username, $hostname)
public function getMessageAndSqlQueryForPrivilegesRevoke(
string $dbname,
string $tablename,
$username,
$hostname,
string $username,
string $hostname,
$itemType
) {
$dbAndTable = $this->wildcardEscapeForGrant($dbname, $tablename);
Expand Down Expand Up @@ -2587,8 +2587,8 @@ public function getDataForQueries(array $queries, $queriesForDisplay)
*/
public function addUser(
$dbname,
$username,
$hostname,
string $username,
string $hostname,
?string $password,
$isMenuwork
) {
Expand Down Expand Up @@ -2888,7 +2888,7 @@ public function getDataForDBInfo()
*
* @return array ($title, $export)
*/
public function getListForExportUserDefinition($username, $hostname)
public function getListForExportUserDefinition(string $username, string $hostname)
{
$export = '<textarea class="export" cols="60" rows="15">';

Expand Down Expand Up @@ -3349,8 +3349,8 @@ public function getTablePrivsQueriesForChangeOrCopyUser(
*/
public function getDbSpecificPrivsQueriesForChangeOrCopyUser(
array $queries,
$username,
$hostname
string $username,
string $hostname
) {
$userHostCondition = ' WHERE `User`'
. ' = \'' . $this->dbi->escapeString($_POST['old_username']) . "'"
Expand All @@ -3369,9 +3369,7 @@ public function getDbSpecificPrivsQueriesForChangeOrCopyUser(

$this->dbi->freeResult($res);

$queries = $this->getTablePrivsQueriesForChangeOrCopyUser($userHostCondition, $queries, $username, $hostname);

return $queries;
return $this->getTablePrivsQueriesForChangeOrCopyUser($userHostCondition, $queries, $username, $hostname);
}

/**
Expand Down Expand Up @@ -3732,7 +3730,7 @@ public function getSqlQueriesForDisplayAndAddUser($username, $hostname, $passwor
*
* @return string type
*/
public function getRoutineType($dbname, $routineName)
public function getRoutineType(string $dbname, string $routineName)
{
$routineData = $this->dbi->getRoutines($dbname);

Expand Down
23 changes: 0 additions & 23 deletions psalm-baseline.xml
Expand Up @@ -2521,29 +2521,6 @@
<code>$export</code>
<code>$title</code>
</MixedOperand>
<PossiblyInvalidArgument occurrences="4">
<code>$dbname</code>
<code>$dbname</code>
<code>$dbname ?? ''</code>
<code>$dbname ?? ''</code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast occurrences="4">
<code>$dbname</code>
<code>$dbname</code>
<code>$dbname ?? ''</code>
<code>$dbname ?? ''</code>
</PossiblyInvalidCast>
<PossiblyNullArgument occurrences="9">
<code>$hostname</code>
<code>$hostname</code>
<code>$hostname</code>
<code>$hostname ?? null</code>
<code>$username</code>
<code>$username</code>
<code>$username</code>
<code>$username</code>
<code>$username ?? null</code>
</PossiblyNullArgument>
<RedundantCondition occurrences="1">
<code>$sub_part</code>
</RedundantCondition>
Expand Down

0 comments on commit 9245cec

Please sign in to comment.