Extract global/db specific privs view logic to twig

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2019-08-29 18:53:27 -03:00
parent f3bdd3521d
commit d19908eab1
2 changed files with 464 additions and 284 deletions

View File

@ -727,7 +727,11 @@ class Privileges
);
} else {
// global or db-specific
$html_output .= $this->getHtmlForGlobalOrDbSpecificPrivs($db, $table, $row);
$html_output .= $this->template->render('server/privileges/global_db_specific_privileges', [
'is_global' => $db === '*',
'is_database' => $table === '*',
'row' => $row,
]);
}
if ($submit) {
$html_output .= '<fieldset id="fieldset_user_privtable_footer" '
@ -925,285 +929,6 @@ class Privileges
return $privileges;
}
/**
* Get HTML for global or database specific privileges
*
* @param string $db the database
* @param string $table the table
* @param array $row first row from result or boolean false
*
* @return string
*/
public function getHtmlForGlobalOrDbSpecificPrivs($db, $table, array $row)
{
$privTableNames = [
0 => __('Data'),
1 => __('Structure'),
2 => __('Administration'),
];
$privTable = [];
$privTable[0] = $this->getDataPrivilegeTable($db);
$privTable[1] = $this->getStructurePrivilegeTable($table, $row);
$privTable[2] = $this->getAdministrationPrivilegeTable($db);
$grantCount = count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (
isset($row['Grant_priv']) ? 1 : 0);
if ($db == '*') {
$legend = __('Global privileges');
$menuLabel = __('Global');
} elseif ($table == '*') {
$legend = __('Database-specific privileges');
$menuLabel = __('Database');
} else {
$legend = __('Table-specific privileges');
$menuLabel = __('Table');
}
// Output the Global privilege tables with checkboxes
$globalPrivTable = $this->getHtmlForGlobalPrivTableWithCheckboxes(
$privTable,
$privTableNames,
$row
);
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 === '*',
'row' => $row,
]);
}
/**
* Get data privilege table as an array
*
* @param string $db the database
*
* @return array data privilege table
*/
public function getDataPrivilegeTable($db)
{
$data_privTable = [
[
'Select',
'SELECT',
__('Allows reading data.'),
],
[
'Insert',
'INSERT',
__('Allows inserting and replacing data.'),
],
[
'Update',
'UPDATE',
__('Allows changing data.'),
],
[
'Delete',
'DELETE',
__('Allows deleting data.'),
],
];
if ($db == '*') {
$data_privTable[]
= [
'File',
'FILE',
__('Allows importing data from and exporting data into files.'),
];
}
return $data_privTable;
}
/**
* Get structure privilege table as an array
*
* @param string $table the table
* @param array $row first row from result or boolean false
*
* @return array structure privilege table
*/
public function getStructurePrivilegeTable($table, array $row)
{
$structure_privTable = [
[
'Create',
'CREATE',
$table == '*'
? __('Allows creating new databases and tables.')
: __('Allows creating new tables.'),
],
[
'Alter',
'ALTER',
__('Allows altering the structure of existing tables.'),
],
[
'Index',
'INDEX',
__('Allows creating and dropping indexes.'),
],
[
'Drop',
'DROP',
$table == '*'
? __('Allows dropping databases and tables.')
: __('Allows dropping tables.'),
],
[
'Create_tmp_table',
'CREATE TEMPORARY TABLES',
__('Allows creating temporary tables.'),
],
[
'Show_view',
'SHOW VIEW',
__('Allows performing SHOW CREATE VIEW queries.'),
],
[
'Create_routine',
'CREATE ROUTINE',
__('Allows creating stored routines.'),
],
[
'Alter_routine',
'ALTER ROUTINE',
__('Allows altering and dropping stored routines.'),
],
[
'Execute',
'EXECUTE',
__('Allows executing stored routines.'),
],
];
// this one is for a db-specific priv: Create_view_priv
if (isset($row['Create_view_priv'])) {
$structure_privTable[] = [
'Create_view',
'CREATE VIEW',
__('Allows creating new views.'),
];
}
// this one is for a table-specific priv: Create View_priv
if (isset($row['Create View_priv'])) {
$structure_privTable[] = [
'Create View',
'CREATE VIEW',
__('Allows creating new views.'),
];
}
if (isset($row['Event_priv'])) {
// MySQL 5.1.6
$structure_privTable[] = [
'Event',
'EVENT',
__('Allows to set up events for the event scheduler.'),
];
$structure_privTable[] = [
'Trigger',
'TRIGGER',
__('Allows creating and dropping triggers.'),
];
}
return $structure_privTable;
}
/**
* Get administration privilege table as an array
*
* @param string $db the table
*
* @return array administration privilege table
*/
public function getAdministrationPrivilegeTable($db)
{
if ($db == '*') {
$adminPrivTable = [
[
'Grant',
'GRANT',
__(
'Allows adding users and privileges '
. 'without reloading the privilege tables.'
),
],
];
$adminPrivTable[] = [
'Super',
'SUPER',
__(
'Allows connecting, even if maximum number '
. 'of connections is reached; required for '
. 'most administrative operations like '
. 'setting global variables or killing threads of other users.'
),
];
$adminPrivTable[] = [
'Process',
'PROCESS',
__('Allows viewing processes of all users.'),
];
$adminPrivTable[] = [
'Reload',
'RELOAD',
__('Allows reloading server settings and flushing the server\'s caches.'),
];
$adminPrivTable[] = [
'Shutdown',
'SHUTDOWN',
__('Allows shutting down the server.'),
];
$adminPrivTable[] = [
'Show_db',
'SHOW DATABASES',
__('Gives access to the complete list of databases.'),
];
} else {
$adminPrivTable = [
[
'Grant',
'GRANT',
__(
'Allows user to give to other users or remove from other'
. ' users the privileges that user possess yourself.'
),
],
];
}
$adminPrivTable[] = [
'Lock_tables',
'LOCK TABLES',
__('Allows locking tables for the current thread.'),
];
$adminPrivTable[] = [
'References',
'REFERENCES',
__('Has no effect in this MySQL version.'),
];
if ($db == '*') {
$adminPrivTable[] = [
'Repl_client',
'REPLICATION CLIENT',
__('Allows the user to ask where the slaves / masters are.'),
];
$adminPrivTable[] = [
'Repl_slave',
'REPLICATION SLAVE',
__('Needed for the replication slaves.'),
];
$adminPrivTable[] = [
'Create_user',
'CREATE USER',
__('Allows creating, dropping and renaming user accounts.'),
];
}
return $adminPrivTable;
}
/**
* Get HTML snippet for global privileges table with check boxes
*

View File

@ -1,7 +1,20 @@
<input type="hidden" name="grant_count" value="{{ grant_count }}">
{% set grant_count = 0 %}
<fieldset id="fieldset_user_global_rights">
<legend data-submenu-label="{{ menu_label }}">
{{ legend }}
<legend data-submenu-label="
{%- if is_global %}
{%- trans 'Global' -%}
{% elseif is_database %}
{%- trans 'Database' -%}
{% else %}
{%- trans 'Table' -%}
{% endif %}">
{% if is_global %}
{% trans 'Global privileges' %}
{% elseif is_database %}
{% trans 'Database-specific privileges' %}
{% else %}
{% trans 'Table-specific privileges' %}
{% endif %}
<input type="checkbox" id="addUsersForm_checkall" class="checkall_box" title="{% trans 'Check all' %}">
<label for="addUsersForm_checkall">{% trans 'Check all' %}</label>
</legend>
@ -9,7 +22,448 @@
<small><em>{% trans 'Note: MySQL privilege names are expressed in English.' %}</em></small>
</p>
{{ global_priv_table|raw }}
<fieldset>
<legend>
<input type="checkbox" class="sub_checkall_box" id="checkall_Data_priv" title="{% trans 'Check all' %}">
<label for="checkall_Data_priv">{% trans 'Data' %}</label>
</legend>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Select_priv" id="checkbox_Select_priv" value="Y" title="
{%- trans 'Allows reading data.' %}"{{ row['Select_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Select_priv">
<code>
<dfn title="{% trans 'Allows reading data.' %}">
SELECT
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Insert_priv" id="checkbox_Insert_priv" value="Y" title="
{%- trans 'Allows inserting and replacing data.' %}"{{ row['Insert_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Insert_priv">
<code>
<dfn title="{% trans 'Allows inserting and replacing data.' %}">
INSERT
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Update_priv" id="checkbox_Update_priv" value="Y" title="
{%- trans 'Allows changing data.' %}"{{ row['Update_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Update_priv">
<code>
<dfn title="{% trans 'Allows changing data.' %}">
UPDATE
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Delete_priv" id="checkbox_Delete_priv" value="Y" title="
{%- trans 'Allows deleting data.' %}"{{ row['Delete_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Delete_priv">
<code>
<dfn title="{% trans 'Allows deleting data.' %}">
DELETE
</dfn>
</code>
</label>
</div>
{% if is_global %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="File_priv" id="checkbox_File_priv" value="Y" title="
{%- trans 'Allows importing data from and exporting data into files.' %}"{{ row['File_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_File_priv">
<code>
<dfn title="{% trans 'Allows importing data from and exporting data into files.' %}">
FILE
</dfn>
</code>
</label>
</div>
{% endif %}
</fieldset>
<fieldset>
<legend>
<input type="checkbox" class="sub_checkall_box" id="checkall_Structure_priv" title="{% trans 'Check all' %}">
<label for="checkall_Structure_priv">{% trans 'Structure' %}</label>
</legend>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create_priv" id="checkbox_Create_priv" value="Y" title="
{%- if is_database %}
{%- trans 'Allows creating new databases and tables.' -%}
{% else %}
{%- trans 'Allows creating new tables.' -%}
{% endif %}"{{ row['Create_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create_priv">
<code>
<dfn title="
{%- if is_database %}
{%- trans 'Allows creating new databases and tables.' -%}
{% else %}
{%- trans 'Allows creating new tables.' -%}
{% endif %}">
CREATE
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Alter_priv" id="checkbox_Alter_priv" value="Y" title="
{%- trans 'Allows altering the structure of existing tables.' %}"{{ row['Alter_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Alter_priv">
<code>
<dfn title="{% trans 'Allows altering the structure of existing tables.' %}">
ALTER
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Index_priv" id="checkbox_Index_priv" value="Y" title="
{%- trans 'Allows creating and dropping indexes.' %}"{{ row['Index_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Index_priv">
<code>
<dfn title="{% trans 'Allows creating and dropping indexes.' %}">
INDEX
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Drop_priv" id="checkbox_Drop_priv" value="Y" title="
{%- if is_database %}
{%- trans 'Allows dropping databases and tables.' -%}
{% else %}
{%- trans 'Allows dropping tables.' -%}
{% endif %}"{{ row['Drop_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Drop_priv">
<code>
<dfn title="
{%- if is_database %}
{%- trans 'Allows dropping databases and tables.' -%}
{% else %}
{%- trans 'Allows dropping tables.' -%}
{% endif %}">
DROP
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create_tmp_table_priv" id="checkbox_Create_tmp_table_priv" value="Y" title="
{%- trans 'Allows creating temporary tables.' %}"{{ row['Create_tmp_table_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create_tmp_table_priv">
<code>
<dfn title="{% trans 'Allows creating temporary tables.' %}">
CREATE TEMPORARY TABLES
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Show_view_priv" id="checkbox_Show_view_priv" value="Y" title="
{%- trans 'Allows performing SHOW CREATE VIEW queries.' %}"{{ row['Show_view_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Show_view_priv">
<code>
<dfn title="{% trans 'Allows performing SHOW CREATE VIEW queries.' %}">
SHOW VIEW
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create_routine_priv" id="checkbox_Create_routine_priv" value="Y" title="
{%- trans 'Allows creating stored routines.' %}"{{ row['Create_routine_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create_routine_priv">
<code>
<dfn title="{% trans 'Allows creating stored routines.' %}">
CREATE ROUTINE
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Alter_routine_priv" id="checkbox_Alter_routine_priv" value="Y" title="
{%- trans 'Allows altering and dropping stored routines.' %}"{{ row['Alter_routine_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Alter_routine_priv">
<code>
<dfn title="{% trans 'Allows altering and dropping stored routines.' %}">
ALTER ROUTINE
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Execute_priv" id="checkbox_Execute_priv" value="Y" title="
{%- trans 'Allows executing stored routines.' %}"{{ row['Execute_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Execute_priv">
<code>
<dfn title="{% trans 'Allows executing stored routines.' %}">
EXECUTE
</dfn>
</code>
</label>
</div>
{% if row['Create_view_priv'] is defined %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create_view_priv" id="checkbox_Create_view_priv" value="Y" title="
{%- trans 'Allows creating new views.' %}"{{ row['Create_view_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create_view_priv">
<code>
<dfn title="{% trans 'Allows creating new views.' %}">
CREATE VIEW
</dfn>
</code>
</label>
</div>
{% endif %}
{% if row['Create View_priv'] is defined %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create View_priv" id="checkbox_Create View_priv" value="Y" title="
{%- trans 'Allows creating new views.' %}"{{ row['Create View_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create View_priv">
<code>
<dfn title="{% trans 'Allows creating new views.' %}">
CREATE VIEW
</dfn>
</code>
</label>
</div>
{% endif %}
{% if row['Event_priv'] is defined %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Event_priv" id="checkbox_Event_priv" value="Y" title="
{%- trans 'Allows to set up events for the event scheduler.' %}"{{ row['Event_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Event_priv">
<code>
<dfn title="{% trans 'Allows to set up events for the event scheduler.' %}">
EVENT
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Trigger_priv" id="checkbox_Trigger_priv" value="Y" title="
{%- trans 'Allows creating and dropping triggers.' %}"{{ row['Trigger_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Trigger_priv">
<code>
<dfn title="{% trans 'Allows creating and dropping triggers.' %}">
TRIGGER
</dfn>
</code>
</label>
</div>
{% endif %}
</fieldset>
<fieldset>
<legend>
<input type="checkbox" class="sub_checkall_box" id="checkall_Administration_priv" title="{% trans 'Check all' %}">
<label for="checkall_Administration_priv">{% trans 'Administration' %}</label>
</legend>
{% if is_global %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Grant_priv" id="checkbox_Grant_priv" value="Y" title="
{%- trans 'Allows adding users and privileges without reloading the privilege tables.' %}"{{ row['Grant_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Grant_priv">
<code>
<dfn title="{% trans 'Allows adding users and privileges without reloading the privilege tables.' %}">
GRANT
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Super_priv" id="checkbox_Super_priv" value="Y" title="
{%- trans 'Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.' %}"
{{- row['Super_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Super_priv">
<code>
<dfn title="{% trans 'Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.' %}">
SUPER
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Process_priv" id="checkbox_Process_priv" value="Y" title="
{%- trans 'Allows viewing processes of all users.' %}"{{ row['Process_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Process_priv">
<code>
<dfn title="{% trans 'Allows viewing processes of all users.' %}">
PROCESS
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Reload_priv" id="checkbox_Reload_priv" value="Y" title="
{%- trans 'Allows reloading server settings and flushing the server\'s caches.' %}"{{ row['Reload_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Reload_priv">
<code>
<dfn title="{% trans 'Allows reloading server settings and flushing the server\'s caches.' %}">
RELOAD
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Shutdown_priv" id="checkbox_Shutdown_priv" value="Y" title="
{%- trans 'Allows shutting down the server.' %}"{{ row['Shutdown_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Shutdown_priv">
<code>
<dfn title="{% trans 'Allows shutting down the server.' %}">
SHUTDOWN
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Show_db_priv" id="checkbox_Show_db_priv" value="Y" title="
{%- trans 'Gives access to the complete list of databases.' %}"{{ row['Show_db_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Show_db_priv">
<code>
<dfn title="{% trans 'Gives access to the complete list of databases.' %}">
SHOW DATABASES
</dfn>
</code>
</label>
</div>
{% else %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Grant_priv" id="checkbox_Grant_priv" value="Y" title="
{%- trans 'Allows user to give to other users or remove from other users the privileges that user possess yourself.' %}"
{{- row['Grant_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Grant_priv">
<code>
<dfn title="{% trans 'Allows user to give to other users or remove from other users the privileges that user possess yourself.' %}">
GRANT
</dfn>
</code>
</label>
</div>
{% endif %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Lock_tables_priv" id="checkbox_Lock_tables_priv" value="Y" title="
{%- trans 'Allows locking tables for the current thread.' %}"{{ row['Lock_tables_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Lock_tables_priv">
<code>
<dfn title="{% trans 'Allows locking tables for the current thread.' %}">
LOCK TABLES
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="References_priv" id="checkbox_References_priv" value="Y" title="
{%- trans 'Has no effect in this MySQL version.' %}"{{ row['References_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_References_priv">
<code>
<dfn title="{% trans 'Has no effect in this MySQL version.' %}">
REFERENCES
</dfn>
</code>
</label>
</div>
{% if is_global %}
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Repl_client_priv" id="checkbox_Repl_client_priv" value="Y" title="
{%- trans 'Allows the user to ask where the slaves / masters are.' %}"{{ row['Repl_client_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Repl_client_priv">
<code>
<dfn title="{% trans 'Allows the user to ask where the slaves / masters are.' %}">
REPLICATION CLIENT
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Repl_slave_priv" id="checkbox_Repl_slave_priv" value="Y" title="
{%- trans 'Needed for the replication slaves.' %}"{{ row['Repl_slave_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Repl_slave_priv">
<code>
<dfn title="{% trans 'Needed for the replication slaves.' %}">
REPLICATION SLAVE
</dfn>
</code>
</label>
</div>
<div class="item">
{% set grant_count = grant_count + 1 %}
<input type="checkbox" class="checkall" name="Create_user_priv" id="checkbox_Create_user_priv" value="Y" title="
{%- trans 'Allows creating, dropping and renaming user accounts.' %}"{{ row['Create_user_priv'] == 'Y' ? ' checked' }}>
<label for="checkbox_Create_user_priv">
<code>
<dfn title="{% trans 'Allows creating, dropping and renaming user accounts.' %}">
CREATE USER
</dfn>
</code>
</label>
</div>
{% endif %}
</fieldset>
{% if is_global %}
<fieldset>
@ -143,3 +597,4 @@
<div class="clearfloat"></div>
</fieldset>
<input type="hidden" name="grant_count" value="{{ grant_count - (row['Grant_priv'] is defined ? 1 : 0) }}">