diff options
Diffstat (limited to 'application/forms')
14 files changed, 121 insertions, 100 deletions
diff --git a/application/forms/Command/CommandForm.php b/application/forms/Command/CommandForm.php index a535c6d..49a0565 100644 --- a/application/forms/Command/CommandForm.php +++ b/application/forms/Command/CommandForm.php @@ -5,8 +5,8 @@ namespace Icinga\Module\Icingadb\Forms\Command; use ArrayIterator; +use Countable; use Exception; -use Generator; use Icinga\Application\Logger; use Icinga\Module\Icingadb\Command\IcingaCommand; use Icinga\Module\Icingadb\Command\Transport\CommandTransport; @@ -16,6 +16,8 @@ use Icinga\Web\Session; use ipl\Html\Form; use ipl\Orm\Model; use ipl\Web\Common\CsrfCounterMeasure; +use Iterator; +use IteratorIterator; use Traversable; abstract class CommandForm extends Form @@ -25,7 +27,7 @@ abstract class CommandForm extends Form protected $defaultAttributes = ['class' => 'icinga-form icinga-controls']; - /** @var mixed */ + /** @var (Traversable<Model>&Countable)|array<Model> */ protected $objects; /** @var bool */ @@ -43,7 +45,7 @@ abstract class CommandForm extends Form /** * Set the objects to issue the command for * - * @param mixed $objects A traversable that is also countable + * @param (Traversable<Model>&Countable)|array<Model> $objects A traversable that is also countable * * @return $this */ @@ -57,7 +59,7 @@ abstract class CommandForm extends Form /** * Get the objects to issue the command for * - * @return mixed + * @return (Traversable<Model>&Countable)|array<Model> */ public function getObjects() { @@ -105,11 +107,11 @@ abstract class CommandForm extends Form /** * Get the commands to issue for the given objects * - * @param Traversable<Model> $objects + * @param Iterator<Model> $objects * * @return Traversable<IcingaCommand> */ - abstract protected function getCommands(Traversable $objects): Traversable; + abstract protected function getCommands(Iterator $objects): Traversable; protected function assemble() { @@ -123,10 +125,15 @@ abstract class CommandForm extends Form protected function onSuccess() { - $errors = []; $objects = $this->getObjects(); + if (is_array($objects)) { + $objects = new ArrayIterator($objects); + } else { + $objects = new IteratorIterator($objects); + } - foreach ($this->getCommands(is_array($objects) ? new ArrayIterator($objects) : $objects) as $command) { + $errors = []; + foreach ($this->getCommands($objects) as $command) { try { $this->sendCommand($command); } catch (Exception $e) { @@ -159,21 +166,4 @@ abstract class CommandForm extends Form { (new CommandTransport())->send($command); } - - /** - * Yield the $objects the currently logged in user has the permission $permission for - * - * @param string $permission - * @param Traversable $objects - * - * @return Generator - */ - protected function filterGrantedOn(string $permission, Traversable $objects): Generator - { - foreach ($objects as $object) { - if ($this->isGrantedOn($permission, $object)) { - yield $object; - } - } - } } diff --git a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php index cf14db8..86c0bd1 100644 --- a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php +++ b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Icingadb\Command\Instance\ToggleInstanceFeatureCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ToggleInstanceFeaturesForm extends CommandForm @@ -133,7 +134,7 @@ class ToggleInstanceFeaturesForm extends CommandForm { } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { foreach ($this->features as $feature => $spec) { $featureState = $this->getElement($feature)->isChecked(); diff --git a/application/forms/Command/Object/AcknowledgeProblemForm.php b/application/forms/Command/Object/AcknowledgeProblemForm.php index 81b93e2..f5e7923 100644 --- a/application/forms/Command/Object/AcknowledgeProblemForm.php +++ b/application/forms/Command/Object/AcknowledgeProblemForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -14,9 +15,11 @@ use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -186,10 +189,13 @@ class AcknowledgeProblemForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/acknowledge-problem', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/acknowledge-problem', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new AcknowledgeProblemCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/AddCommentForm.php b/application/forms/Command/Object/AddCommentForm.php index 9cd0754..e991e84 100644 --- a/application/forms/Command/Object/AddCommentForm.php +++ b/application/forms/Command/Object/AddCommentForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -14,9 +15,11 @@ use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -141,10 +144,13 @@ class AddCommentForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/comment/add', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/comment/add', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new AddCommentCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/CheckNowForm.php b/application/forms/Command/Object/CheckNowForm.php index b7a506c..eb1e03b 100644 --- a/application/forms/Command/Object/CheckNowForm.php +++ b/application/forms/Command/Object/CheckNowForm.php @@ -4,11 +4,13 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class CheckNowForm extends CommandForm @@ -44,22 +46,17 @@ class CheckNowForm extends CommandForm ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $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; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/schedule-check', $object) + || ( + $object->active_checks_enabled + && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) + ); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleCheckCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/DeleteCommentForm.php b/application/forms/Command/Object/DeleteCommentForm.php index 25275ba..a35fc2e 100644 --- a/application/forms/Command/Object/DeleteCommentForm.php +++ b/application/forms/Command/Object/DeleteCommentForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand; 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 DeleteCommentForm extends CommandForm @@ -54,16 +56,13 @@ class DeleteCommentForm extends CommandForm ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ($this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type}); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new DeleteCommentCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/DeleteDowntimeForm.php b/application/forms/Command/Object/DeleteDowntimeForm.php index 5f695b9..b13bcc5 100644 --- a/application/forms/Command/Object/DeleteDowntimeForm.php +++ b/application/forms/Command/Object/DeleteDowntimeForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\DeleteDowntimeCommand; 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 @@ -66,19 +68,14 @@ class DeleteDowntimeForm extends CommandForm ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ( - $this->isGrantedOn('icingadb/command/downtime/delete', $object->{$object->object_type}) - && $object->scheduled_by === null - ) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $object->scheduled_by === null + && $this->isGrantedOn('icingadb/command/downtime/delete', $object->{$object->object_type}); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new DeleteDowntimeCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ProcessCheckResultForm.php b/application/forms/Command/Object/ProcessCheckResultForm.php index 5764bf8..7c612bf 100644 --- a/application/forms/Command/Object/ProcessCheckResultForm.php +++ b/application/forms/Command/Object/ProcessCheckResultForm.php @@ -4,7 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; @@ -15,6 +15,7 @@ use ipl\Html\Text; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -133,16 +134,14 @@ class ProcessCheckResultForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($this->filterGrantedOn('icingadb/command/process-check-result', $objects) as $object) { - if ($object->passive_checks_enabled) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $object->passive_checks_enabled + && $this->isGrantedOn('icingadb/command/process-check-result', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ProcessCheckResultCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/RemoveAcknowledgementForm.php b/application/forms/Command/Object/RemoveAcknowledgementForm.php index 8697985..d81fe7f 100644 --- a/application/forms/Command/Object/RemoveAcknowledgementForm.php +++ b/application/forms/Command/Object/RemoveAcknowledgementForm.php @@ -4,11 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\RemoveAcknowledgementCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -62,10 +65,13 @@ class RemoveAcknowledgementForm extends CommandForm ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/remove-acknowledgement', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/remove-acknowledgement', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new RemoveAcknowledgementCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ScheduleCheckForm.php b/application/forms/Command/Object/ScheduleCheckForm.php index 9b32ea1..bdeba53 100644 --- a/application/forms/Command/Object/ScheduleCheckForm.php +++ b/application/forms/Command/Object/ScheduleCheckForm.php @@ -4,9 +4,9 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; 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; @@ -14,8 +14,10 @@ use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -109,22 +111,17 @@ class ScheduleCheckForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $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; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/schedule-check', $object) + || ( + $object->active_checks_enabled + && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) + ); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleCheckCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ScheduleHostDowntimeForm.php b/application/forms/Command/Object/ScheduleHostDowntimeForm.php index bc21114..a09b00d 100644 --- a/application/forms/Command/Object/ScheduleHostDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleHostDowntimeForm.php @@ -4,13 +4,16 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm @@ -87,10 +90,13 @@ class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm $decorator->decorate($this->getElement('child_options')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/downtime/schedule', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { if (($childOptions = (int) $this->getValue('child_options'))) { $command = new PropagateHostDowntimeCommand(); diff --git a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php index 184a4e8..b237408 100644 --- a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -13,9 +14,11 @@ use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class ScheduleServiceDowntimeForm extends CommandForm @@ -242,10 +245,13 @@ class ScheduleServiceDowntimeForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/downtime/schedule', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleServiceDowntimeCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/SendCustomNotificationForm.php b/application/forms/Command/Object/SendCustomNotificationForm.php index dfb1e96..4b0539e 100644 --- a/application/forms/Command/Object/SendCustomNotificationForm.php +++ b/application/forms/Command/Object/SendCustomNotificationForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\SendCustomNotificationCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; @@ -12,8 +13,10 @@ use Icinga\Web\Notification; use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -108,10 +111,13 @@ class SendCustomNotificationForm extends CommandForm (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/send-custom-notification', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/send-custom-notification', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new SendCustomNotificationCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ToggleObjectFeaturesForm.php b/application/forms/Command/Object/ToggleObjectFeaturesForm.php index 50767da..63530f6 100644 --- a/application/forms/Command/Object/ToggleObjectFeaturesForm.php +++ b/application/forms/Command/Object/ToggleObjectFeaturesForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Html\FormElement\CheckboxElement; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ToggleObjectFeaturesForm extends CommandForm @@ -156,7 +158,7 @@ class ToggleObjectFeaturesForm extends CommandForm { } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { foreach ($this->features as $feature => $spec) { if ($this->getElement($feature) instanceof CheckboxElement) { @@ -169,8 +171,11 @@ class ToggleObjectFeaturesForm extends CommandForm continue; } - $granted = $this->filterGrantedOn($spec['permission'], $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object) use ($spec): bool { + return $this->isGrantedOn($spec['permission'], $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ToggleObjectFeatureCommand(); $command->setObjects($granted); |