diff options
Diffstat (limited to 'library/Icingadb/Common')
-rw-r--r-- | library/Icingadb/Common/CommandActions.php | 21 | ||||
-rw-r--r-- | library/Icingadb/Common/Macros.php | 13 |
2 files changed, 23 insertions, 11 deletions
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<Model> $commandTargets */ protected $commandTargets; /** @var Model $commandTargetModel */ @@ -51,14 +52,14 @@ trait CommandActions /** * Fetch command targets * - * @return Query|Model[] + * @return Query|array<Model> */ abstract protected function fetchCommandTargets(); /** * Get command targets * - * @return Query|Model[] + * @return Query|array<Model> */ 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)) { |