From d3fb94a6742915d861336083d5d863003e7a1273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Wed, 28 Aug 2019 00:51:35 -0300 Subject: [PATCH] Extract HTML from table specific privileges MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Server\Privileges::getHtmlForTableSpecificPrivileges Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Server/Privileges.php | 217 ++++-------------- .../server/privileges/column_privileges.twig | 24 -- .../privileges/table_specific_privileges.twig | 106 +++++++++ test/classes/Server/PrivilegesTest.php | 133 ++--------- 4 files changed, 169 insertions(+), 311 deletions(-) delete mode 100644 templates/server/privileges/column_privileges.twig create mode 100644 templates/server/privileges/table_specific_privileges.twig diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php index 33858f5af8..6f69b30c4f 100644 --- a/libraries/classes/Server/Privileges.php +++ b/libraries/classes/Server/Privileges.php @@ -504,40 +504,6 @@ class Privileges ]; } - /** - * Displays on which column(s) a table-specific privilege is granted - * - * @param array $columns columns array - * @param array $row first row from result or boolean false - * @param string $name_for_select privilege types - Select_priv, Insert_priv - * Update_priv, References_priv - * @param string $priv_for_header privilege for header - * @param string $name privilege name: insert, select, update, references - * @param string $name_for_dfn name for dfn - * @param string $name_for_current name for current - * - * @return string html snippet - */ - public function getHtmlForColumnPrivileges( - array $columns, - array $row, - $name_for_select, - $priv_for_header, - $name, - $name_for_dfn, - $name_for_current - ) { - return $this->template->render('server/privileges/column_privileges', [ - 'columns' => $columns, - 'row' => $row, - 'name_for_select' => $name_for_select, - 'priv_for_header' => $priv_for_header, - 'name' => $name, - 'name_for_dfn' => $name_for_dfn, - 'name_for_current' => $name_for_current, - ]); - } - /** * Get sql query for display privileges table * @@ -1064,165 +1030,60 @@ class Privileges } } $this->dbi->freeResult($res); - unset($res, $row1, $current); - $html_output = '' . "\n" - . '' . "\n" - . '
' . "\n" - . '' . __('Table-specific privileges') - . '' - . '

' - . __('Note: MySQL privilege names are expressed in English.') - . '

'; + $notAttachedPrivileges = $this->getNotAttachedPrivilegesToTableSpecificColumn($row); - // privs that are attached to a specific column - $html_output .= $this->getHtmlForAttachedPrivilegesToTableSpecificColumn( - $columns, - $row - ); - - // privs that are not attached to a specific column - $html_output .= '
' . "\n" - . $this->getHtmlForNotAttachedPrivilegesToTableSpecificColumn($row) - . '
' . "\n"; - - // for Safari 2.0.2 - $html_output .= '
' . "\n"; - - return $html_output; + return $this->template->render('server/privileges/table_specific_privileges', [ + 'row' => $row, + 'columns' => $columns, + 'privileges' => $notAttachedPrivileges, + ]); } /** - * Get HTML snippet for privileges that are attached to a specific column - * - * @param array $columns columns array - * @param array $row first row from result or boolean false - * - * @return string - */ - public function getHtmlForAttachedPrivilegesToTableSpecificColumn(array $columns, array $row) - { - $html_output = $this->getHtmlForColumnPrivileges( - $columns, - $row, - 'Select_priv', - 'SELECT', - 'select', - __('Allows reading data.'), - 'Select' - ); - - $html_output .= $this->getHtmlForColumnPrivileges( - $columns, - $row, - 'Insert_priv', - 'INSERT', - 'insert', - __('Allows inserting and replacing data.'), - 'Insert' - ); - - $html_output .= $this->getHtmlForColumnPrivileges( - $columns, - $row, - 'Update_priv', - 'UPDATE', - 'update', - __('Allows changing data.'), - 'Update' - ); - - $html_output .= $this->getHtmlForColumnPrivileges( - $columns, - $row, - 'References_priv', - 'REFERENCES', - 'references', - __('Has no effect in this MySQL version.'), - 'References' - ); - return $html_output; - } - - /** - * Get HTML for privileges that are not attached to a specific column + * Get privileges that are not attached to a specific column * * @param array $row first row from result or boolean false * - * @return string + * @return array */ - public function getHtmlForNotAttachedPrivilegesToTableSpecificColumn(array $row) + private function getNotAttachedPrivilegesToTableSpecificColumn(array $row): array { - $html_output = ''; - - foreach ($row as $current_grant => $current_grant_value) { - $grant_type = substr($current_grant, 0, -5); - if (in_array($grant_type, ['Select', 'Insert', 'Update', 'References']) - ) { + $privileges = []; + foreach ($row as $grant => $value) { + $type = substr($grant, 0, -5); + if (in_array($type, ['Select', 'Insert', 'Update', 'References'])) { continue; } - // make a substitution to match the messages variables; - // also we must substitute the grant we get, because we can't generate - // a form variable containing blanks (those would get changed to - // an underscore when receiving the POST) - if ($current_grant == 'Create View_priv') { - $tmp_current_grant = 'CreateView_priv'; - $current_grant = 'Create_view_priv'; - } elseif ($current_grant == 'Show view_priv') { - $tmp_current_grant = 'ShowView_priv'; - $current_grant = 'Show_view_priv'; - } elseif ($current_grant == 'Delete versioning rows_priv') { - $tmp_current_grant = 'DeleteHistoricalRows_priv'; - $current_grant = 'Delete_history_priv'; + + /** + * Make a substitution to match the messages variables; + * also we must substitute the grant we get, because we can't generate + * a form variable containing blanks (those would get changed to + * an underscore when receiving the POST). + */ + if ($grant === 'Create View_priv') { + $grantName = 'CreateView_priv'; + $grant = 'Create_view_priv'; + } elseif ($grant === 'Show view_priv') { + $grantName = 'ShowView_priv'; + $grant = 'Show_view_priv'; + } elseif ($grant === 'Delete versioning rows_priv') { + $grantName = 'DeleteHistoricalRows_priv'; + $grant = 'Delete_history_priv'; } else { - $tmp_current_grant = $current_grant; + $grantName = $grant; } + $descriptionName = 'strPrivDesc' . mb_substr($grantName, 0, -5); - $html_output .= '
' . "\n" - . '' . "\n"; - - $privGlobalName1 = 'strPrivDesc' - . mb_substr( - $tmp_current_grant, - 0, - - 5 - ); - $html_output .= '' . "\n" - . '
' . "\n"; - } // end foreach () - return $html_output; + $privileges[] = [ + 'grant' => $grant, + 'is_checked' => $value === 'Y', + 'name' => mb_strtoupper(mb_substr($grant, 0, -5)), + 'description' => $GLOBALS[$descriptionName] ?? $GLOBALS[$descriptionName . 'Tbl'] ?? '', + ]; + } + return $privileges; } /** diff --git a/templates/server/privileges/column_privileges.twig b/templates/server/privileges/column_privileges.twig deleted file mode 100644 index b49efa50e8..0000000000 --- a/templates/server/privileges/column_privileges.twig +++ /dev/null @@ -1,24 +0,0 @@ -
- - - - - {% trans 'Or' %} - -
diff --git a/templates/server/privileges/table_specific_privileges.twig b/templates/server/privileges/table_specific_privileges.twig new file mode 100644 index 0000000000..a0089899a0 --- /dev/null +++ b/templates/server/privileges/table_specific_privileges.twig @@ -0,0 +1,106 @@ + + +
+ + {% trans 'Table-specific privileges' %} + +

+ {% trans 'Note: MySQL privilege names are expressed in English.' %} +

+ +
+ + + + + {% trans 'Or' %} + +
+ +
+ + + + + {% trans 'Or' %} + +
+ +
+ + + + + {% trans 'Or' %} + +
+ +
+ + + + + {% trans 'Or' %} + +
+ +
+ {% for privilege in privileges %} +
+ + +
+ {% endfor %} +
+ +
diff --git a/test/classes/Server/PrivilegesTest.php b/test/classes/Server/PrivilegesTest.php index ea1efa85fa..339995ecde 100644 --- a/test/classes/Server/PrivilegesTest.php +++ b/test/classes/Server/PrivilegesTest.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests\Server; +use PhpMyAdmin\Config; use PhpMyAdmin\Core; use PhpMyAdmin\Relation; use PhpMyAdmin\RelationCleanup; @@ -40,50 +41,17 @@ class PrivilegesTest extends TestCase */ protected function setUp(): void { - //Constants - if (! defined("PMA_USR_BROWSER_AGENT")) { - define("PMA_USR_BROWSER_AGENT", "other"); - } - - //$_REQUEST - $_REQUEST['log'] = "index1"; - $_REQUEST['pos'] = 3; - $_GET['initial'] = null; - - //$GLOBALS - $GLOBALS['lang'] = 'en'; - $GLOBALS['cfg']['MaxRows'] = 10; - $GLOBALS['cfg']['SendErrorReports'] = "never"; - $GLOBALS['cfg']['ServerDefault'] = "server"; - $GLOBALS['cfg']['RememberSorting'] = true; - $GLOBALS['cfg']['SQP'] = []; - $GLOBALS['cfg']['MaxCharactersInDisplayedSQL'] = 1000; - $GLOBALS['cfg']['ShowSQL'] = true; - $GLOBALS['cfg']['TableNavigationLinksMode'] = 'icons'; - $GLOBALS['cfg']['LimitChars'] = 100; - $GLOBALS['cfg']['AllowThirdPartyFraming'] = false; - $GLOBALS['cfg']['ActionLinksMode'] = "both"; - $GLOBALS['cfg']['DefaultTabDatabase'] = 'structure'; - $GLOBALS['cfg']['DefaultTabTable'] = "structure"; - $GLOBALS['cfg']['NavigationTreeDefaultTabTable'] = "structure"; - $GLOBALS['cfg']['NavigationTreeDefaultTabTable2'] = ""; - $GLOBALS['cfg']['Confirm'] = "Confirm"; - $GLOBALS['cfg']['ShowHint'] = true; - $GLOBALS['cfg']['ShowDatabasesNavigationAsTree'] = true; - $GLOBALS['cfg']['LoginCookieValidity'] = 1440; - $GLOBALS['cfg']['enable_drag_drop_import'] = true; - + $GLOBALS['PMA_Config'] = new Config(); + $GLOBALS['PMA_Config']->enableBc(); + $GLOBALS['cfg']['Server']['DisableIS'] = false; $GLOBALS['cfgRelation'] = []; $GLOBALS['cfgRelation']['menuswork'] = false; $GLOBALS['table'] = "table"; - $GLOBALS['PMA_PHP_SELF'] = Core::getenv('PHP_SELF'); $GLOBALS['pmaThemeImage'] = 'image'; $GLOBALS['server'] = 1; $GLOBALS['db'] = 'db'; $GLOBALS['hostname'] = "hostname"; $GLOBALS['username'] = "username"; - $GLOBALS['text_dir'] = "text_dir"; - $GLOBALS['is_reload_priv'] = true; $relation = new Relation($GLOBALS['dbi']); $this->serverPrivileges = new Privileges( @@ -332,66 +300,6 @@ class PrivilegesTest extends TestCase ); } - /** - * Test for getHtmlForColumnPrivileges - * - * @return void - */ - public function testGetHtmlForColumnPrivileges() - { - $columns = [ - 'row1' => 'name1', - ]; - $row = [ - 'name_for_select' => 'Y', - ]; - $name_for_select = 'name_for_select'; - $priv_for_header = 'priv_for_header'; - $name = 'name'; - $name_for_dfn = 'name_for_dfn'; - $name_for_current = 'name_for_current'; - - $html = $this->serverPrivileges->getHtmlForColumnPrivileges( - $columns, - $row, - $name_for_select, - $priv_for_header, - $name, - $name_for_dfn, - $name_for_current - ); - //$name - $this->assertStringContainsString( - $name, - $html - ); - //$name_for_dfn - $this->assertStringContainsString( - $name_for_dfn, - $html - ); - //$priv_for_header - $this->assertStringContainsString( - $priv_for_header, - $html - ); - //$name_for_select - $this->assertStringContainsString( - $name_for_select, - $html - ); - //$columns and $row - $this->assertStringContainsString( - htmlspecialchars('row1'), - $html - ); - //$columns and $row - $this->assertStringContainsString( - _pgettext('None privileges', 'None'), - $html - ); - } - /** * Test for getHtmlForRequires * @@ -1320,13 +1228,16 @@ class PrivilegesTest extends TestCase $row ); - //validate 1: getHtmlForAttachedPrivilegesToTableSpecificColumn - $item = $this->serverPrivileges->getHtmlForAttachedPrivilegesToTableSpecificColumn( - $columns, - $row + $this->assertStringContainsString( + 'checkbox_Update_priv_none', + $html ); $this->assertStringContainsString( - $item, + 'UPDATE', + $html + ); + $this->assertStringContainsString( + 'checkbox_Insert_priv_none', $html ); $this->assertStringContainsString( @@ -1346,12 +1257,12 @@ class PrivilegesTest extends TestCase $html ); - //validate 2: getHtmlForNotAttachedPrivilegesToTableSpecificColumn - $item = $this->serverPrivileges->getHtmlForNotAttachedPrivilegesToTableSpecificColumn( - $row + $this->assertStringContainsString( + 'title="strPrivDescShowViewTbl" checked>', + $html ); $this->assertStringContainsString( - $item, + 'CREATE_VIEW', $html ); $this->assertStringContainsString( @@ -1362,6 +1273,10 @@ class PrivilegesTest extends TestCase 'ShowView_priv', $html ); + $this->assertStringContainsString( + _pgettext('None privileges', 'None'), + $html + ); } /** @@ -2563,13 +2478,13 @@ class PrivilegesTest extends TestCase $this->assertStringContainsString('A', $actual); $this->assertStringContainsString('Z', $actual); $this->assertStringContainsString( - '-', + '-', $actual ); $this->assertStringContainsString( - '"', + '"', $actual ); $this->assertStringContainsString('Show all', $actual);