Extract HTML from global/database specific privs

Server\Privileges::getHtmlForGlobalOrDbSpecificPrivs

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2019-08-28 16:29:13 -03:00
parent d3fb94a674
commit 7eca9d8e2b
6 changed files with 201 additions and 376 deletions

View File

@ -729,7 +729,6 @@ class Privileges
// global or db-specific
$html_output .= $this->getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row);
}
$html_output .= '</fieldset>' . "\n";
if ($submit) {
$html_output .= '<fieldset id="fieldset_user_privtable_footer" '
. 'class="tblFooters">' . "\n"
@ -741,163 +740,127 @@ class Privileges
} // end of the 'PMA_displayPrivTable()' function
/**
* Get HTML for "Require"
* Get require options
*
* @param array $row privilege array
*
* @return string html snippet
* @return array
*/
public function getHtmlForRequires(array $row)
private function getRequireOptions(array $row): array
{
$specified = (isset($row['ssl_type']) && $row['ssl_type'] == 'SPECIFIED');
$require_options = [
$specified = isset($row['ssl_type']) && $row['ssl_type'] === 'SPECIFIED';
return [
[
'name' => 'ssl_type',
'value' => 'NONE',
'description' => __(
'Does not require SSL-encrypted connections.'
),
'label' => 'REQUIRE NONE',
'checked' => isset($row['ssl_type'])
&& ($row['ssl_type'] == 'NONE'
|| $row['ssl_type'] == '')
? 'checked="checked"'
: '',
'disabled' => false,
'radio' => true,
'name' => 'ssl_type',
'value' => 'NONE',
'description' => __('Does not require SSL-encrypted connections.'),
'label' => 'REQUIRE NONE',
'is_checked' => isset($row['ssl_type']) && ($row['ssl_type'] === 'NONE' || $row['ssl_type'] === ''),
'is_disabled' => false,
'is_radio' => true,
],
[
'name' => 'ssl_type',
'value' => 'ANY',
'description' => __(
'Requires SSL-encrypted connections.'
),
'label' => 'REQUIRE SSL',
'checked' => isset($row['ssl_type']) && ($row['ssl_type'] == 'ANY')
? 'checked="checked"'
: '',
'disabled' => false,
'radio' => true,
'name' => 'ssl_type',
'value' => 'ANY',
'description' => __('Requires SSL-encrypted connections.'),
'label' => 'REQUIRE SSL',
'is_checked' => isset($row['ssl_type']) && $row['ssl_type'] === 'ANY',
'is_disabled' => false,
'is_radio' => true,
],
[
'name' => 'ssl_type',
'value' => 'X509',
'description' => __(
'Requires a valid X509 certificate.'
),
'label' => 'REQUIRE X509',
'checked' => isset($row['ssl_type']) && ($row['ssl_type'] == 'X509')
? 'checked="checked"'
: '',
'disabled' => false,
'radio' => true,
'name' => 'ssl_type',
'value' => 'X509',
'description' => __('Requires a valid X509 certificate.'),
'label' => 'REQUIRE X509',
'is_checked' => isset($row['ssl_type']) && $row['ssl_type'] === 'X509',
'is_disabled' => false,
'is_radio' => true,
],
[
'name' => 'ssl_type',
'value' => 'SPECIFIED',
'name' => 'ssl_type',
'value' => 'SPECIFIED',
'description' => '',
'label' => 'SPECIFIED',
'checked' => $specified ? 'checked="checked"' : '',
'disabled' => false,
'radio' => true,
'label' => 'SPECIFIED',
'is_checked' => $specified,
'is_disabled' => false,
'is_radio' => true,
],
[
'name' => 'ssl_cipher',
'value' => isset($row['ssl_cipher'])
? htmlspecialchars($row['ssl_cipher']) : '',
'description' => __(
'Requires that a specific cipher method be used for a connection.'
),
'label' => 'REQUIRE CIPHER',
'checked' => '',
'disabled' => ! $specified,
'radio' => false,
'name' => 'ssl_cipher',
'value' => $row['ssl_cipher'] ?? '',
'description' => __('Requires that a specific cipher method be used for a connection.'),
'label' => 'REQUIRE CIPHER',
'is_checked' => false,
'is_disabled' => ! $specified,
'is_radio' => false,
],
[
'name' => 'x509_issuer',
'value' => isset($row['x509_issuer'])
? htmlspecialchars($row['x509_issuer']) : '',
'description' => __(
'Requires that a valid X509 certificate issued by this CA be presented.'
),
'label' => 'REQUIRE ISSUER',
'checked' => '',
'disabled' => ! $specified,
'radio' => false,
'name' => 'x509_issuer',
'value' => $row['x509_issuer'] ?? '',
'description' => __('Requires that a valid X509 certificate issued by this CA be presented.'),
'label' => 'REQUIRE ISSUER',
'is_checked' => false,
'is_disabled' => ! $specified,
'is_radio' => false,
],
[
'name' => 'x509_subject',
'value' => isset($row['x509_subject'])
? htmlspecialchars($row['x509_subject']) : '',
'description' => __(
'Requires that a valid X509 certificate with this subject be presented.'
),
'label' => 'REQUIRE SUBJECT',
'checked' => '',
'disabled' => ! $specified,
'radio' => false,
'name' => 'x509_subject',
'value' => $row['x509_subject'] ?? '',
'description' => __('Requires that a valid X509 certificate with this subject be presented.'),
'label' => 'REQUIRE SUBJECT',
'is_checked' => false,
'is_disabled' => ! $specified,
'is_radio' => false,
],
];
return $this->template->render('server/privileges/require_options', [
'require_options' => $require_options,
]);
}
/**
* Get HTML for "Resource limits"
* Get resource limits
*
* @param array $row first row from result or boolean false
*
* @return string html snippet
* @return array
*/
public function getHtmlForResourceLimits(array $row)
private function getResourceLimits(array $row): array
{
$limits = [
return [
[
'input_name' => 'max_questions',
'name_main' => 'MAX QUERIES PER HOUR',
'value' => isset($row['max_questions']) ? $row['max_questions'] : '0',
'input_name' => 'max_questions',
'name_main' => 'MAX QUERIES PER HOUR',
'value' => $row['max_questions'] ?? '0',
'description' => __(
'Limits the number of queries the user may send to the server per hour.'
),
],
[
'input_name' => 'max_updates',
'name_main' => 'MAX UPDATES PER HOUR',
'value' => isset($row['max_updates']) ? $row['max_updates'] : '0',
'input_name' => 'max_updates',
'name_main' => 'MAX UPDATES PER HOUR',
'value' => $row['max_updates'] ?? '0',
'description' => __(
'Limits the number of commands that change any table '
. 'or database the user may execute per hour.'
),
],
[
'input_name' => 'max_connections',
'name_main' => 'MAX CONNECTIONS PER HOUR',
'value' => isset($row['max_connections']) ? $row['max_connections'] : '0',
'input_name' => 'max_connections',
'name_main' => 'MAX CONNECTIONS PER HOUR',
'value' => $row['max_connections'] ?? '0',
'description' => __(
'Limits the number of new connections the user may open per hour.'
),
],
[
'input_name' => 'max_user_connections',
'name_main' => 'MAX USER_CONNECTIONS',
'value' => isset($row['max_user_connections']) ?
$row['max_user_connections'] : '0',
'input_name' => 'max_user_connections',
'name_main' => 'MAX USER_CONNECTIONS',
'value' => $row['max_user_connections'] ?? '0',
'description' => __(
'Limits the number of simultaneous connections '
. 'the user may have.'
),
],
];
$html_output = $this->template->render('server/privileges/resource_limits', [
'limits' => $limits,
]);
$html_output .= '</fieldset>' . "\n";
return $html_output;
}
/**
@ -1097,7 +1060,7 @@ class Privileges
*/
public function getHtmlForGlobalOrDbSpecificPrivs($db, $table, array $row)
{
$privTable_names = [
$privTableNames = [
0 => __('Data'),
1 => __('Structure'),
2 => __('Administration'),
@ -1107,49 +1070,43 @@ class Privileges
$privTable[1] = $this->getStructurePrivilegeTable($table, $row);
$privTable[2] = $this->getAdministrationPrivilegeTable($db);
$html_output = '<input type="hidden" name="grant_count" value="'
. (count($privTable[0])
+ count($privTable[1])
+ count($privTable[2])
- (isset($row['Grant_priv']) ? 1 : 0)
)
. '">';
$grantCount = count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (
isset($row['Grant_priv']) ? 1 : 0);
if ($db == '*') {
$legend = __('Global privileges');
$menu_label = __('Global');
$menuLabel = __('Global');
} elseif ($table == '*') {
$legend = __('Database-specific privileges');
$menu_label = __('Database');
$menuLabel = __('Database');
} else {
$legend = __('Table-specific privileges');
$menu_label = __('Table');
$menuLabel = __('Table');
}
$html_output .= '<fieldset id="fieldset_user_global_rights">'
. '<legend data-submenu-label="' . $menu_label . '">' . $legend
. '<input type="checkbox" id="addUsersForm_checkall" '
. 'class="checkall_box" title="' . __('Check all') . '"> '
. '<label for="addUsersForm_checkall">' . __('Check all') . '</label> '
. '</legend>'
. '<p><small><i>'
. __('Note: MySQL privilege names are expressed in English.')
. '</i></small></p>';
// Output the Global privilege tables with checkboxes
$html_output .= $this->getHtmlForGlobalPrivTableWithCheckboxes(
$globalPrivTable = $this->getHtmlForGlobalPrivTableWithCheckboxes(
$privTable,
$privTable_names,
$privTableNames,
$row
);
// The "Resource limits" box is not displayed for db-specific privs
if ($db == '*') {
$html_output .= $this->getHtmlForResourceLimits($row);
$html_output .= $this->getHtmlForRequires($row);
$resourceLimits = [];
$requireOptions = [];
if ($db === '*') {
$resourceLimits = $this->getResourceLimits($row);
$requireOptions = $this->getRequireOptions($row);
}
// for Safari 2.0.2
$html_output .= '<div class="clearfloat"></div>';
return $html_output;
return $this->template->render('server/privileges/global_db_specific_privileges', [
'grant_count' => $grantCount,
'menu_label' => $menuLabel,
'legend' => $legend,
'global_priv_table' => $globalPrivTable,
'is_global' => $db === '*',
'resource_limits' => $resourceLimits,
'require_options' => $requireOptions,
]);
}
/**

View File

@ -0,0 +1,64 @@
<input type="hidden" name="grant_count" value="{{ grant_count }}">
<fieldset id="fieldset_user_global_rights">
<legend data-submenu-label="{{ menu_label }}">
{{ legend }}
<input type="checkbox" id="addUsersForm_checkall" class="checkall_box" title="{% trans 'Check all' %}">
<label for="addUsersForm_checkall">{% trans 'Check all' %}</label>
</legend>
<p>
<small><em>{% trans 'Note: MySQL privilege names are expressed in English.' %}</em></small>
</p>
{{ global_priv_table|raw }}
{% if is_global %}
<fieldset>
<legend>{% trans 'Resource limits' %}</legend>
<p>
<small><em>{% trans 'Note: Setting these options to 0 (zero) removes the limit.' %}</em></small>
</p>
{% for limit in resource_limits %}
<div class="item">
<label for="text_{{ limit.input_name }}">
<code><dfn title="{{ limit.description }}">{{ limit.name_main }}</dfn></code>
</label>
<input type="number" name="{{ limit.input_name }}" id="text_{{ limit.input_name }}" value="
{{- limit.value }}" title="{{ limit.description }}">
</div>
{% endfor %}
</fieldset>
<fieldset>
<legend>SSL</legend>
<div id="require_ssl_div">
{% for option in require_options %}
{% if option.name == 'ssl_cipher' %}
<div id="specified_div" style="padding-left:20px;">
{% endif %}
<div class="item">
{% if option.is_radio %}
<input type="radio" name="ssl_type" id="{{ option.name }}_{{ option.value }}" title="
{{- option.description }}" value="{{ option.value }}"{{ option.is_checked ? ' checked' }}>
<label for="{{ option.name }}_{{ option.value }}">
<code>{{ option.label }}</code>
</label>
{% else %}
<label for="text_{{ option.name }}">
<code>{{ option.label }}</code>
</label>
<input type="text" name="{{ option.name }}" id="text_{{ option.name }}" value="
{{- option.value }}" size="80" title="{{ option.description }}"{{ option.is_disabled ? ' disabled' }}>
{% endif %}
</div>
{% if option.name == 'ssl_cipher' %}
</div>
{% endif %}
{% endfor %}
</div>
</fieldset>
{% endif %}
<div class="clearfloat"></div>
</fieldset>

View File

@ -1,32 +0,0 @@
<fieldset>
<legend>SSL</legend>
<div id="require_ssl_div">
{% for require_option in require_options %}
{% if require_option['name'] is same as('ssl_cipher') %}
<div id="specified_div" style="padding-left:20px;">
{% endif %}
<div class="item">
{% if require_option['radio'] %}
<input type="radio" name="ssl_type"
id="{{ require_option['name'] }}_{{ require_option['value'] }}"
title="{{ require_option['description'] }}"
value="{{ require_option['value'] }}" {{ require_option['checked']|raw }}>
<label for="{{ require_option['name'] }}_{{ require_option['value'] }}">
<code>{{ require_option['label'] }}</code>
</label>
{% else %}
<label for="text_{{ require_option['name'] }}">
<code>{{ require_option['label'] }}</code>
</label>
<input type="text" name="{{ require_option['name'] }}"
id="text_{{ require_option['name'] }}" value="{{ require_option['value'] }}"
size="80" title="{{ require_option['description'] }}"
{%- if require_option['disabled'] %}
disabled
{%- endif %}>
{% endif %}
</div>
{% endfor %}
</div>{# END specified_div #}
</div>{# END require_ssl_div #}
</fieldset>

View File

@ -1,21 +0,0 @@
<fieldset>
<legend>{% trans 'Resource limits' %}</legend>
<p>
<small>
<em>{% trans 'Note: Setting these options to 0 (zero) removes the limit.' %}</em>
</small>
</p>
{% for limit in limits %}
<div class="item">
<label for="text_{{ limit['input_name'] }}">
<code>
<dfn title="{{ limit['description'] }}">
{{ limit['name_main'] }}
</dfn>
</code>
</label>
<input type="number" name="{{ limit['input_name'] }}" id="text_{{ limit['input_name'] }}"
value="{{ limit['value'] }}" title="{{ limit['description'] }}">
</div>
{% endfor %}
</fieldset>

View File

@ -95,7 +95,8 @@
<div class="item">
{% for privilege in privileges %}
<div class="item">
<input type="checkbox" name="{{ privilege.grant }}" id="checkbox_{{ privilege.grant }}" value="Y" title="{{ privilege.description }}"{{ privilege.is_checked ? ' checked' }}>
<input type="checkbox" name="{{ privilege.grant }}" id="checkbox_{{ privilege.grant }}" value="Y" title="
{{- privilege.description }}"{{ privilege.is_checked ? ' checked' }}>
<label for="checkbox_{{ privilege.grant }}">
<code><dfn title="{{ privilege.description }}">{{ privilege.name }}</dfn></code>
</label>
@ -104,3 +105,4 @@
</div>
<div class="clearfloat"></div>
</fieldset>

View File

@ -300,138 +300,6 @@ class PrivilegesTest extends TestCase
);
}
/**
* Test for getHtmlForRequires
*
* @return void
*/
public function testGetHtmlForRequires()
{
/* Assertion 1 */
$row = [
'ssl_type' => '',
'ssh_cipher' => '',
];
$html = $this->serverPrivileges->getHtmlForRequires(
$row
);
// <legend>SSL</legend>
$this->assertStringContainsString(
'<legend>SSL</legend>',
$html
);
$this->assertStringContainsString(
'value="NONE" checked="checked"',
$html
);
$this->assertStringContainsString(
'value="ANY"',
$html
);
$this->assertStringContainsString(
'value="X509"',
$html
);
$this->assertStringContainsString(
'value="SPECIFIED"',
$html
);
/* Assertion 2 */
$row = [
'ssl_type' => 'ANY',
'ssh_cipher' => '',
];
$html = $this->serverPrivileges->getHtmlForRequires(
$row
);
// <legend>SSL</legend>
$this->assertStringContainsString(
'<legend>SSL</legend>',
$html
);
$this->assertStringContainsString(
'value="NONE"',
$html
);
$this->assertStringContainsString(
'value="ANY" checked="checked"',
$html
);
$this->assertStringContainsString(
'value="X509"',
$html
);
$this->assertStringContainsString(
'value="SPECIFIED"',
$html
);
/* Assertion 3 */
$row = [
'ssl_type' => 'X509',
'ssh_cipher' => '',
];
$html = $this->serverPrivileges->getHtmlForRequires(
$row
);
// <legend>SSL</legend>
$this->assertStringContainsString(
'<legend>SSL</legend>',
$html
);
$this->assertStringContainsString(
'value="NONE"',
$html
);
$this->assertStringContainsString(
'value="ANY"',
$html
);
$this->assertStringContainsString(
'value="X509" checked="checked"',
$html
);
$this->assertStringContainsString(
'value="SPECIFIED"',
$html
);
/* Assertion 4 */
$row = [
'ssl_type' => 'SPECIFIED',
'ssh_cipher' => '',
];
$html = $this->serverPrivileges->getHtmlForRequires(
$row
);
// <legend>SSL</legend>
$this->assertStringContainsString(
'<legend>SSL</legend>',
$html
);
$this->assertStringContainsString(
'value="NONE"',
$html
);
$this->assertStringContainsString(
'value="ANY"',
$html
);
$this->assertStringContainsString(
'value="X509"',
$html
);
$this->assertStringContainsString(
'value="SPECIFIED" checked="checked"',
$html
);
}
/**
* Test for getHtmlForUserGroupDialog
*
@ -547,59 +415,6 @@ class PrivilegesTest extends TestCase
);
}
/**
* Test for getHtmlForResourceLimits
*
* @return void
*/
public function testGetHtmlForResourceLimits()
{
$row = [
'max_questions' => 'max_questions',
'max_updates' => 'max_updates',
'max_connections' => 'max_connections',
'max_user_connections' => 'max_user_connections',
];
$html = $this->serverPrivileges->getHtmlForResourceLimits($row);
$this->assertStringContainsString(
'<legend>' . __('Resource limits') . '</legend>',
$html
);
$this->assertStringContainsString(
__('Note: Setting these options to 0 (zero) removes the limit.'),
$html
);
$this->assertStringContainsString(
'MAX QUERIES PER HOUR',
$html
);
$this->assertStringContainsString(
$row['max_connections'],
$html
);
$this->assertStringContainsString(
$row['max_updates'],
$html
);
$this->assertStringContainsString(
$row['max_connections'],
$html
);
$this->assertStringContainsString(
$row['max_user_connections'],
$html
);
$this->assertStringContainsString(
__('Limits the number of new connections the user may open per hour.'),
$html
);
$this->assertStringContainsString(
__('Limits the number of simultaneous connections the user may have.'),
$html
);
}
/**
* Test for getSqlQueryForDisplayPrivTable
*
@ -1031,8 +846,11 @@ class PrivilegesTest extends TestCase
//validate 3: getHtmlForGlobalOrDbSpecificPrivs
$this->assertStringContainsString(
'<fieldset id="fieldset_user_global_rights"><legend '
. 'data-submenu-label="' . __('Global') . '">',
'<fieldset id="fieldset_user_global_rights">',
$html
);
$this->assertStringContainsString(
'<legend data-submenu-label="' . __('Global') . '">',
$html
);
$this->assertStringContainsString(
@ -1094,6 +912,43 @@ class PrivilegesTest extends TestCase
__('Note: Setting these options to 0 (zero) removes the limit.'),
$html
);
$this->assertStringContainsString(
'MAX QUERIES PER HOUR',
$html
);
$this->assertStringContainsString(
'id="text_max_updates" value="max_updates"',
$html
);
$this->assertStringContainsString(
__('Limits the number of new connections the user may open per hour.'),
$html
);
$this->assertStringContainsString(
__('Limits the number of simultaneous connections the user may have.'),
$html
);
$this->assertStringContainsString(
'<legend>SSL</legend>',
$html
);
$this->assertStringContainsString(
'value="NONE"',
$html
);
$this->assertStringContainsString(
'value="ANY"',
$html
);
$this->assertStringContainsString(
'value="X509"',
$html
);
$this->assertStringContainsString(
'value="SPECIFIED"',
$html
);
$GLOBALS['dbi'] = $dbi_old;
$this->serverPrivileges->dbi = $dbi_old;