DashboardHome: Check whether org home doesn't match the current one before moving a pane

When updating/creating a Dashlet, the current and the original home passed as param
might be identical, which is fine but this shouldn't cause any duplicate entry errors
This commit is contained in:
Yonas Habteab 2022-06-13 09:15:40 +02:00
parent ffbdbb9e13
commit 94d3fd2ac4

View File

@ -10,6 +10,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Model\Home; use Icinga\Model\Home;
use Icinga\Web\Dashboard\Common\BaseDashboard; use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\DashboardEntries;
use Icinga\Web\Dashboard\Common\DashboardEntry;
use Icinga\Web\Dashboard\Common\Sortable; use Icinga\Web\Dashboard\Common\Sortable;
use Icinga\Util\DBUtils; use Icinga\Util\DBUtils;
use Icinga\Web\Dashboard\Common\WidgetState; use Icinga\Web\Dashboard\Common\WidgetState;
@ -17,7 +18,7 @@ use ipl\Stdlib\Filter;
use function ipl\Stdlib\get_php_type; use function ipl\Stdlib\get_php_type;
class DashboardHome extends BaseDashboard implements Sortable class DashboardHome extends BaseDashboard implements DashboardEntry, Sortable
{ {
use DashboardEntries; use DashboardEntries;
use WidgetState; use WidgetState;
@ -125,7 +126,7 @@ class DashboardHome extends BaseDashboard implements Sortable
* *
* @return ?Pane * @return ?Pane
*/ */
public function getActivePane() public function getActivePane(): ?Pane
{ {
/** @var Pane $pane */ /** @var Pane $pane */
foreach ($this->getEntries() as $pane) { foreach ($this->getEntries() as $pane) {
@ -177,7 +178,6 @@ class DashboardHome extends BaseDashboard implements Sortable
->setPriority($pane->priority); ->setPriority($pane->priority);
$this->addEntry($newPane); $this->addEntry($newPane);
$newPane->loadDashboardEntries();
} }
if ($name !== null) { if ($name !== null) {
@ -185,11 +185,13 @@ class DashboardHome extends BaseDashboard implements Sortable
$pane = $this->getEntry($name); $pane = $this->getEntry($name);
$this->activatePane($pane); $this->activatePane($pane);
$pane->loadDashboardEntries();
} else { } else {
throw new HttpNotFoundException(t('Pane "%s" not found'), $name); throw new HttpNotFoundException(t('Pane "%s" not found'), $name);
} }
} elseif (($firstPane = $this->rewindEntries())) { } elseif (($firstPane = $this->rewindEntries())) {
$this->activatePane($firstPane); $this->activatePane($firstPane);
$firstPane->loadDashboardEntries();
} }
return $this; return $this;
@ -207,13 +209,6 @@ class DashboardHome extends BaseDashboard implements Sortable
public function manageEntry($entryOrEntries, BaseDashboard $origin = null, bool $manageRecursive = false) public function manageEntry($entryOrEntries, BaseDashboard $origin = null, bool $manageRecursive = false)
{ {
$user = Dashboard::getUser();
$conn = DBUtils::getConn();
$panes = is_array($entryOrEntries) ? $entryOrEntries : [$entryOrEntries];
// Highest priority is 0, so count($entries) are all always lowest prio + 1
$order = count($this->getEntries());
if ($origin && ! $origin instanceof DashboardHome) { if ($origin && ! $origin instanceof DashboardHome) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
__METHOD__ . ' expects parameter "$origin" to be an instance of "%s". Got "%s" instead.', __METHOD__ . ' expects parameter "$origin" to be an instance of "%s". Got "%s" instead.',
@ -222,10 +217,17 @@ class DashboardHome extends BaseDashboard implements Sortable
)); ));
} }
$user = Dashboard::getUser();
$conn = DBUtils::getConn();
$panes = is_array($entryOrEntries) ? $entryOrEntries : [$entryOrEntries];
// Highest priority is 0, so count($entries) are all always lowest prio + 1
$order = $this->countEntries();
/** @var Pane $pane */ /** @var Pane $pane */
foreach ($panes as $pane) { foreach ($panes as $pane) {
$uuid = Dashboard::getSHA1($user->getUsername() . $this->getName() . $pane->getName()); $uuid = Dashboard::getSHA1($user->getUsername() . $this->getName() . $pane->getName());
$movePane = $origin && $origin->hasEntry($pane->getName()); $movePane = $origin && $origin->hasEntry($pane->getName()) && $this->getName() !== $origin->getName();
if (! $this->hasEntry($pane->getName()) && ! $movePane) { if (! $this->hasEntry($pane->getName()) && ! $movePane) {
$conn->insert(Pane::TABLE, [ $conn->insert(Pane::TABLE, [
@ -235,6 +237,8 @@ class DashboardHome extends BaseDashboard implements Sortable
'label' => $pane->getTitle(), 'label' => $pane->getTitle(),
'priority' => $order++ 'priority' => $order++
]); ]);
$this->addEntry($pane);
} elseif (! $this->hasEntry($pane->getName()) || ! $movePane) { } elseif (! $this->hasEntry($pane->getName()) || ! $movePane) {
$filterCondition = [ $filterCondition = [
'id = ?' => $pane->getUuid(), 'id = ?' => $pane->getUuid(),
@ -275,15 +279,7 @@ class DashboardHome extends BaseDashboard implements Sortable
$pane->manageEntry($dashlets); $pane->manageEntry($dashlets);
} }
} }
}
public function toArray(bool $stringify = true): array return $this;
{
return [
'id' => $this->getUuid(),
'name' => $this->getName(),
'title' => $this->getTitle(),
'priority' => $this->getPriority()
];
} }
} }