Skip to content
Permalink
Browse files

Extract HTML from db and table links line

Server\Privileges::getLinkToDbAndTable

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information...
mauriciofauth committed Oct 5, 2019
1 parent cc44bd9 commit d7bc90fa2c7e148ce0c4568554c8c05ca5d4b32c
@@ -1799,55 +1799,6 @@ public function getExtraDataForAjaxBehavior(
return $extra_data;
}
/**
* Provide a line with links to the relevant database and table
*
* @param string $url_dbname url database name that urlencode() string
* @param string $dbname database name
* @param string $tablename table name
*
* @return string HTML snippet
*/
public function getLinkToDbAndTable($url_dbname, $dbname, $tablename)
{
$scriptName = Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabDatabase'],
'database'
);
$html_output = '[ ' . __('Database')
. ' <a href="' . $scriptName
. Url::getCommon([
'db' => $url_dbname,
'reload' => 1,
], strpos($scriptName, '?') === false ? '?' : '&')
. '">'
. htmlspecialchars(Util::unescapeMysqlWildcards($dbname)) . ': '
. Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabDatabase']
)
. "</a> ]\n";
if (strlen($tablename) > 0) {
$scriptName = Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabTable'],
'table'
);
$html_output .= ' [ ' . __('Table') . ' <a href="'
. $scriptName
. Url::getCommon([
'db' => $url_dbname,
'table' => $tablename,
'reload' => 1,
], strpos($scriptName, '?') === false ? '?' : '&')
. '">' . htmlspecialchars($tablename) . ': '
. Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabTable']
)
. "</a> ]\n";
}
return $html_output;
}
/**
* no db name given, so we want all privs for the given user
* db name was given, so we want all user specific rights for this db
@@ -2842,7 +2793,6 @@ public function getDataForDBInfo()
$dbname = null;
$tablename = null;
$routinename = null;
$dbname_is_wildcard = null;
if (isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
@@ -2933,14 +2883,10 @@ public function getDataForDBInfo()
}
// check if given $dbname is a wildcard or not
if (isset($dbname)) {
//if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
if (! is_array($dbname) && preg_match('/(?<!\\\\)(?:_|%)/', $dbname)) {
$dbname_is_wildcard = true;
} else {
$dbname_is_wildcard = false;
}
}
$databaseNameIsWildcard = ! is_array($dbname ?? '') && preg_match(
'/(?<!\\\\)(?:_|%)/',
$dbname ?? ''
);
return [
$username,
@@ -2949,7 +2895,7 @@ public function getDataForDBInfo()
isset($tablename) ? $tablename : null,
isset($routinename) ? $routinename : null,
$db_and_table,
$dbname_is_wildcard,
$databaseNameIsWildcard,
];
}
@@ -3237,6 +3183,8 @@ public function getHtmlForUserProperties(
$dbname,
$tablename
) {
global $cfg;
$sql = "SELECT '1' FROM `mysql`.`user`"
. " WHERE `User` = '" . $this->dbi->escapeString($username) . "'"
. " AND `Host` = '" . $this->dbi->escapeString($hostname) . "';";
@@ -3298,11 +3246,20 @@ public function getHtmlForUserProperties(
}
}
// Provide a line with links to the relevant database and table
$linkToDatabaseAndTable = '';
if (! is_array($dbname) && strlen($dbname) > 0 && empty($dbname_is_wildcard)) {
$linkToDatabaseAndTable = $this->getLinkToDbAndTable($url_dbname, $dbname, $tablename);
}
$databaseUrl = Util::getScriptNameForOption(
$cfg['DefaultTabDatabase'],
'database'
);
$databaseUrlTitle = Util::getTitleForTarget(
$cfg['DefaultTabDatabase']
);
$tableUrl = Util::getScriptNameForOption(
$cfg['DefaultTabTable'],
'table'
);
$tableUrlTitle = Util::getTitleForTarget(
$cfg['DefaultTabTable']
);
$changePassword = '';
$userGroup = '';
@@ -3324,17 +3281,21 @@ public function getHtmlForUserProperties(
'params' => $_params,
'privileges_table' => $privilegesTable,
'table_specific_rights' => $tableSpecificRights,
'link_to_database_and_table' => $linkToDatabaseAndTable,
'change_password' => $changePassword,
'database' => $dbname,
'dbname' => $url_dbname,
'username' => $username,
'hostname' => $hostname,
'is_databases' => $dbname_is_wildcard || is_array($dbname) && count($dbname) > 1,
'is_wildcard' => $dbname_is_wildcard,
'table' => $tablename,
'current_user' => $this->dbi->getCurrentUser(),
'user_group' => $userGroup,
'change_login_info_fields' => $changeLoginInfoFields,
'database_url' => $databaseUrl,
'database_url_title' => $databaseUrlTitle,
'table_url' => $tableUrl,
'table_url_title' => $tableUrlTitle,
]);
}
@@ -501,7 +501,7 @@
$hostname ?? '',
$dbname,
$routinename,
(isset($url_dbname) ? $url_dbname : '')
$url_dbname ?? ''
)
);
} else {
@@ -513,12 +513,12 @@
$response->addHTML(
$serverPrivileges->getHtmlForUserProperties(
(isset($dbname_is_wildcard) ? $dbname_is_wildcard : ''),
(isset($url_dbname) ? $url_dbname : ''),
$dbname_is_wildcard,
$url_dbname ?? '',
$username,
$hostname ?? '',
(isset($dbname) ? $dbname : ''),
(isset($tablename) ? $tablename : '')
$dbname ?? '',
$tablename ?? ''
)
);
}
@@ -66,7 +66,31 @@

{{ table_specific_rights|raw }}

{{ link_to_database_and_table|raw }}
{% if database is not iterable and database|length > 0 and not is_wildcard %}
[
{% trans 'Database' %}
<a href="{{ database_url|raw }}{{ get_common({
'db': database|replace({'\\_': '_', '\\%': '%'}),
'reload': true
}, '&') }}">
{{ database|replace({'\\_': '_', '\\%': '%'}) }}:
{{ database_url_title }}
</a>
]
{% if table|length > 0 %}
[
{% trans 'Table' %}
<a href="{{ table_url|raw }}{{ get_common({
'db': database|replace({'\\_': '_', '\\%': '%'}),
'table': table,
'reload': true
}, '&') }}">
{{ table }}:
{{ table_url_title }}
</a>
]
{% endif %}
{% endif %}

{{ change_password|raw }}

@@ -1486,78 +1486,6 @@ public function testGetUserGroupForUser()
$this->serverPrivileges->dbi = $dbi_old;
}
/**
* Test for getLinkToDbAndTable
*
* @return void
*/
public function testGetLinkToDbAndTable()
{
$url_dbname = "url_dbname";
$dbname = "dbname";
$tablename = "tablename";
$html = $this->serverPrivileges->getLinkToDbAndTable($url_dbname, $dbname, $tablename);
//$dbname
$this->assertStringContainsString(
__('Database'),
$html
);
$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabDatabase'],
'database'
),
$html
);
$item = Url::getCommon([
'db' => $url_dbname,
'reload' => 1,
], '');
$this->assertStringContainsString(
$item,
$html
);
$this->assertStringContainsString(
htmlspecialchars($dbname),
$html
);
//$tablename
$this->assertStringContainsString(
__('Table'),
$html
);
$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabTable'],
'table'
),
$html
);
$item = Url::getCommon([
'db' => $url_dbname,
'table' => $tablename,
'reload' => 1,
], '');
$this->assertStringContainsString(
$item,
$html
);
$this->assertStringContainsString(
htmlspecialchars($tablename),
$html
);
$item = Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabTable']
);
$this->assertStringContainsString(
$item,
$html
);
}
/**
* Test for getUsersOverview
*
@@ -1866,6 +1794,63 @@ public function testGetHtmlForUserProperties()
'Create a new user account with the same privileges',
$actual
);
$this->assertStringContainsString(
__('Database'),
$actual
);
$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabDatabase'],
'database'
),
$actual
);
$item = Url::getCommon([
'db' => 'db',
'reload' => 1,
], '');
$this->assertStringContainsString(
$item,
$actual
);
$this->assertStringContainsString(
'db',
$actual
);
//$tablename
$this->assertStringContainsString(
__('Table'),
$actual
);
$this->assertStringContainsString(
Util::getScriptNameForOption(
$GLOBALS['cfg']['DefaultTabTable'],
'table'
),
$actual
);
$item = Url::getCommon([
'db' => 'db',
'table' => 'table',
'reload' => 1,
], '');
$this->assertStringContainsString(
$item,
$actual
);
$this->assertStringContainsString(
'table',
$actual
);
$item = Util::getTitleForTarget(
$GLOBALS['cfg']['DefaultTabTable']
);
$this->assertStringContainsString(
$item,
$actual
);
}
/**

0 comments on commit d7bc90f

Please sign in to comment.
You can’t perform that action at this time.