From be4626482ba8761da39746a6ac60d133d3852a0f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:45:00 +0200 Subject: Merging upstream version 1.1.2. Signed-off-by: Daniel Baumann --- library/Icingadb/Common/CommandActions.php | 21 ++++++++++++++------- library/Icingadb/Common/Macros.php | 13 +++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'library/Icingadb/Common') diff --git a/library/Icingadb/Common/CommandActions.php b/library/Icingadb/Common/CommandActions.php index 2cd13fe..af7d194 100644 --- a/library/Icingadb/Common/CommandActions.php +++ b/library/Icingadb/Common/CommandActions.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Common; +use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Forms\Command\Object\AcknowledgeProblemForm; use Icinga\Module\Icingadb\Forms\Command\Object\AddCommentForm; @@ -26,7 +27,7 @@ use ipl\Web\Url; */ trait CommandActions { - /** @var Query $commandTargets */ + /** @var null|Query|array $commandTargets */ protected $commandTargets; /** @var Model $commandTargetModel */ @@ -51,14 +52,14 @@ trait CommandActions /** * Fetch command targets * - * @return Query|Model[] + * @return Query|array */ abstract protected function fetchCommandTargets(); /** * Get command targets * - * @return Query|Model[] + * @return Query|array */ protected function getCommandTargets() { @@ -73,14 +74,19 @@ trait CommandActions * Get the model of the command targets * * @return Model + * @throws HttpNotFoundException If no command targets were found */ protected function getCommandTargetModel(): Model { if (! isset($this->commandTargetModel)) { $commandTargets = $this->getCommandTargets(); - if (is_array($commandTargets) && !empty($commandTargets)) { + if (empty($commandTargets) || count($commandTargets) === 0) { + throw new HttpNotFoundException('No command targets found'); + } + + if (is_array($commandTargets)) { $this->commandTargetModel = $commandTargets[0]; - } else { + } elseif ($commandTargets->count() > 0) { $this->commandTargetModel = $commandTargets->getModel(); } } @@ -139,7 +145,8 @@ trait CommandActions protected function handleCommandForm($form) { $isXhr = $this->getRequest()->isXmlHttpRequest(); - if ($isXhr && $this->getRequest()->isApiRequest()) { + $isApi = $this->getRequest()->isApiRequest(); + if ($isXhr && $isApi) { // Prevents the framework already, this is just a fail-safe $this->httpBadRequest('Responding with JSON during a Web request is not supported'); } @@ -151,7 +158,7 @@ trait CommandActions $form->setObjects($this->getCommandTargets()); - if ($isXhr) { + if (! $isApi || $isXhr) { $this->handleWebRequest($form); } else { $this->handleApiRequest($form); diff --git a/library/Icingadb/Common/Macros.php b/library/Icingadb/Common/Macros.php index 4842c27..c7f0e15 100644 --- a/library/Icingadb/Common/Macros.php +++ b/library/Icingadb/Common/Macros.php @@ -8,7 +8,7 @@ use Icinga\Application\Logger; use Icinga\Module\Icingadb\Compat\CompatHost; use Icinga\Module\Icingadb\Compat\CompatService; use Icinga\Module\Icingadb\Model\Host; -use ipl\Orm\Model; +use Icinga\Module\Icingadb\Model\Service; use ipl\Orm\Query; use ipl\Orm\ResultSet; @@ -20,7 +20,7 @@ trait Macros * Get the given string with macros being resolved * * @param string $input The string in which to look for macros - * @param Model|CompatService|CompatHost $object The host or service used to resolve the macros + * @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros * * @return string */ @@ -42,7 +42,7 @@ trait Macros * Resolve a macro based on the given object * * @param string $macro The macro to resolve - * @param Model|CompatService|CompatHost $object The host or service used to resolve the macros + * @param Host|Service|CompatService|CompatHost $object The host or service used to resolve the macros * * @return string */ @@ -102,8 +102,13 @@ trait Macros $value = $object->$macro; } } catch (\Exception $e) { + $objectName = $object->name; + if ($objectType === 'service' && isset($object->host)) { + $objectName = $object->host->name . '!' . $objectName; + } + $value = null; - Logger::debug('Unable to resolve macro "%s". An error occurred: %s', $macro, $e); + Logger::debug('Unable to resolve macro "%s" on object "%s". An error occured: %s', $macro, $objectName, $e); } if ($value instanceof Query || $value instanceof ResultSet || is_array($value)) { -- cgit v1.2.3