Jump to content

Hide Menu Items and/or empty module: Difference between revisions

From NusaATK
No edit summary
 
No edit summary
Line 5: Line 5:


== Solution ==
== Solution ==
I found out that a module will not appear in the mainmenu if there are no menuitems added. So the solution I came up with is checking if the user has rights on the node before adding it to the menu.
It is possible to set the conditions of a node-menuItem for appearing in the menu. This is also possible for the parent menuitem in a slightly different way.


<pre>
<pre>
class mod_orders extends atkModule {
class mod_bestellingen extends atkModule {
function getMenuItems() {
function getMenuItems() {
// Get an instance of the security manager
// securtiyConditions is a 1 dimensional array containing
$securityManager = &atkGetSecurityManager();
// pairs of nodename and the right the user should have for it
// to appear in the menu.
$securityConditions = Array(
"bestellingen.orders","admin",
"bestellingen.items","admin"
);


// An array with all the nodes in this module
$this->menuitem("bestellingen","","",$securityConditions);
$nodes = Array("order","items");
// The hasAccess variabele indicates if the logged in user
// has access to any of the nodes in this module.
$hasAccess = false; // Default is no access
// If the user does have access to one of the nodes
// the menu items are added. The securityManager takes care of hiding
// specific nodes bases on the rights the user has.
foreach($nodes AS $nodeName) {
if($securityManager->allowed("orders",$nodeName)) {
// Accessible item found
$hasAccess = true;
break;
}
}
if($hasAccess) {
$this->menuitem("orders");
// Loop through $nodes and add them to the menu
$this->menuitem("orders", dispatch_url("bestellingen.orders", "admin"), "bestellingen", Array("bestellingen.orders","admin"));
// this is just an example. In the real world
$this->menuitem("items", dispatch_url("bestellingen.items", "admin"), "bestellingen", Array("bestellingen.items","admin"));
// you probably would want different kind of minimal rights
// and perhaps a different default view.
foreach($nodes AS $nodeName) {
$this->menuitem("order", dispatch_url("orders.".$nodeName, "admin"), "orders", Array("orders.".$nodeName,"admin"));
}
}
}
}
</pre>
</pre>


This is just an example of the getMenuItems function. If you use this you should of course also have a getNodes function with the proper rights.
This is just an example of the getMenuItems function. If you use this you should of course also have a getNodes function with the proper rights.

Revision as of 12:40, 26 June 2006

ATK Howto: Hide Menu Items and/or empty module

Complexity: Advanced
Author: Meinaart van Straalen <meinaart@zicht.nl>

List of other Howto's

Issue

If you use the built-in security features for ATK you might have come accros the following scenario. If an user doesn't have rights on any of the nodes inside a module the module still appears in the main menu. This default behaviour is a little bit strange because when you click on a "empty" module you will only get the "back to Main menu" link. In this HowTo I will describe how you can hide a module from the main menu if there are no accessible nodes.

Solution

It is possible to set the conditions of a node-menuItem for appearing in the menu. This is also possible for the parent menuitem in a slightly different way.

class mod_bestellingen extends atkModule {
	function getMenuItems() {
		// securtiyConditions is a 1 dimensional array containing
		// pairs of nodename and the right the user should have for it
		// to appear in the menu. 
		$securityConditions = Array(
			"bestellingen.orders","admin",
			"bestellingen.items","admin"
		);

		$this->menuitem("bestellingen","","",$securityConditions);
			
		$this->menuitem("orders", dispatch_url("bestellingen.orders", "admin"), "bestellingen", Array("bestellingen.orders","admin"));
		$this->menuitem("items", dispatch_url("bestellingen.items", "admin"), "bestellingen", Array("bestellingen.items","admin"));
	}

This is just an example of the getMenuItems function. If you use this you should of course also have a getNodes function with the proper rights.