Redirect properly when the currently active home/pane is removed/moved

When the currently request home/pane is being removed/moved and we're still redirecting
with the `__CLOSE__` url, the removed/moved home/pane will sill remain in the history
state and will produce http not found exceptions
This commit is contained in:
Yonas Habteab 2022-06-10 20:04:56 +02:00
parent 8685a0d247
commit e689959839
5 changed files with 54 additions and 13 deletions

View File

@ -34,10 +34,22 @@ class DashboardsController extends CompatController
/** @var Dashboard */
protected $dashboard;
/**
* Whether the currently loaded home/pane is also loaded on the previous page
*
* @var string
*/
protected $prevActive = null;
public function init()
{
parent::init();
// The "prevActive" param indicates whether this home/pane is currently being loaded in the
// DM view meanwhile rendering a modal view (update and remove actions). If it's indeed the case,
// we have to construct a proper http redirect after successfully removing this home
$this->prevActive = $this->params->shift('prevActive');
$this->dashboard = new Dashboard();
$this->dashboard->setUser($this->Auth()->getUser());
$this->dashboard->setTabs($this->getTabs());
@ -136,11 +148,16 @@ class DashboardsController extends CompatController
$homeForm = (new RemoveHomeForm($this->dashboard))
->on(RemoveHomeForm::ON_SUCCESS, function () {
$this->getResponse()
->setHeader('X-Icinga-Extra-Updates', '#menu')
->setHeader('X-Icinga-Container', 'modal-content', true);
$response = $this->getResponse();
$response->setHeader('X-Icinga-Extra-Updates', '#menu');
$this->redirectNow('__CLOSE__');
if ($this->prevActive) {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
} else {
$response->setHeader('X-Icinga-Container', 'modal-content', true);
$this->redirectNow('__CLOSE__');
}
})
->handleRequest($this->getServerRequest());
@ -179,9 +196,13 @@ class DashboardsController extends CompatController
$paneForm = (new PaneForm($this->dashboard))
->on(PaneForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
if ($this->prevActive) {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
} else {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
$this->redirectNow('__CLOSE__');
$this->redirectNow('__CLOSE__');
}
})
->handleRequest($this->getServerRequest());
@ -205,9 +226,13 @@ class DashboardsController extends CompatController
$paneForm = new RemovePaneForm($this->dashboard);
$paneForm->populate(['org_name' => $paneParam]);
$paneForm->on(RemovePaneForm::ON_SUCCESS, function () {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
if ($this->prevActive) {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
} else {
$this->getResponse()->setHeader('X-Icinga-Container', 'modal-content', true);
$this->redirectNow('__CLOSE__');
$this->redirectNow('__CLOSE__');
}
})->handleRequest($this->getServerRequest());
$this->setTitle(t('Remove Pane'));

View File

@ -152,6 +152,8 @@ class DashboardHome extends BaseDashboard implements Sortable
'home_id = ?' => $this->getUuid()
]);
$this->unsetEntry($pane);
return $this;
}

View File

@ -78,8 +78,14 @@ class DashboardHomeList extends ItemListControl
protected function createItemList(): BaseHtmlElement
{
if (! $this->headerDisabled) {
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-home')
->setParams(['home' => $this->home->getName()]);
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-home')->setParams([
'home' => $this->home->getName()
]);
if ($this->home->isActive()) {
$url->addParams(['prevActive' => true]);
}
$this->assembleHeader($url, $this->home->getTitle());
}

View File

@ -43,8 +43,14 @@ class DashboardList extends ItemListControl
protected function createItemList(): BaseHtmlElement
{
$pane = $this->pane;
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-pane')
->setParams(['home' => $pane->getHome()->getName(), 'pane' => $pane->getName()]);
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-pane')->setParams([
'home' => $pane->getHome()->getName(),
'pane' => $pane->getName()
]);
if ($this->pane->isActive()) {
$url->addParams(['prevActive' => true]);
}
$this->assembleHeader($url, $pane->getTitle());

View File

@ -89,6 +89,8 @@ class Pane extends BaseDashboard implements Sortable
'dashboard_id = ?' => $this->getUuid()
]);
$this->unsetEntry($dashlet);
return $this;
}
@ -176,7 +178,7 @@ class Pane extends BaseDashboard implements Sortable
$order = count($this->getEntries());
foreach ($dashlets as $dashlet) {
if (is_array($dashlet)) {
$this->manageEntry($dashlet, $origin);
$this->manageEntry($dashlet, $origin, $manageRecursive);
continue;
}