Skip to content

Commit

Permalink
Remove DatabaseInterface::isUserType method
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Oct 25, 2020
1 parent a83ec78 commit 4873c32
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 88 deletions.
183 changes: 106 additions & 77 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1750,104 +1750,133 @@ public function getCurrentUser(): string

public function isSuperUser(): bool
{
return $this->isUserType('super');
if (Util::cacheExists('is_superuser')) {
return Util::cacheGet('is_superuser');
}

if (! $this->isConnected()) {
return false;
}

$result = $this->tryQuery(
'SELECT 1 FROM mysql.user LIMIT 1',
self::CONNECT_USER,
self::QUERY_STORE
);
$isSuperUser = false;

if ($result) {
$isSuperUser = (bool) $this->numRows($result);
}

$this->freeResult($result);
Util::cacheSet('is_superuser', $isSuperUser);

return $isSuperUser;
}

public function isGrantUser(): bool
{
return $this->isUserType('grant');
}
global $cfg;

public function isCreateUser(): bool
{
return $this->isUserType('create');
}
if (Util::cacheExists('is_grantuser')) {
return Util::cacheGet('is_grantuser');
}

public function isLoggedUser(): bool
{
return $this->isUserType('logged');
if (! $this->isConnected()) {
return false;
}

$hasGrantPrivilege = false;

if ($cfg['Server']['DisableIS']) {
$grants = $this->getCurrentUserGrants();

foreach ($grants as $grant) {
if (strpos($grant, 'WITH GRANT OPTION') !== false) {
$hasGrantPrivilege = true;
break;
}
}

Util::cacheSet('is_grantuser', $hasGrantPrivilege);

return $hasGrantPrivilege;
}

[$user, $host] = $this->getCurrentUserAndHost();
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($user, $host);
$result = $this->tryQuery($query, self::CONNECT_USER, self::QUERY_STORE);

if ($result) {
$hasGrantPrivilege = (bool) $this->numRows($result);
}

$this->freeResult($result);
Util::cacheSet('is_grantuser', $hasGrantPrivilege);

return $hasGrantPrivilege;
}

