Skip to content

Commit

Permalink
Tidies up the way the menu instance uses the credentials to check if …
Browse files Browse the repository at this point in the history
…an item or it's children should be added
  • Loading branch information
catchamonkey committed Sep 23, 2011
1 parent 364fc21 commit 8d59b39
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
50 changes: 18 additions & 32 deletions lib/sfNavBuilder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,28 @@ public function setUser(sfUser $u)
*/
public function addItem(sfNavBuilderItem $item)
{
if (count($item->getCredentialRules()) > 0)
// pass in the request instance and context to the item
$item->setRequest($this->_request)->setContext($this->_context);
// if the item can be added, then add it
if ($item->canBeAdded())
{
$hasPermission = FALSE;
}
else
{
$hasPermission = TRUE;
}
// the item must have one defined credential (at least)
foreach ($item->getCredentialRules() as $credential)
{
// if the permission is now found
if ($this->_context->user->hasCredential($credential))
$this->_menu[$item->getDisplayName()] = $item;
// mark the active state of the parent element
$item->setActive($this->isItemActive($item));
// mark the active state of any children elements
foreach ($item->getChildren() as $child)
{
// break out as they only have to have one
$hasPermission = TRUE;
break;
// the child also needs the request and context instances
$child->setRequest($this->_request)->setContext($this->_context);
$child->setActive($this->isItemActive($child));
// if this item should not be part of this users menu instance
if (!$child->canBeAdded())
{
// remove it
$item->remove($child->getDisplayName());
}
}
}
if (!$hasPermission)
{
return $this;
}
// pass in the request instance to the item
$item->setRequest($this->_request);
// add this item to the menu
$this->_menu[$item->getDisplayName()] = $item;
// mark the active state of the parent element
$item->setActive($this->isItemActive($item));
// mark the active state of any children elements
foreach ($item->getChildren() as $child)
{
// the child also needs the request instance
$child->setRequest($this->_request);
$child->setActive($this->isItemActive($child));
}
return $this;
}

Expand Down
48 changes: 47 additions & 1 deletion lib/sfNavBuilderItem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class sfNavBuilderItem
private $_children;
private $_attributes;
private $_request;
private $_context;
private $_route;
private $_routeParams;
private $_persistableRouteParams;
Expand All @@ -28,6 +29,7 @@ public function __construct()
$this->_children = array();
$this->_attributes = array();
$this->_request = FALSE;
$this->_context = FALSE;
$this->_route = FALSE;
$this->_routeParams = array();
$this->_persistableRouteParams = array();
Expand Down Expand Up @@ -110,7 +112,7 @@ public function setActive($b)
*/
public function addChild(sfNavBuilderItem $i)
{
$this->_children[] = $i;
$this->_children[$i->_displayName] = $i;
return $this;
}

Expand Down Expand Up @@ -150,6 +152,17 @@ public function setRequest(sfWebRequest $r)
return $this;
}

/**
* Sets the context of this menu
* @param StdClass $c The Context items defined as a StdClass
* @return sfNavBuilderItem $this The current sfNavBuilderItem instance
*/
public function setContext(StdClass $c)
{
$this->_context = $c;
return $this;
}

/**
* Sets the route to use for generatiion of the URL for this item
* @param String $r The name of the route
Expand Down Expand Up @@ -223,6 +236,15 @@ public function getDisplayName()
return $this->_displayName;
}

/**
* Removes a child item from the current Item
* @param String $name The Display name of the child to remove
*/
public function removeChild($name)
{
unset($this->_children[$name]);
}

/**
* Returns the URL of this item, will also run the generation of the URL
* if _route has been defined
Expand Down Expand Up @@ -261,6 +283,30 @@ public function isActive()
return $this->_isActive;
}

public function canBeAdded()
{
// be defensive
$hasPermission = FALSE;
// if no credential based rules are defined
if (count($this->_credentialRules) == 0)
{
// grant permission to this item
$hasPermission = TRUE;
}
// the user must have one of the defined credentials (at least)
foreach ($this->_credentialRules as $credential)
{
// if the permission is now found
if ($this->_context->user->hasCredential($credential))
{
// break out as they only have to have one
$hasPermission = TRUE;
break;
}
}
return $hasPermission;
}

/**
* Returns any children defined for this item
* @return Array $_children, Will be an empty array if no children are defined
Expand Down

0 comments on commit 8d59b39

Please sign in to comment.