Hide Menu Items and/or empty module
From Achievo/ATK Wiki
|
ATK Howto: Hide Menu Items and/or empty module
|
Contents |
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.
Changed title because this also covers how to hide menu items for which the User does not have rights
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"); // Parent menu item $this->menuitem("bestellingen", "", "main", $securityConditions); // Add child items, only visible if parent menu item is visible $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")); } }
NOTE: The 4th parameter of function menuitem is $enable as defined at http://www.ibuildings.com/docs/atk/trunk/atk/modules/atkModule.html#methodmenuitem and causes menuitem to check the user privileges. The menu item is not displayed if user does not have rights to the node/action
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.
Another solution
I found out that a module will not appear in the mainmenu if there are no menuitems added. So another solution I came up with is checking if the user has rights on the node before adding it to the menu. You could also use your own custom ways to determine if a menuitem should be added. The example below does exactly the same as the example code above but is much more customizeable.
class mod_orders extends atkModule { function getMenuItems() { // Get an instance of the security manager $securityManager = &atkGetSecurityManager(); // An array with all the nodes in this module $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 is just an example. In the real world // 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")); } } } }
And another Solution
It is possible to set whether or not a menu item appears based on User name
class mod_custom extends atkModule { function getMenuItems() { global $g_user; if ($g_user['name']=='administrator') { $this->menuitem("Charge_Detail", dispatch_url("cms.chargesummary", "admin")); } }