/**
* Checks if current user has global create user/grant privilege
* or is a superuser (i.e. SELECT on mysql.users)
* while caching the result in session.
*
* @param string $type type of user to check for
* i.e. 'create', 'grant', 'super'
*
* @return bool Whether user is a given type of user
*/
private function isUserType(string $type): bool
public function isCreateUser(): bool
{
if (Util::cacheExists('is_' . $type . 'user')) {
return Util::cacheGet('is_' . $type . 'user');
global $cfg;

if (Util::cacheExists('is_createuser')) {
return Util::cacheGet('is_createuser');
}

// when connection failed we don't have a $userlink
if (! isset($this->links[self::CONNECT_USER])) {
if (! $this->isConnected()) {
return false;
}

// checking if user is logged in
if ($type === 'logged') {
return true;
}
$hasCreatePrivilege = false;

if (! $GLOBALS['cfg']['Server']['DisableIS'] || $type === 'super') {
// Prepare query for each user type check
$query = '';
if ($type === 'super') {
$query = 'SELECT 1 FROM mysql.user LIMIT 1';
} elseif ($type === 'create') {
[$user, $host] = $this->getCurrentUserAndHost();
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($user, $host);
} elseif ($type === 'grant') {
[$user, $host] = $this->getCurrentUserAndHost();
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($user, $host);
}
if ($cfg['Server']['DisableIS']) {
$grants = $this->getCurrentUserGrants();

$is = false;
$result = $this->tryQuery(
$query,
self::CONNECT_USER,
self::QUERY_STORE
);
if ($result) {
$is = (bool) $this->numRows($result);
}
$this->freeResult($result);
} else {
$is = false;
$grants = $this->fetchResult(
'SHOW GRANTS FOR CURRENT_USER();',
null,
null,
self::CONNECT_USER,
self::QUERY_STORE
);
if ($grants) {
foreach ($grants as $grant) {
if ($type === 'create') {
if (strpos($grant, 'ALL PRIVILEGES ON *.*') !== false
|| strpos($grant, 'CREATE USER') !== false
) {
$is = true;
break;
}
} elseif ($type === 'grant') {
if (strpos($grant, 'WITH GRANT OPTION') !== false) {
$is = true;
break;
}
}
foreach ($grants as $grant) {
if (strpos($grant, 'ALL PRIVILEGES ON *.*') !== false
|| strpos($grant, 'CREATE USER') !== false
) {
$hasCreatePrivilege = true;
break;
}
}

Util::cacheSet('is_createuser', $hasCreatePrivilege);

return $hasCreatePrivilege;
}

[$user, $host] = $this->getCurrentUserAndHost();
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($user, $host);
$result = $this->tryQuery($query, self::CONNECT_USER, self::QUERY_STORE);

if ($result) {
$hasCreatePrivilege = (bool) $this->numRows($result);
}

Util::cacheSet('is_' . $type . 'user', $is);
$this->freeResult($result);
Util::cacheSet('is_createuser', $hasCreatePrivilege);

return $hasCreatePrivilege;
}

public function isConnected(): bool
{
return isset($this->links[self::CONNECT_USER]);
}

return $is;
private function getCurrentUserGrants(): array
{
return $this->fetchResult(
'SHOW GRANTS FOR CURRENT_USER();',
null,
null,
self::CONNECT_USER,
self::QUERY_STORE
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Dbal/DbalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function isGrantUser(): bool;

public function isCreateUser(): bool;

public function isLoggedUser(): bool;
public function isConnected(): bool;

/**
* Get the current user and host
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private function setHistory(): void
|| ! empty($GLOBALS['error_message'])
|| empty($GLOBALS['sql_query'])
|| ! isset($dbi)
|| ! $dbi->isLoggedUser()
|| ! $dbi->isConnected()
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function getJsParams(): array
'confirm' => $GLOBALS['cfg']['Confirm'],
'LoginCookieValidity' => $GLOBALS['cfg']['LoginCookieValidity'],
'session_gc_maxlifetime' => (int) ini_get('session.gc_maxlifetime'),
'logged_in' => isset($dbi) ? $dbi->isLoggedUser() : false,
'logged_in' => isset($dbi) ? $dbi->isConnected() : false,
'is_https' => $GLOBALS['PMA_Config']->isHttps(),
'rootPath' => $GLOBALS['PMA_Config']->getRootPath(),
'arg_separator' => Url::getArgSeparator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;

class PrivilegesControllerTest extends AbstractTestCase
{
Expand All @@ -34,8 +33,6 @@ public function testIndex(): void
$server = 0;
$cfg['Server']['DisableIS'] = false;
$PMA_PHP_SELF = 'index.php';
Util::cacheSet('is_grantuser', true);
Util::cacheSet('is_createuser', true);

$privileges = [];

Expand Down
3 changes: 0 additions & 3 deletions test/classes/Controllers/Table/PrivilegesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;

class PrivilegesControllerTest extends AbstractTestCase
{
Expand All @@ -35,8 +34,6 @@ public function testIndex(): void
$server = 0;
$cfg['Server']['DisableIS'] = false;
$PMA_PHP_SELF = 'index.php';
Util::cacheSet('is_grantuser', true);
Util::cacheSet('is_createuser', true);

$privileges = [];

Expand Down
6 changes: 4 additions & 2 deletions test/classes/Server/PrivilegesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ protected function setUp(): void
$dbi->expects($this->any())->method('escapeString')
->will($this->returnArgument(0));

$dbi->expects($this->any())->method('isUserType')
$dbi->expects($this->any())->method('isCreateUser')
->will($this->returnValue(true));
$dbi->expects($this->any())->method('isGrantUser')
->will($this->returnValue(true));

$GLOBALS['dbi'] = $dbi;
Expand Down Expand Up @@ -1292,7 +1294,7 @@ public function testGetHtmlForAddUser(): void
$dbi->expects($this->any())
->method('escapeString')
->will($this->returnArgument(0));
$dbi->expects($this->any())->method('isUserType')
$dbi->expects($this->any())->method('isGrantUser')
->will($this->returnValue(true));

$GLOBALS['dbi'] = $dbi;
Expand Down

0 comments on commit 4873c32

Please sign in to comment.