Skip to content

Commit

Permalink
Fix removal of a system home doesn't work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jun 1, 2021
1 parent 20a97c8 commit a58e449
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion application/controllers/DashboardsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function indexAction()

$activeHome = $this->dashboard->getActiveHome();

if (! $this->dashboard || ! $activeHome || ! $activeHome->hasPanes()) {
if (! $activeHome || ! $activeHome->hasPanes()) {
$this->getTabs()->add('dashboard', [
'active' => true,
'title' => $this->translate('Dashboard'),
Expand Down
9 changes: 4 additions & 5 deletions application/forms/Dashboard/DashletForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,6 @@ public function updateDashlet()
return;
}

// Begin DB transaction
$db->beginTransaction();

$createNewHome = true;
if ($this->dashboard->hasHome($homeName)) {
$home = $this->dashboard->getHome($homeName);
Expand All @@ -420,8 +417,7 @@ public function updateDashlet()
$createNewHome = false;

if ($orgHome->getName() !== $home->getName()) {
$this->dashboard->loadDashboards($homeName);
$homeId = $this->dashboard->getActiveHome()->getIdentifier();
$this->dashboard->loadDashboards($home->getName());
}
}

Expand All @@ -448,6 +444,9 @@ public function updateDashlet()
$dashletDisabled = false;
}

// Begin DB transaction
$db->beginTransaction();

if (! $orgDashlet->isUserWidget()) { // System dashlets
// Since system dashlets can be edited by multiple users, we need to change
// the original id here so we don't encounter a duplicate key error
Expand Down
47 changes: 30 additions & 17 deletions application/forms/Dashboard/HomeAndPaneForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function onSuccess()

if ($this->getPopulatedValue('btn_update')) {
$newHome = $this->getPopulatedValue('home', $orgHome->getName());
$orgHomeId = $orgHome->getIdentifier();
$homeId = $orgHome->getIdentifier();

if ($pane->getOwner() === DashboardHome::DEFAULT_IW2_USER && $orgHome->getName() !== $newHome) {
Notification::error(sprintf(
Expand All @@ -228,26 +228,21 @@ public function onSuccess()
return;
}

// Begin DB transaction
$db->beginTransaction();
$createNewHome = true;
if ($this->dashboard->hasHome($newHome)) {
$home = $this->dashboard->getHome($newHome);
$homeId = $home->getIdentifier();

$homeId = $orgHomeId;
if (! $this->dashboard->hasHome($newHome) || /** prevents from failing foreign key constraint */
($this->dashboard->getHome($newHome)->getOwner() === DashboardHome::DEFAULT_IW2_USER &&
$this->dashboard->getHome($newHome)->getName() !== DashboardHome::DEFAULT_HOME)) {
$label = $newHome;
$createNewHome = false;

if ($this->dashboard->hasHome($newHome)) {
$label = $this->dashboard->getHome($newHome)->getLabel();
if ($home->getName() !== $orgHome->getName()) {
$this->dashboard->loadDashboards($home->getName());
}

$db->insert('dashboard_home', ['name' => $newHome, 'label' => $label, 'owner' => $username]);

$homeId = $db->lastInsertId();
} elseif ($orgHome->getName() !== $newHome) {
$homeId = $this->dashboard->getHome($newHome)->getIdentifier();
}

// Begin the DB transaction
$db->beginTransaction();

$paneUpdated = false;
if ($this->getPopulatedValue('enable_pane') === 'y') {
$paneUpdated = true;
Expand All @@ -268,7 +263,7 @@ public function onSuccess()
'dashboard_id = ?' => $pane->getPaneId(),
]);

} elseif ($pane->getOwner() === DashboardHome::DEFAULT_IW2_USER) { // System panes
} elseif (! $pane->isUserWidget()) { // System panes
$paneId = DashboardHome::getSHA1($username . $newHome . $pane->getName());

$db->insert('dashboard_override', [
Expand All @@ -278,6 +273,24 @@ public function onSuccess()
'label' => $this->getValue('title')
]);
} else { // Custom panes
if ($createNewHome || ($newHome !== DashboardHome::DEFAULT_HOME &&
$this->dashboard->getActiveHome()->getOwner() === DashboardHome::DEFAULT_IW2_USER)) {
$label = $newHome;

if (! $createNewHome) {
$activeHome = $this->dashboard->getActiveHome();
$label = $activeHome->getLabel();
}

$db->insert('dashboard_home', [
'name' => $newHome,
'label' => $label,
'owner' => $username
]);

$homeId = $db->lastInsertId();
}

$db->update('dashboard', [
'home_id' => $homeId,
'name' => $this->getValue('name'),
Expand Down
2 changes: 1 addition & 1 deletion test/php/library/Icinga/Web/Widget/DashboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DashboardWithPredefinableActiveName extends Dashboard
{
public $activeName = '';

public function getTabs($defaultPane = false)
public function getTabs()
{
return Mockery::mock('Icinga\Web\Widget\Tabs')
->shouldReceive('getActiveName')->andReturn($this->activeName)
Expand Down

0 comments on commit a58e449

Please sign in to comment.