From cece77d831b1669a6d510e341db209446b2b20e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 5 Jun 2018 02:26:08 -0300 Subject: Refactor Server/Privileges MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces Template::get() with $this->template->render() Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Display/ChangePassword.php | 3 +- libraries/classes/Server/Privileges.php | 208 +++++++++------------ .../classes/Twig/ServerPrivilegesExtension.php | 3 +- libraries/classes/UserPassword.php | 2 +- server_privileges.php | 5 +- test/classes/Server/PrivilegesTest.php | 2 +- 6 files changed, 98 insertions(+), 125 deletions(-) diff --git a/libraries/classes/Display/ChangePassword.php b/libraries/classes/Display/ChangePassword.php index f9b8c6e25a..b15776d6e9 100644 --- a/libraries/classes/Display/ChangePassword.php +++ b/libraries/classes/Display/ChangePassword.php @@ -11,6 +11,7 @@ namespace PhpMyAdmin\Display; use PhpMyAdmin\Message; use PhpMyAdmin\Server\Privileges; +use PhpMyAdmin\Template; use PhpMyAdmin\Url; use PhpMyAdmin\Util; @@ -33,7 +34,7 @@ class ChangePassword */ public static function getHtml($mode, $username, $hostname) { - $serverPrivileges = new Privileges(); + $serverPrivileges = new Privileges(new Template()); /** * autocomplete feature of IE kills the "onchange" event handler and it * must be replaced by the "onpropertychange" one in this case diff --git a/libraries/classes/Server/Privileges.php b/libraries/classes/Server/Privileges.php index 6b92466d2a..a6bdb3d731 100644 --- a/libraries/classes/Server/Privileges.php +++ b/libraries/classes/Server/Privileges.php @@ -27,6 +27,20 @@ use PhpMyAdmin\Util; */ class Privileges { + /** + * @var Template + */ + public $template; + + /** + * Privileges constructor. + * @param Template $template + */ + public function __construct(Template $template) + { + $this->template = $template; + } + /** * Get Html for User Group Dialog * @@ -477,21 +491,16 @@ class Privileges $name_for_dfn, $name_for_current ) { - $data = [ - 'columns' => $columns, - 'row' => $row, - 'name_for_select' => $name_for_select, - 'priv_for_header' => $priv_for_header, - 'name' => $name, - 'name_for_dfn' => $name_for_dfn, + return $this->template->render('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 - ]; - - $html_output = Template::get('privileges/column_privileges') - ->render($data); - - return $html_output; - } // end function + ]); + } /** * Get sql query for display privileges table @@ -563,16 +572,11 @@ class Privileges } $GLOBALS['dbi']->freeResult($result); - // render the template - $data = [ + return $this->template->render('privileges/choose_user_group', [ 'all_user_groups' => $allUserGroups, - 'user_group' => $userGroup, - 'params' => ['username' => $username] - ]; - $html_output = Template::get('privileges/choose_user_group') - ->render($data); - - return $html_output; + 'user_group' => $userGroup, + 'params' => ['username' => $username] + ]); } /** @@ -839,10 +843,9 @@ class Privileges ], ]; - $html_output = Template::get('privileges/require_options') - ->render(['require_options' => $require_options]); - - return $html_output; + return $this->template->render('privileges/require_options', [ + 'require_options' => $require_options + ]); } /** @@ -892,8 +895,9 @@ class Privileges ] ]; - $html_output = Template::get('privileges/resource_limits') - ->render(['limits' => $limits]); + $html_output = $this->template->render('privileges/resource_limits', [ + 'limits' => $limits + ]); $html_output .= '' . "\n"; @@ -947,19 +951,15 @@ class Privileges $privs ); - $data = [ - 'username' => $username, - 'hostname' => $hostname, - 'database' => $db, - 'routine' => $routine, - 'grant_count' => count($privs), + return $this->template->render('privileges/edit_routine_privileges', [ + 'username' => $username, + 'hostname' => $hostname, + 'database' => $db, + 'routine' => $routine, + 'grant_count' => count($privs), 'priv_checkboxes' => $privCheckboxes, - 'header' => $header, - ]; - $html_output = Template::get('privileges/edit_routine_privileges') - ->render($data); - - return $html_output; + 'header' => $header, + ]); } /** @@ -1453,7 +1453,7 @@ class Privileges array $privTableNames, array $row ) { - return Template::get('privileges/global_priv_table')->render([ + return $this->template->render('privileges/global_priv_table', [ 'priv_table' => $privTable, 'priv_table_names' => $privTableNames, 'row' => $row, @@ -2236,40 +2236,31 @@ class Privileges $html_output .= '
' . "\n" . '' . __('Database for user account') . '' . "\n"; - $html_output .= Template::get('checkbox') - ->render( - [ - 'html_field_name' => 'createdb-1', - 'label' => __('Create database with same name and grant all privileges.'), - 'checked' => false, - 'onclick' => false, - 'html_field_id' => 'createdb-1', - ] - ); + $html_output .= $this->template->render('checkbox', [ + 'html_field_name' => 'createdb-1', + 'label' => __('Create database with same name and grant all privileges.'), + 'checked' => false, + 'onclick' => false, + 'html_field_id' => 'createdb-1', + ]); $html_output .= '
' . "\n"; - $html_output .= Template::get('checkbox') - ->render( - [ - 'html_field_name' => 'createdb-2', - 'label' => __('Grant all privileges on wildcard name (username\\_%).'), - 'checked' => false, - 'onclick' => false, - 'html_field_id' => 'createdb-2', - ] - ); + $html_output .= $this->template->render('checkbox', [ + 'html_field_name' => 'createdb-2', + 'label' => __('Grant all privileges on wildcard name (username\\_%).'), + 'checked' => false, + 'onclick' => false, + 'html_field_id' => 'createdb-2', + ]); $html_output .= '
' . "\n"; if (! empty($dbname)) { - $html_output .= Template::get('checkbox') - ->render( - [ - 'html_field_name' => 'createdb-3', - 'label' => sprintf(__('Grant all privileges on database %s.'), htmlspecialchars($dbname)), - 'checked' => true, - 'onclick' => false, - 'html_field_id' => 'createdb-3', - ] - ); + $html_output .= $this->template->render('checkbox', [ + 'html_field_name' => 'createdb-3', + 'label' => sprintf(__('Grant all privileges on database %s.'), htmlspecialchars($dbname)), + 'checked' => true, + 'onclick' => false, + 'html_field_id' => 'createdb-3', + ]); $html_output .= '' . "\n"; $html_output .= '
' . "\n"; @@ -2457,14 +2448,11 @@ class Privileges $html_output .= ''; $html_output .= '
'; - $html_output .= Template::get('select_all') - ->render( - [ - 'pma_theme_image' => $GLOBALS['pmaThemeImage'], - 'text_dir' => $GLOBALS['text_dir'], - 'form_name' => "usersForm", - ] - ); + $html_output .= $this->template->render('select_all', [ + 'pma_theme_image' => $GLOBALS['pmaThemeImage'], + 'text_dir' => $GLOBALS['text_dir'], + 'form_name' => "usersForm", + ]); $html_output .= Util::getButtonOrImage( 'submit_mult', 'mult_submit', @@ -2547,14 +2535,11 @@ class Privileges $html_output .= '
'; $html_output .= '
'; - $html_output .= Template::get('select_all') - ->render( - [ - 'pma_theme_image' => $GLOBALS['pmaThemeImage'], - 'text_dir' => $GLOBALS['text_dir'], - 'form_name' => "usersForm", - ] - ); + $html_output .= $this->template->render('select_all', [ + 'pma_theme_image' => $GLOBALS['pmaThemeImage'], + 'text_dir' => $GLOBALS['text_dir'], + 'form_name' => "usersForm", + ]); $html_output .= Util::getButtonOrImage( 'submit_mult', 'mult_submit', @@ -3425,7 +3410,7 @@ class Privileges * @param string $type database, table or routine * @param string $dbname database name * - * @return array $html_output + * @return string $html_output */ public function getHtmlForAllTableSpecificRights( $username, @@ -3594,10 +3579,7 @@ class Privileges $data['routines'] = $routines; } - $html_output = Template::get('privileges/privileges_summary') - ->render($data); - - return $html_output; + return $this->template->render('privileges/privileges_summary', $data); } /** @@ -3654,14 +3636,11 @@ class Privileges . '
' . "\n"; $html_output .= '
' - . Template::get('select_all') - ->render( - [ - 'pma_theme_image' => $pmaThemeImage, - 'text_dir' => $text_dir, - 'form_name' => 'usersForm', - ] - ) . "\n"; + . $this->template->render('select_all', [ + 'pma_theme_image' => $pmaThemeImage, + 'text_dir' => $text_dir, + 'form_name' => 'usersForm', + ]) . "\n"; $html_output .= Util::getButtonOrImage( 'submit_mult', 'mult_submit', @@ -3831,8 +3810,7 @@ class Privileges { $html_output = $this->getAddUserHtmlFieldset(); - $html_output .= Template::get('privileges/delete_user_fieldset') - ->render([]); + $html_output .= $this->template->render('privileges/delete_user_fieldset'); return $html_output; } @@ -3871,15 +3849,10 @@ class Privileges uksort($array_initials, "strnatcasecmp"); - $html_output = Template::get('privileges/initials_row') - ->render( - [ - 'array_initials' => $array_initials, - 'initial' => isset($_REQUEST['initial']) ? $_REQUEST['initial'] : null, - ] - ); - - return $html_output; + return $this->template->render('privileges/initials_row', [ + 'array_initials' => $array_initials, + 'initial' => isset($_REQUEST['initial']) ? $_REQUEST['initial'] : null, + ]); } /** @@ -4597,13 +4570,10 @@ class Privileges = $table; } - return Template::get('privileges/add_user_fieldset') - ->render( - [ - 'url_params' => $url_params, - 'rel_params' => $rel_params - ] - ); + return $this->template->render('privileges/add_user_fieldset', [ + 'url_params' => $url_params, + 'rel_params' => $rel_params + ]); } /** diff --git a/libraries/classes/Twig/ServerPrivilegesExtension.php b/libraries/classes/Twig/ServerPrivilegesExtension.php index c1848a501f..14b0e01919 100644 --- a/libraries/classes/Twig/ServerPrivilegesExtension.php +++ b/libraries/classes/Twig/ServerPrivilegesExtension.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Twig; use PhpMyAdmin\Server\Privileges; +use PhpMyAdmin\Template; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -27,7 +28,7 @@ class ServerPrivilegesExtension extends AbstractExtension */ public function getFunctions() { - $serverPrivileges = new Privileges(); + $serverPrivileges = new Privileges(new Template()); return [ new TwigFunction( 'ServerPrivileges_formatPrivilege', diff --git a/libraries/classes/UserPassword.php b/libraries/classes/UserPassword.php index 0b4a80391a..4945f88050 100644 --- a/libraries/classes/UserPassword.php +++ b/libraries/classes/UserPassword.php @@ -33,7 +33,7 @@ class UserPassword */ public function __construct() { - $this->serverPrivileges = new Privileges(); + $this->serverPrivileges = new Privileges(new Template()); } /** diff --git a/server_privileges.php b/server_privileges.php index c92009439c..869789e78f 100644 --- a/server_privileges.php +++ b/server_privileges.php @@ -38,7 +38,8 @@ $scripts = $header->getScripts(); $scripts->addFile('server_privileges.js'); $scripts->addFile('vendor/zxcvbn.js'); -$serverPrivileges = new Privileges(); +$template = new Template(); +$serverPrivileges = new Privileges($template); if ((isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'server') @@ -138,7 +139,7 @@ if (!$GLOBALS['dbi']->isSuperuser() && !$GLOBALS['is_grantuser'] && !$GLOBALS['is_createuser'] ) { $response->addHTML( - Template::get('server/sub_page_header')->render([ + $template->render('server/sub_page_header', [ 'type' => 'privileges', 'is_image' => false, ]) diff --git a/test/classes/Server/PrivilegesTest.php b/test/classes/Server/PrivilegesTest.php index 38f5290715..640c4f69a6 100644 --- a/test/classes/Server/PrivilegesTest.php +++ b/test/classes/Server/PrivilegesTest.php @@ -81,7 +81,7 @@ class PrivilegesTest extends TestCase $GLOBALS['text_dir'] = "text_dir"; $GLOBALS['is_reload_priv'] = true; - $this->serverPrivileges = new Privileges(); + $this->serverPrivileges = new Privileges(new Template()); //$_POST $_POST['pred_password'] = 'none'; -- cgit v1.2.3