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 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'library/Icingadb/Common/CommandActions.php') 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); -- cgit v1.2.3