initial setup of framework code
[cacert-mgr.git] / manager / library / plugins / plugin.buildmenu.php
1 <?php
2
3 /**
4 * this plugin tries to add modules to the top navigation depending on the user
5 * which is logged in and the required permissions needed (provided by the action modules)
6 *
7 * @author markus
8 * $Id: plugin.buildmenu.php 95 2010-03-19 14:14:39Z markus $
9 */
10 class BuildMenu extends Zend_Controller_Plugin_Abstract {
11 public function preDispatch(Zend_Controller_Request_Abstract $request) {
12 $session = Zend_Registry::get('session');
13 if (!isset($session->authdata) || !isset($session->authdata['authed']) || $session->authdata['authed'] === false)
14 return;
15
16 $cur_ctrl = $request->getControllerName();
17 $cur_action = $request->getActionName();
18
19 $view = Zend_Registry::get('view');
20
21 if (is_dir(FWACTIONS_PATH)) {
22 $dir = opendir(FWACTIONS_PATH);
23
24 while (($file = readdir($dir)) !== false) {
25 if ($file == '.' || $file == '..')
26 continue;
27 if (preg_match('/^Action([a-zA-Z0-9_]*)\.php/', $file, $match)) {
28 $path = FWACTIONS_PATH . '/' . $file;
29 require_once($path);
30
31 $r = new ReflectionClass($match[1]);
32
33 if ($r->isSubclassOf('FWAction')) {
34 /**
35 * match Actions permission with the permissions of the currently logged in user,
36 * add to menu if user has access to that action
37 */
38
39 $required = $r->getMethod('getRequiredPermissions')->invoke(null);
40 $menuprio = $r->getMethod('getTopNavPrio')->invoke(null);
41 $ctrl = $r->getMethod('getController')->invoke(null);
42 $action = $r->getMethod('getAction')->invoke(null);
43 $text = $r->getMethod('getMenutext')->invoke(null);
44 $role = $session->authdata['authed_role'];
45
46 if ($cur_ctrl == $ctrl) # && $cur_action == $action)
47 $aclass = ' class="active"';
48 else
49 $aclass = '';
50
51 $acl = $session->authdata['authed_permissions'];
52 if (is_array($required) && count($required) == 0) {
53 $view->topNav('<a href="' .
54 $view->url(array('controller' => $ctrl, 'action' => $action), 'default', true) .
55 '"' . $aclass . '>' . I18n::_($text) . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, $menuprio);
56 }
57 else {
58 foreach ($required as $rperm) {
59 if ($acl->has($rperm) && $acl->isAllowed($role, $rperm, 'view')) {
60 $view->topNav('<a href="' .
61 $view->url(array('controller' => $ctrl, 'action' => $action), 'default', true) .
62 '"' . $aclass . '>' . I18n::_($text) . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, $menuprio);
63 break; // exit on first match
64 }
65 }
66 }
67 }
68 }
69 }
70
71 closedir($dir);
72 }
73 }
74 }