From 518cdad1a803258e5713646e4e51db8ec6222639 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 18 Sep 2015 15:57:45 +0200 Subject: [PATCH] NavigationConfigForm: Automatically unshare childrens in method unshare() refs #5600 --- .../forms/Navigation/NavigationConfigForm.php | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/application/forms/Navigation/NavigationConfigForm.php b/application/forms/Navigation/NavigationConfigForm.php index 025e59fe3..fafc665a8 100644 --- a/application/forms/Navigation/NavigationConfigForm.php +++ b/application/forms/Navigation/NavigationConfigForm.php @@ -327,7 +327,7 @@ class NavigationConfigForm extends ConfigForm : ((! isset($data['users']) || !$data['users']) && (! isset($data['groups']) || !$data['groups'])) ) { // It is shared but shouldn't anymore - $config = $this->unshare($name)->config; // unshare() calls setIniConfig() + $config = $this->unshare($name, isset($data['parent']) ? $data['parent'] : null)->config; } } elseif ((isset($data['users']) && $data['users']) || (isset($data['groups']) && $data['groups'])) { if ($this->getUser()->can('application/share/navigation')) { @@ -393,12 +393,14 @@ class NavigationConfigForm extends ConfigForm * Unshare the given navigation item * * @param string $name + * @param string $parent * * @return $this * - * @throws NotFoundError In case no navigation item with the given name is found + * @throws NotFoundError In case no navigation item with the given name is found + * @throws IcingaException In case the navigation item has a parent assigned to it */ - public function unshare($name) + public function unshare($name, $parent = null) { $config = $this->getShareConfig(); if (! $config->hasSection($name)) { @@ -406,6 +408,20 @@ class NavigationConfigForm extends ConfigForm } $itemConfig = $config->getSection($name); + if ($parent === null) { + $parent = $itemConfig->parent; + } + + if ($parent && $this->hasBeenShared($parent)) { + throw new IcingaException( + 'Unable to unshare navigation item "%s". It is dependent from item "%s".' + . ' Dependent items can only be unshared by unsharing their parent', + $name, + $parent + ); + } + + $children = $this->getFlattenedChildren($name); $config->removeSection($name); $this->secondaryConfig = $config; @@ -416,6 +432,13 @@ class NavigationConfigForm extends ConfigForm $config = $owner->loadNavigationConfig(); } + foreach ($children as $child) { + $childConfig = $this->secondaryConfig->getSection($child); + unset($childConfig->owner); + $this->secondaryConfig->removeSection($child); + $config->setSection($child, $childConfig); + } + unset($itemConfig->owner); unset($itemConfig->users); unset($itemConfig->groups);