Navigation: Accept item configuration for method addItem()

And check an item's permission.

refs #5600
This commit is contained in:
Johannes Meyer 2015-09-02 12:57:14 +02:00
parent c154f96d44
commit 0788041c43

View File

@ -8,6 +8,7 @@ use ArrayIterator;
use Countable; use Countable;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use Icinga\Authentication\Auth;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
@ -209,23 +210,36 @@ class Navigation implements ArrayAccess, Countable, IteratorAggregate
/** /**
* Add a navigation item * Add a navigation item
* *
* @param NavigationItem|array $item The item to append * If you do not pass an instance of NavigationItem, this will only add the item
* if it does not require a permission or the current user has the permission.
* *
* @return $this * @param string|NavigationItem $name The name of the item or an instance of NavigationItem
* @throws InvalidArgumentException If the item argument is invalid * @param array $properties The properties of the item to add (Ignored if $name is not a string)
*
* @return bool Whether the item was added or not
*
* @throws InvalidArgumentException In case $name is neither a string nor an instance of NavigationItem
*/ */
public function addItem(NavigationItem $item) public function addItem($name, array $properties = array())
{ {
if (! $item instanceof NavigationItem) { if (is_string($name)) {
if (! is_array($item)) { if (isset($properties['permission'])) {
throw new InvalidArgumentException( if (! Auth::getInstance()->hasPermission($properties['permission'])) {
'Argument item must be either an array or an instance of NavigationItem' return false;
); }
unset($properties['permission']);
} }
$item = new NavigationItem($item);
$item = $this->createItem($name, $properties);
} elseif (! $name instanceof NavigationItem) {
throw new InvalidArgumentException('Argument $name must be of type string or NavigationItem');
} else {
$item = $name;
} }
$this->items[] = $item; $this->items[] = $item;
return $this; return true;
} }
/** /**