Skip to content

Commit

Permalink
Channel: Don't allow to delete channel if in use
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jun 24, 2024
1 parent 1ddff50 commit 69d8c7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
27 changes: 26 additions & 1 deletion application/forms/ChannelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -111,14 +114,36 @@ 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',
'delete',
[
'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
]
);

Expand Down
8 changes: 8 additions & 0 deletions public/css/form.less
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@
.source-form textarea {
.monospace-font();
}

.channel-form {
.btn-remove:disabled {
background: @gray-light;
color: @disabled-gray;
border-color: transparent;
}
}

0 comments on commit 69d8c7c

Please sign in to comment.