From 4833ff109c6fcfb2dbd3b836d024ca89a0fac331 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 21 May 2015 15:01:13 +0200 Subject: [PATCH] RepositoryQuery: Validate the table passed when calling from() refs #8826 --- library/Icinga/Repository/DbRepository.php | 34 +++++++++++-------- library/Icinga/Repository/Repository.php | 19 +++++++++++ library/Icinga/Repository/RepositoryQuery.php | 1 + 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/library/Icinga/Repository/DbRepository.php b/library/Icinga/Repository/DbRepository.php index aec2a4639..a1fb800ee 100644 --- a/library/Icinga/Repository/DbRepository.php +++ b/library/Icinga/Repository/DbRepository.php @@ -74,21 +74,6 @@ abstract class DbRepository extends Repository implements Extensible, Updatable, parent::__construct($ds); } - /** - * Return the base table name this repository is responsible for - * - * This prepends the datasource's table prefix, if available and required. - * - * @return mixed - * - * @throws ProgrammingError In case no base table name has been set and - * $this->queryColumns does not provide one either - */ - public function getBaseTable() - { - return $this->prependTablePrefix(parent::getBaseTable()); - } - /** * Return the given table with the datasource's prefix being prepended * @@ -277,6 +262,25 @@ abstract class DbRepository extends Repository implements Extensible, Updatable, } } + /** + * Validate that the requested table exists + * + * @param string $table + * + * @return string The table's name, with the table prefix being prepended + * + * @throws ProgrammingError In case the given table does not exist + */ + public function requireTable($table) + { + $statementColumns = $this->getStatementColumns(); + if (! isset($statementColumns[$table])) { + $table = parent::requireTable($table); + } + + return $this->prependTablePrefix($table); + } + /** * Return this repository's query columns of the given table mapped to their respective aliases * diff --git a/library/Icinga/Repository/Repository.php b/library/Icinga/Repository/Repository.php index 9a40a3d6f..8bc6517af 100644 --- a/library/Icinga/Repository/Repository.php +++ b/library/Icinga/Repository/Repository.php @@ -589,6 +589,25 @@ abstract class Repository implements Selectable return $value; } + /** + * Validate that the requested table exists + * + * @param string $table + * + * @return string The table's name, may differ from the given one + * + * @throws ProgrammingError In case the given table does not exist + */ + public function requireTable($table) + { + $queryColumns = $this->getQueryColumns(); + if (! isset($queryColumns[$table])) { + throw new ProgrammingError('Table "%s" not found', $table); + } + + return $table; + } + /** * Recurse the given filter, require each column for the given table and convert all values * diff --git a/library/Icinga/Repository/RepositoryQuery.php b/library/Icinga/Repository/RepositoryQuery.php index dc13ec68c..bdd51f53f 100644 --- a/library/Icinga/Repository/RepositoryQuery.php +++ b/library/Icinga/Repository/RepositoryQuery.php @@ -76,6 +76,7 @@ class RepositoryQuery implements QueryInterface, Iterator */ public function from($target, array $columns = null) { + $target = $this->repository->requireTable($target); $this->query = $this->repository ->getDataSource() ->select()