From 5d769c130b57fcd35d9c3a43e5d480ff7431c22e Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 18 Sep 2024 09:34:02 +0200 Subject: [PATCH] downtimes: Support automated removal --- .../controllers/DowntimesController.php | 70 ++++++++++++------- .../Command/Object/DeleteDowntimeForm.php | 4 -- doc/09-Automation.md | 10 +++ .../Icingadb/Widget/Detail/DowntimeDetail.php | 1 - 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/application/controllers/DowntimesController.php b/application/controllers/DowntimesController.php index c045ffb45..448f1de5f 100644 --- a/application/controllers/DowntimesController.php +++ b/application/controllers/DowntimesController.php @@ -5,6 +5,7 @@ namespace Icinga\Module\Icingadb\Controllers; use GuzzleHttp\Psr7\ServerRequest; +use Icinga\Module\Icingadb\Common\CommandActions; use Icinga\Module\Icingadb\Common\Links; use Icinga\Module\Icingadb\Forms\Command\Object\DeleteDowntimeForm; use Icinga\Module\Icingadb\Model\Downtime; @@ -13,12 +14,16 @@ use Icinga\Module\Icingadb\Widget\ItemList\DowntimeList; use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher; use Icinga\Module\Icingadb\Widget\ShowMore; +use ipl\Orm\Model; +use ipl\Stdlib\Filter; use ipl\Web\Control\LimitControl; use ipl\Web\Control\SortControl; use ipl\Web\Url; class DowntimesController extends Controller { + use CommandActions; + public function indexAction() { $this->addTitleTab(t('Downtimes')); @@ -109,34 +114,9 @@ public function indexAction() public function deleteAction() { + $this->assertIsGrantedOnCommandTargets('icingadb/command/downtime/delete'); $this->setTitle(t('Cancel Downtimes')); - - $db = $this->getDb(); - - $downtimes = Downtime::on($db)->with([ - 'host', - 'host.state', - 'service', - 'service.host', - 'service.host.state', - 'service.state' - ]); - - $this->filter($downtimes); - - $form = (new DeleteDowntimeForm()) - ->setObjects($downtimes) - ->setRedirectUrl(Links::downtimes()->getAbsoluteUrl()) - ->on(DeleteDowntimeForm::ON_SUCCESS, function ($form) { - // This forces the column to reload nearly instantly after the redirect - // and ensures the effect of the command is visible to the user asap - $this->getResponse()->setAutoRefreshInterval(1); - - $this->redirectNow($form->getRedirectUrl()); - }) - ->handleRequest(ServerRequest::fromGlobals()); - - $this->addContent($form); + $this->handleCommandForm(DeleteDowntimeForm::class); } public function detailsAction() @@ -200,4 +180,40 @@ public function searchEditorAction() $this->getDocument()->add($editor); $this->setTitle(t('Adjust Filter')); } + + protected function getCommandTargetsUrl(): Url + { + return Url::fromPath('__CLOSE__'); + } + + protected function fetchCommandTargets() + { + $downtimes = Downtime::on($this->getDb())->with([ + 'host', + 'host.state', + 'service', + 'service.host', + 'service.host.state', + 'service.state' + ]); + + $this->filter($downtimes); + + return $downtimes; + } + + public function isGrantedOn(string $permission, Model $object): bool + { + if ($object->scheduled_by !== null) { + return false; + } + + return parent::isGrantedOn($permission, $object->{$object->object_type}); + } + + public function isGrantedOnType(string $permission, string $type, Filter\Rule $filter, bool $cache = true): bool + { + return parent::isGrantedOnType($permission, 'host', $filter, $cache) + || parent::isGrantedOnType($permission, 'service', $filter, $cache); + } } diff --git a/application/forms/Command/Object/DeleteDowntimeForm.php b/application/forms/Command/Object/DeleteDowntimeForm.php index b13bcc524..79165d2c8 100644 --- a/application/forms/Command/Object/DeleteDowntimeForm.php +++ b/application/forms/Command/Object/DeleteDowntimeForm.php @@ -9,15 +9,12 @@ use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Orm\Model; -use ipl\Web\Common\RedirectOption; use ipl\Web\Widget\Icon; use Iterator; use Traversable; class DeleteDowntimeForm extends CommandForm { - use RedirectOption; - protected $defaultAttributes = ['class' => 'inline']; public function __construct() @@ -38,7 +35,6 @@ public function __construct() protected function assembleElements() { - $this->addElement($this->createRedirectOption()); } protected function assembleSubmitButton() diff --git a/doc/09-Automation.md b/doc/09-Automation.md index 1c5ac4ac3..f993482ef 100644 --- a/doc/09-Automation.md +++ b/doc/09-Automation.md @@ -228,6 +228,16 @@ None. | hours | y | Number | flexible | | minutes | y | Number | flexible | +### Delete Downtimes + +#### Routes + +* icingadb/downtimes/delete + +#### Options + +None. + ### Send Custom Notification #### Routes diff --git a/library/Icingadb/Widget/Detail/DowntimeDetail.php b/library/Icingadb/Widget/Detail/DowntimeDetail.php index 9e50f7f26..6a3902dc4 100644 --- a/library/Icingadb/Widget/Detail/DowntimeDetail.php +++ b/library/Icingadb/Widget/Detail/DowntimeDetail.php @@ -58,7 +58,6 @@ protected function createCancelDowntimeForm() return (new DeleteDowntimeForm()) ->setObjects([$this->downtime]) - ->populate(['redirect' => '__BACK__']) ->setAction($action->getAbsoluteUrl()); }