Skip to content

Commit f89ab9e

Browse files
Merge pull request #19953 from kamil-tekiela/hasNoRightsToDropDatabase
Refactor hasNoRightsToDropDatabase
2 parents f3ce098 + 70a773e commit f89ab9e

6 files changed

Lines changed: 10 additions & 33 deletions

File tree

app/services_controllers.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@
933933
'arguments' => [
934934
'$response' => '@response',
935935
'$sql' => '@sql',
936-
'$dbi' => '@dbi',
937936
'$pageSettings' => '@' . PageSettings::class,
938937
'$bookmarkRepository' => '@bookmarkRepository',
939938
'$config' => '@config',

src/Controllers/Import/ImportController.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,7 @@ public function __invoke(ServerRequest $request): Response
618618
ResponseRenderer::$reload = $reloadNeeded;
619619

620620
// Check if User is allowed to issue a 'DROP DATABASE' Statement
621-
if (
622-
$this->sql->hasNoRightsToDropDatabase(
623-
$statementInfo,
624-
$this->config->settings['AllowUserDropDatabase'],
625-
$this->dbi->isSuperUser(),
626-
)
627-
) {
621+
if ($this->sql->hasNoRightsToDropDatabase($statementInfo)) {
628622
Generator::mysqlDie(
629623
__('"DROP DATABASE" statements are disabled.'),
630624
'',

src/Controllers/Sql/SqlController.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpMyAdmin\Controllers\InvocableController;
1111
use PhpMyAdmin\Core;
1212
use PhpMyAdmin\Current;
13-
use PhpMyAdmin\Dbal\DatabaseInterface;
1413
use PhpMyAdmin\Html\Generator;
1514
use PhpMyAdmin\Http\Response;
1615
use PhpMyAdmin\Http\ServerRequest;
@@ -35,7 +34,6 @@
3534
public function __construct(
3635
private ResponseRenderer $response,
3736
private Sql $sql,
38-
private DatabaseInterface $dbi,
3937
private PageSettings $pageSettings,
4038
private BookmarkRepository $bookmarkRepository,
4139
private Config $config,
@@ -134,13 +132,7 @@ public function __invoke(ServerRequest $request): Response
134132
* but since a malicious user may pass this variable by url/form, we don't take
135133
* into account this case.
136134
*/
137-
if (
138-
$this->sql->hasNoRightsToDropDatabase(
139-
$statementInfo,
140-
$this->config->settings['AllowUserDropDatabase'],
141-
$this->dbi->isSuperUser(),
142-
)
143-
) {
135+
if ($this->sql->hasNoRightsToDropDatabase($statementInfo)) {
144136
Generator::mysqlDie(
145137
__('"DROP DATABASE" statements are disabled.'),
146138
'',

src/Sql.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,12 @@ private function isDeleteTransformationInfo(StatementInfo $statementInfo): bool
414414
|| $statementInfo->flags->queryType === StatementType::Drop;
415415
}
416416

417-
/**
418-
* Function to check whether the user has rights to drop the database
419-
*
420-
* @param bool $allowUserDropDatabase whether the user is allowed to drop db
421-
* @param bool $isSuperUser whether this user is a superuser
422-
*/
423417
public function hasNoRightsToDropDatabase(
424418
StatementInfo $statementInfo,
425-
bool $allowUserDropDatabase,
426-
bool $isSuperUser,
427419
): bool {
428-
return ! $allowUserDropDatabase && $statementInfo->flags->dropDatabase && ! $isSuperUser;
420+
return ! $this->config->settings['AllowUserDropDatabase']
421+
&& $statementInfo->flags->dropDatabase
422+
&& ! $this->dbi->isSuperUser();
429423
}
430424

431425
/**

tests/unit/Controllers/Table/ReplaceControllerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public function testReplace(): void
113113
$bookmarkRepository,
114114
$config,
115115
),
116-
$dbi,
117116
$pageSettings,
118117
$bookmarkRepository,
119118
$config,

tests/unit/SqlTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,26 @@ public function testIsDeleteTransformationInfo(): void
197197
*/
198198
public function testHasNoRightsToDropDatabase(): void
199199
{
200+
$this->dummyDbi->addResult(
201+
'SELECT 1 FROM mysql.user LIMIT 1',
202+
[],
203+
);
204+
200205
self::assertTrue(
201206
$this->sql->hasNoRightsToDropDatabase(
202207
ParseAnalyze::sqlQuery('DROP DATABASE db', Current::$database)[0],
203-
false,
204-
false,
205208
),
206209
);
207210

208211
self::assertFalse(
209212
$this->sql->hasNoRightsToDropDatabase(
210213
ParseAnalyze::sqlQuery('DROP TABLE tbl', Current::$database)[0],
211-
false,
212-
false,
213214
),
214215
);
215216

216217
self::assertFalse(
217218
$this->sql->hasNoRightsToDropDatabase(
218219
ParseAnalyze::sqlQuery('SELECT * from tbl', Current::$database)[0],
219-
false,
220-
false,
221220
),
222221
);
223222
}

0 commit comments

Comments
 (0)