DeleteCommentCommand: process multiple objects at once

This commit is contained in:
Alexander A. Klimov 2023-03-01 14:33:05 +01:00
parent b92eeb69e7
commit cd5f52c1bc
3 changed files with 17 additions and 45 deletions

View file

@ -4,6 +4,7 @@
namespace Icinga\Module\Icingadb\Forms\Command\Object;
use Generator;
use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Web\Notification;
@ -55,13 +56,17 @@ class DeleteCommentForm extends CommandForm
protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if ($this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) {
yield $object;
}
}
})();
if ($granted->valid()) {
$command = new DeleteCommentCommand();
$command->setCommentName($object->name);
$command->setObjects($granted);
$command->setAuthor($this->getAuth()->getUser()->getUsername());
yield $command;

View file

@ -4,49 +4,10 @@
namespace Icinga\Module\Icingadb\Command\Object;
use Icinga\Module\Icingadb\Command\IcingaCommand;
/**
* Delete a host or service comment
*/
class DeleteCommentCommand extends IcingaCommand
class DeleteCommentCommand extends ObjectsCommand
{
use CommandAuthor;
/**
* Name of the comment
*
* @var string
*/
protected $commentName;
/**
* Get the name of the comment
*
* @return string
*/
public function getCommentName(): string
{
if ($this->commentName === null) {
throw new \LogicException(
'You are accessing an unset property. Please make sure to set it beforehand.'
);
}
return $this->commentName;
}
/**
* Set the name of the comment
*
* @param string $commentName
*
* @return $this
*/
public function setCommentName(string $commentName): self
{
$this->commentName = $commentName;
return $this;
}
}

View file

@ -274,10 +274,16 @@ class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
public function renderDeleteComment(DeleteCommentCommand $command): IcingaApiCommand
{
$comments = [];
foreach ($command->getObjects() as $object) {
$comments[] = $object->name;
}
$endpoint = 'actions/remove-comment';
$data = [
'author' => $command->getAuthor(),
'comment' => $command->getCommentName()
'comments' => $comments
];
return IcingaApiCommand::create($endpoint, $data);