From b18bc644404e02b57635bfcc8258e85abb141146 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:44:46 +0200 Subject: Adding upstream version 1.1.1. Signed-off-by: Daniel Baumann --- application/forms/Command/CommandForm.php | 179 ++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 application/forms/Command/CommandForm.php (limited to 'application/forms/Command/CommandForm.php') diff --git a/application/forms/Command/CommandForm.php b/application/forms/Command/CommandForm.php new file mode 100644 index 0000000..a535c6d --- /dev/null +++ b/application/forms/Command/CommandForm.php @@ -0,0 +1,179 @@ + 'icinga-form icinga-controls']; + + /** @var mixed */ + protected $objects; + + /** @var bool */ + protected $isApiTarget = false; + + /** + * Whether an error occurred while sending the command + * + * Prevents the success message from being rendered simultaneously + * + * @var bool + */ + protected $errorOccurred = false; + + /** + * Set the objects to issue the command for + * + * @param mixed $objects A traversable that is also countable + * + * @return $this + */ + public function setObjects($objects): self + { + $this->objects = $objects; + + return $this; + } + + /** + * Get the objects to issue the command for + * + * @return mixed + */ + public function getObjects() + { + return $this->objects; + } + + /** + * Set whether this form is an API target + * + * @param bool $state + * + * @return $this + */ + public function setIsApiTarget(bool $state = true): self + { + $this->isApiTarget = $state; + + return $this; + } + + /** + * Get whether this form is an API target + * + * @return bool + */ + public function isApiTarget(): bool + { + return $this->isApiTarget; + } + + /** + * Create and add form elements representing the command's options + * + * @return void + */ + abstract protected function assembleElements(); + + /** + * Create and add a submit button to the form + * + * @return void + */ + abstract protected function assembleSubmitButton(); + + /** + * Get the commands to issue for the given objects + * + * @param Traversable $objects + * + * @return Traversable + */ + abstract protected function getCommands(Traversable $objects): Traversable; + + protected function assemble() + { + $this->assembleElements(); + + if (! $this->isApiTarget()) { + $this->assembleSubmitButton(); + $this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId())); + } + } + + protected function onSuccess() + { + $errors = []; + $objects = $this->getObjects(); + + foreach ($this->getCommands(is_array($objects) ? new ArrayIterator($objects) : $objects) as $command) { + try { + $this->sendCommand($command); + } catch (Exception $e) { + Logger::error($e->getMessage()); + $errors[] = $e->getMessage(); + } + } + + if (! empty($errors)) { + if (count($errors) > 1) { + Notification::warning( + t('Some commands were not transmitted. Please check the log. The first error follows.') + ); + } + + $this->errorOccurred = true; + + Notification::error($errors[0]); + } + } + + /** + * Transmit the given command + * + * @param IcingaCommand $command + * + * @return void + */ + protected function sendCommand(IcingaCommand $command) + { + (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; + } + } + } +} -- cgit v1.2.3