From 0911910856bcaf74275b6033e01e5f1d7428af0c Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Fri, 24 Aug 2018 12:55:37 +0200 Subject: [PATCH] DbObject: Don't allow empty id for multi-column objects This avoids that the user can actually create duplicate objects in the database. (E.g. for service templates) --- library/Director/Data/Db/DbObject.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index 3242fc96..f593be34 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Data\Db; +use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; use Icinga\Module\Director\Db; use Icinga\Module\Director\Exception\DuplicateKeyException; @@ -536,18 +537,22 @@ abstract class DbObject * * // TODO: may conflict with ->id * + * @throws IcingaException When key can not be calculated + * * @return string|array */ public function getId() { - // TODO: Doesn't work for array() / multicol key if (is_array($this->keyName)) { $id = array(); foreach ($this->keyName as $key) { - if (! isset($this->properties[$key])) { - return null; // Really? + if (isset($this->properties[$key])) { + $id[$key] = $this->properties[$key]; } - $id[$key] = $this->properties[$key]; + } + + if (empty($id)) { + throw new IcingaException('Could not evaluate id for multi-column object!'); } return $id;