From 69d8c7cd887cc5a5269793975f1860b8ae7d5212 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 21 Jun 2024 11:18:15 +0200 Subject: [PATCH] Channel: Don't allow to delete channel if in use --- application/forms/ChannelForm.php | 27 ++++++++++++++++++++++++++- public/css/form.less | 8 ++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/application/forms/ChannelForm.php b/application/forms/ChannelForm.php index 7bb56455a..45f5c915d 100644 --- a/application/forms/ChannelForm.php +++ b/application/forms/ChannelForm.php @@ -7,6 +7,8 @@ use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\Model\AvailableChannelType; +use Icinga\Module\Notifications\Model\Contact; +use Icinga\Module\Notifications\Model\RuleEscalationRecipient; use Icinga\Web\Session; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\FormElement\BaseFormElement; @@ -52,6 +54,7 @@ public function __construct(Connection $db) protected function assemble() { + $this->addAttributes(['class' => 'channel-form']); $this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId())); $this->addElement( @@ -111,6 +114,24 @@ protected function assemble() ); if ($this->channelId !== null) { + $isInUse = Contact::on($this->db) + ->columns('1') + ->filter(Filter::all( + Filter::equal('default_channel_id', $this->channelId), + Filter::equal('deleted', 'n') + )) + ->count() > 0; + + if (! $isInUse) { + $isInUse = RuleEscalationRecipient::on($this->db) + ->columns('1') + ->filter(Filter::all( + Filter::equal('channel_id', $this->channelId), + Filter::equal('deleted', 'n') + )) + ->count() > 0; + } + /** @var FormSubmitElement $deleteButton */ $deleteButton = $this->createElement( 'submit', @@ -118,7 +139,11 @@ protected function assemble() [ 'label' => $this->translate('Delete'), 'class' => 'btn-remove', - 'formnovalidate' => true + 'formnovalidate' => true, + 'disabled' => $isInUse, + 'title' => $isInUse + ? $this->translate('Cannot delete channel, contacts or event rules are using it') + : null ] ); diff --git a/public/css/form.less b/public/css/form.less index ca2a24062..c9626b665 100644 --- a/public/css/form.less +++ b/public/css/form.less @@ -108,3 +108,11 @@ .source-form textarea { .monospace-font(); } + +.channel-form { + .btn-remove:disabled { + background: @gray-light; + color: @disabled-gray; + border-color: transparent; + } +}