Actually let ObjectsCommand derivates process multiple objects at once

This commit is contained in:
Alexander A. Klimov 2023-03-01 13:11:15 +01:00
parent 1a04927c8b
commit b92eeb69e7
10 changed files with 71 additions and 73 deletions

View file

@ -186,13 +186,11 @@ class AcknowledgeProblemForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/acknowledge-problem', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/acknowledge-problem', $objects);
if ($granted->valid()) {
$command = new AcknowledgeProblemCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setNotify($this->getElement('notify')->isChecked());

View file

@ -141,13 +141,11 @@ class AddCommentForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/comment/add', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/comment/add', $objects);
if ($granted->valid()) {
$command = new AddCommentCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());

View file

@ -4,6 +4,7 @@
namespace Icinga\Module\Icingadb\Forms\Command\Object;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Web\Notification;
@ -45,19 +46,23 @@ class CheckNowForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $this->isGrantedOn('icingadb/command/schedule-check', $object)
&& (
! $object->active_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if (
$this->isGrantedOn('icingadb/command/schedule-check', $object)
|| (
$object->active_checks_enabled
&& $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
yield $object;
}
}
})();
if ($granted->valid()) {
$command = new ScheduleCheckCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setCheckTime(time());
$command->setForced();

View file

@ -4,6 +4,7 @@
namespace Icinga\Module\Icingadb\Forms\Command\Object;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Module\Icingadb\Model\Host;
@ -136,16 +137,17 @@ class ProcessCheckResultForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $object->passive_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/process-check-result', $object)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($this->filterGrantedOn('icingadb/command/process-check-result', $objects) as $object) {
if ($object->passive_checks_enabled) {
yield $object;
}
}
})();
if ($granted->valid()) {
$command = new ProcessCheckResultCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setStatus($this->getValue('status'));
$command->setOutput($this->getValue('output'));
$command->setPerformanceData($this->getValue('perfdata'));

View file

@ -62,13 +62,11 @@ class RemoveAcknowledgementForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/remove-acknowledgement', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/remove-acknowledgement', $objects);
if ($granted->valid()) {
$command = new RemoveAcknowledgementCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setAuthor($this->getAuth()->getUser()->getUsername());
yield $command;

View file

@ -6,6 +6,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object;
use DateInterval;
use DateTime;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Module\Icingadb\Model\Host;
@ -108,19 +109,23 @@ class ScheduleCheckForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $this->isGrantedOn('icingadb/command/schedule-check', $object)
&& (
! $object->active_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if (
$this->isGrantedOn('icingadb/command/schedule-check', $object)
|| (
$object->active_checks_enabled
&& $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
yield $object;
}
}
})();
if ($granted->valid()) {
$command = new ScheduleCheckCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setForced($this->getElement('force_check')->isChecked());
$command->setCheckTime($this->getValue('check_time')->getTimestamp());

View file

@ -89,11 +89,9 @@ class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/downtime/schedule', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects);
if ($granted->valid()) {
if (($childOptions = (int) $this->getValue('child_options'))) {
$command = new PropagateHostDowntimeCommand();
$command->setTriggered($childOptions === 1);
@ -101,7 +99,7 @@ class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm
$command = new ScheduleHostDowntimeCommand();
}
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setStart($this->getValue('start')->getTimestamp());

View file

@ -244,13 +244,11 @@ class ScheduleServiceDowntimeForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/downtime/schedule', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects);
if ($granted->valid()) {
$command = new ScheduleServiceDowntimeCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setStart($this->getValue('start')->getTimestamp());

View file

@ -108,13 +108,11 @@ class SendCustomNotificationForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/send-custom-notification', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/send-custom-notification', $objects);
if ($granted->valid()) {
$command = new SendCustomNotificationCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setForced($this->getElement('forced')->isChecked());
$command->setAuthor($this->getAuth()->getUser()->getUsername());

View file

@ -158,26 +158,24 @@ class ToggleObjectFeaturesForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
foreach ($this->features as $feature => $spec) {
if ($this->getElement($feature) instanceof CheckboxElement) {
$featureState = $this->getElement($feature)->isChecked();
} else {
$featureState = $this->getElement($feature)->getValue();
}
foreach ($this->features as $feature => $spec) {
if ($this->getElement($feature) instanceof CheckboxElement) {
$state = $this->getElement($feature)->isChecked();
} else {
$state = $this->getElement($feature)->getValue();
}
if (
! $this->isGrantedOn($spec['permission'], $object)
|| $featureState === self::LEAVE_UNCHANGED
|| (int) $featureState === (int) $this->featureStatus[$feature]
) {
continue;
}
if ($state === self::LEAVE_UNCHANGED || (int) $state === (int) $this->featureStatus[$feature]) {
continue;
}
$granted = $this->filterGrantedOn($spec['permission'], $objects);
if ($granted->valid()) {
$command = new ToggleObjectFeatureCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setFeature($feature);
$command->setEnabled((int) $featureState);
$command->setEnabled((int) $state);
$this->submittedFeatures[] = $command;