Cache current user information for DBI lifetime

We query this information quite often, so it makes sense to fetch it
only once from the MySQL server.

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2016-04-06 09:45:43 +02:00
parent 354a434838
commit c136a74705
5 changed files with 22 additions and 24 deletions

View File

@ -48,6 +48,11 @@ class DatabaseInterface
*/
private $_table_cache;
/**
* @var array Current user and host cache
*/
private $_current_user;
/**
* Constructor
*
@ -57,6 +62,7 @@ class DatabaseInterface
{
$this->_extension = $ext;
$this->_table_cache = array();
$this->_current_user = array();
}
/**
@ -2138,12 +2144,12 @@ class DatabaseInterface
if ($type === 'super') {
$query = 'SELECT 1 FROM mysql.user LIMIT 1';
} elseif ($type === 'create') {
list($user, $host) = $this->_getCurrentUserAndHost();
list($user, $host) = $this->getCurrentUserAndHost();
$query = "SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` "
. "WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND "
. "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE` LIMIT 1";
} elseif ($type === 'grant') {
list($user, $host) = $this->_getCurrentUserAndHost();
list($user, $host) = $this->getCurrentUserAndHost();
$query = "SELECT 1 FROM ("
. "SELECT `GRANTEE`, `IS_GRANTABLE` FROM "
. "`INFORMATION_SCHEMA`.`COLUMN_PRIVILEGES` UNION "
@ -2207,10 +2213,13 @@ class DatabaseInterface
*
* @return array array of username and hostname
*/
private function _getCurrentUserAndHost()
public function getCurrentUserAndHost()
{
$user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();");
return explode("@", $user);
if (count($this->_current_user) == 0) {
$user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();");
$this->_current_user = explode("@", $user);
}
return $this->_current_user;
}
/**

View File

@ -4036,20 +4036,16 @@ class Util
{
// Get the username for the current user in the format
// required to use in the information schema database.
$user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();");
if ($user === false) {
return false;
}
list($user, $host) = $GLOBALS['dbi']->getCurrentUserAndHost();
if ($user == '@') { // MySQL is started with --skip-grant-tables
if ($user === '') { // MySQL is started with --skip-grant-tables
return true;
}
$user = explode('@', $user);
$username = "''";
$username .= str_replace("'", "''", $user[0]);
$username .= str_replace("'", "''", $user);
$username .= "''@''";
$username .= str_replace("'", "''", $user[1]);
$username .= str_replace("'", "''", $host);
$username .= "''";
// Prepare the query

View File

@ -306,8 +306,8 @@ function PMA_analyseShowGrant()
PMA\libraries\Util::cacheSet('db_priv', $GLOBALS['db_priv']);
} // end function
$user = $GLOBALS['dbi']->fetchValue("SELECT CURRENT_USER();");
if ($user == '@') { // MySQL is started with --skip-grant-tables
list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
if ($username === '') { // MySQL is started with --skip-grant-tables
$GLOBALS['is_create_db_priv'] = true;
$GLOBALS['is_reload_priv'] = true;
$GLOBALS['db_to_create'] = '';

View File

@ -1966,12 +1966,7 @@ function PMA_getCurrentAuthenticationPlugin(
$authentication_plugin = $row['plugin'];
}
} elseif ($mode == 'change') {
$row = $GLOBALS['dbi']->fetchSingleRow(
'SELECT CURRENT_USER() as user;'
);
if (isset($row) && $row) {
list($username, $hostname) = explode('@', $row['user']);
}
list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
$row = $GLOBALS['dbi']->fetchSingleRow(
'SELECT `plugin` FROM `mysql`.`user` WHERE '

View File

@ -141,9 +141,7 @@ function PMA_changePassword($password, $message, $change_password_message)
$hashing_function = PMA_changePassHashingFunction();
$row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user');
$curr_user = $row['user'];
list($username, $hostname) = explode('@', $curr_user);
list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
$serverType = PMA\libraries\Util::getServerType();