From aa00a60f62d69eda2b2a8b3ae7c58757fc052bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 10 Apr 2020 20:19:28 -0300 Subject: [PATCH] Remove RteList class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Rte/Events.php | 43 ++++--- libraries/classes/Rte/Routines.php | 107 +++++++++++++++-- libraries/classes/Rte/RteList.php | 178 ----------------------------- libraries/classes/Rte/Triggers.php | 36 +++--- 4 files changed, 150 insertions(+), 214 deletions(-) delete mode 100644 libraries/classes/Rte/RteList.php diff --git a/libraries/classes/Rte/Events.php b/libraries/classes/Rte/Events.php index 4b93aefd74..24ce86f600 100644 --- a/libraries/classes/Rte/Events.php +++ b/libraries/classes/Rte/Events.php @@ -1,7 +1,5 @@ dbi = $dbi; $this->export = new Export($this->dbi); $this->general = new General($this->dbi); - $this->rteList = new RteList($this->dbi); $this->words = new Words(); $this->template = new Template(); } @@ -122,14 +116,23 @@ class Events $items = $this->dbi->getEvents($db); $response = Response::getInstance(); + $hasPrivilege = Util::currentUserHasPrivilege('EVENT', $db); $isAjax = $response->isAjax() && empty($_REQUEST['ajax_page_request']); $rows = ''; foreach ($items as $item) { - $rows .= $this->rteList->getEventRow( - $item, - $isAjax ? 'ajaxInsert hide' : '' + $sqlDrop = sprintf( + 'DROP EVENT IF EXISTS %s', + Util::backquote($item['name']) ); + $rows .= $this->template->render('rte/events/row', [ + 'db' => $db, + 'table' => $table, + 'event' => $item, + 'has_privilege' => $hasPrivilege, + 'sql_drop' => $sqlDrop, + 'row_class' => $isAjax ? 'ajaxInsert hide' : '', + ]); } echo $this->template->render('rte/events/list', [ @@ -155,7 +158,7 @@ class Events */ public function handleEditor() { - global $db, $errors, $message; + global $db, $table, $errors, $message; if (! empty($_POST['editor_process_add']) || ! empty($_POST['editor_process_edit']) @@ -264,7 +267,21 @@ class Events ) ); if (! empty($event)) { - $response->addJSON('new_row', $this->rteList->getEventRow($event)); + $sqlDrop = sprintf( + 'DROP EVENT IF EXISTS %s', + Util::backquote($event['name']) + ); + $response->addJSON( + 'new_row', + $this->template->render('rte/events/row', [ + 'db' => $db, + 'table' => $table, + 'event' => $event, + 'has_privilege' => Util::currentUserHasPrivilege('EVENT', $db), + 'sql_drop' => $sqlDrop, + 'row_class' => '', + ]) + ); } $response->addJSON('insert', ! empty($event)); $response->addJSON('message', $output); diff --git a/libraries/classes/Rte/Routines.php b/libraries/classes/Rte/Routines.php index 6297d328f6..cc3a033cb7 100644 --- a/libraries/classes/Rte/Routines.php +++ b/libraries/classes/Rte/Routines.php @@ -1,7 +1,5 @@ dbi = $dbi; $this->export = new Export($this->dbi); $this->general = new General($this->dbi); - $this->rteList = new RteList($this->dbi); $this->words = new Words(); $this->template = new Template(); } @@ -137,7 +131,7 @@ class Routines $rows = ''; foreach ($items as $item) { - $rows .= $this->rteList->getRoutineRow( + $rows .= $this->getRow( $item, $isAjax ? 'ajaxInsert hide' : '' ); @@ -399,7 +393,7 @@ class Routines mb_strtoupper($_POST['item_name']) ) ); - $response->addJSON('new_row', $this->rteList->getRoutineRow($routine)); + $response->addJSON('new_row', $this->getRow($routine)); $response->addJSON('insert', ! empty($routine)); $response->addJSON('message', $output); exit; @@ -1763,4 +1757,97 @@ class Routines return $retval; } + + /** + * Creates the contents for a row in the list of routines + * + * @param array $routine An array of routine data + * @param string $rowClass Additional class + * + * @return string HTML code of a row for the list of routines + */ + private function getRow(array $routine, $rowClass = '') + { + global $db, $table; + + $sqlDrop = sprintf( + 'DROP %s IF EXISTS %s', + $routine['type'], + Util::backquote($routine['name']) + ); + + // this is for our purpose to decide whether to + // show the edit link or not, so we need the DEFINER for the routine + $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '=' + . "'" . $this->dbi->escapeString($db) . "' " + . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($routine['name']) . "'" + . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($routine['type']) . "'"; + $query = 'SELECT `DEFINER` FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';'; + $routineDefiner = $this->dbi->fetchValue($query); + + $currentUser = $this->dbi->getCurrentUser(); + + // Since editing a procedure involved dropping and recreating, check also for + // CREATE ROUTINE privilege to avoid lost procedures. + $hasEditPrivilege = (Util::currentUserHasPrivilege('CREATE ROUTINE', $db) + && $currentUser == $routineDefiner) || $this->dbi->isSuperuser(); + + // There is a problem with Util::currentUserHasPrivilege(): + // it does not detect all kinds of privileges, for example + // a direct privilege on a specific routine. So, at this point, + // we show the Execute link, hoping that the user has the correct rights. + // Also, information_schema might be hiding the ROUTINE_DEFINITION + // but a routine with no input parameters can be nonetheless executed. + + // Check if the routine has any input parameters. If it does, + // we will show a dialog to get values for these parameters, + // otherwise we can execute it directly. + + $definition = $this->dbi->getDefinition( + $db, + $routine['type'], + $routine['name'] + ); + $hasExecutePrivilege = Util::currentUserHasPrivilege('EXECUTE', $db); + $executeAction = ''; + + if ($definition !== null) { + $parser = new Parser($definition); + + /** + * @var CreateStatement $stmt + */ + $stmt = $parser->statements[0]; + + $params = Routine::getParameters($stmt); + + if ($hasExecutePrivilege) { + $executeAction = 'execute_routine'; + for ($i = 0; $i < $params['num']; $i++) { + if ($routine['type'] == 'PROCEDURE' + && $params['dir'][$i] == 'OUT' + ) { + continue; + } + $executeAction = 'execute_dialog'; + break; + } + } + } + + $hasExportPrivilege = (Util::currentUserHasPrivilege('CREATE ROUTINE', $db) + && $currentUser == $routineDefiner) || $this->dbi->isSuperuser(); + + return $this->template->render('rte/routines/row', [ + 'db' => $db, + 'table' => $table, + 'sql_drop' => $sqlDrop, + 'routine' => $routine, + 'row_class' => $rowClass, + 'has_edit_privilege' => $hasEditPrivilege, + 'has_export_privilege' => $hasExportPrivilege, + 'has_execute_privilege' => $hasExecutePrivilege, + 'execute_action' => $executeAction, + ]); + } } diff --git a/libraries/classes/Rte/RteList.php b/libraries/classes/Rte/RteList.php deleted file mode 100644 index 3fa0855e87..0000000000 --- a/libraries/classes/Rte/RteList.php +++ /dev/null @@ -1,178 +0,0 @@ -dbi = $dbi; - $this->template = new Template(); - } - - /** - * Creates the contents for a row in the list of routines - * - * @param array $routine An array of routine data - * @param string $rowClass Additional class - * - * @return string HTML code of a row for the list of routines - */ - public function getRoutineRow(array $routine, $rowClass = '') - { - global $db, $table; - - $sqlDrop = sprintf( - 'DROP %s IF EXISTS %s', - $routine['type'], - Util::backquote($routine['name']) - ); - - // this is for our purpose to decide whether to - // show the edit link or not, so we need the DEFINER for the routine - $where = 'ROUTINE_SCHEMA ' . Util::getCollateForIS() . '=' - . "'" . $this->dbi->escapeString($db) . "' " - . "AND SPECIFIC_NAME='" . $this->dbi->escapeString($routine['name']) . "'" - . "AND ROUTINE_TYPE='" . $this->dbi->escapeString($routine['type']) . "'"; - $query = 'SELECT `DEFINER` FROM INFORMATION_SCHEMA.ROUTINES WHERE ' . $where . ';'; - $routineDefiner = $this->dbi->fetchValue($query); - - $currentUser = $this->dbi->getCurrentUser(); - - // Since editing a procedure involved dropping and recreating, check also for - // CREATE ROUTINE privilege to avoid lost procedures. - $hasEditPrivilege = (Util::currentUserHasPrivilege('CREATE ROUTINE', $db) - && $currentUser == $routineDefiner) || $this->dbi->isSuperuser(); - - // There is a problem with Util::currentUserHasPrivilege(): - // it does not detect all kinds of privileges, for example - // a direct privilege on a specific routine. So, at this point, - // we show the Execute link, hoping that the user has the correct rights. - // Also, information_schema might be hiding the ROUTINE_DEFINITION - // but a routine with no input parameters can be nonetheless executed. - - // Check if the routine has any input parameters. If it does, - // we will show a dialog to get values for these parameters, - // otherwise we can execute it directly. - - $definition = $this->dbi->getDefinition( - $db, - $routine['type'], - $routine['name'] - ); - $hasExecutePrivilege = Util::currentUserHasPrivilege('EXECUTE', $db); - $executeAction = ''; - - if ($definition !== null) { - $parser = new Parser($definition); - - /** - * @var CreateStatement $stmt - */ - $stmt = $parser->statements[0]; - - $params = Routine::getParameters($stmt); - - if ($hasExecutePrivilege) { - $executeAction = 'execute_routine'; - for ($i = 0; $i < $params['num']; $i++) { - if ($routine['type'] == 'PROCEDURE' - && $params['dir'][$i] == 'OUT' - ) { - continue; - } - $executeAction = 'execute_dialog'; - break; - } - } - } - - $hasExportPrivilege = (Util::currentUserHasPrivilege('CREATE ROUTINE', $db) - && $currentUser == $routineDefiner) || $this->dbi->isSuperuser(); - - return $this->template->render('rte/routines/row', [ - 'db' => $db, - 'table' => $table, - 'sql_drop' => $sqlDrop, - 'routine' => $routine, - 'row_class' => $rowClass, - 'has_edit_privilege' => $hasEditPrivilege, - 'has_export_privilege' => $hasExportPrivilege, - 'has_execute_privilege' => $hasExecutePrivilege, - 'execute_action' => $executeAction, - ]); - } - - /** - * Creates the contents for a row in the list of triggers - * - * @param array $trigger An array of routine data - * @param string $rowClass Additional class - * - * @return string HTML code of a cell for the list of triggers - */ - public function getTriggerRow(array $trigger, $rowClass = '') - { - global $db, $table; - - return $this->template->render('rte/triggers/row', [ - 'db' => $db, - 'table' => $table, - 'trigger' => $trigger, - 'has_drop_privilege' => Util::currentUserHasPrivilege('TRIGGER', $db), - 'has_edit_privilege' => Util::currentUserHasPrivilege('TRIGGER', $db, $table), - 'row_class' => $rowClass, - ]); - } - - /** - * Creates the contents for a row in the list of events - * - * @param array $event An array of routine data - * @param string $rowClass Additional class - * - * @return string HTML code of a cell for the list of events - */ - public function getEventRow(array $event, $rowClass = '') - { - global $db, $table; - - $sqlDrop = sprintf( - 'DROP EVENT IF EXISTS %s', - Util::backquote($event['name']) - ); - - return $this->template->render('rte/events/row', [ - 'db' => $db, - 'table' => $table, - 'event' => $event, - 'has_privilege' => Util::currentUserHasPrivilege('EVENT', $db), - 'sql_drop' => $sqlDrop, - 'row_class' => $rowClass, - ]); - } -} diff --git a/libraries/classes/Rte/Triggers.php b/libraries/classes/Rte/Triggers.php index 60cc7413bf..ddc6610380 100644 --- a/libraries/classes/Rte/Triggers.php +++ b/libraries/classes/Rte/Triggers.php @@ -1,7 +1,5 @@ dbi = $dbi; $this->export = new Export($this->dbi); $this->general = new General($this->dbi); - $this->rteList = new RteList($this->dbi); $this->words = new Words(); $this->template = new Template(); } @@ -98,14 +92,20 @@ class Triggers $items = $this->dbi->getTriggers($db, $table); $response = Response::getInstance(); + $hasDropPrivilege = Util::currentUserHasPrivilege('TRIGGER', $db); + $hasEditPrivilege = Util::currentUserHasPrivilege('TRIGGER', $db, $table); $isAjax = $response->isAjax() && empty($_REQUEST['ajax_page_request']); $rows = ''; foreach ($items as $item) { - $rows .= $this->rteList->getTriggerRow( - $item, - $isAjax ? 'ajaxInsert hide' : '' - ); + $rows .= $this->template->render('rte/triggers/row', [ + 'db' => $db, + 'table' => $table, + 'trigger' => $item, + 'has_drop_privilege' => $hasDropPrivilege, + 'has_edit_privilege' => $hasEditPrivilege, + 'row_class' => $isAjax ? 'ajaxInsert hide' : '', + ]); } echo $this->template->render('rte/triggers/list', [ @@ -238,7 +238,17 @@ class Triggers || ($trigger !== false && $table == $trigger['table']) ) { $insert = true; - $response->addJSON('new_row', $this->rteList->getTriggerRow($trigger)); + $response->addJSON( + 'new_row', + $this->template->render('rte/triggers/row', [ + 'db' => $db, + 'table' => $table, + 'trigger' => $trigger, + 'has_drop_privilege' => Util::currentUserHasPrivilege('TRIGGER', $db), + 'has_edit_privilege' => Util::currentUserHasPrivilege('TRIGGER', $db, $table), + 'row_class' => '', + ]) + ); $response->addJSON( 'name', htmlspecialchars(