/** * Prepares the environment before running a test * */ protected function setUp() { $cwd = __DIR__; // read navigation config $this->_files = $cwd . '/_files'; $config = ConfigFactory::fromFile($this->_files . '/navigation.xml', true); // setup containers from config $this->_nav1 = new Navigation($config->get('nav_test1')); $this->_nav2 = new Navigation($config->get('nav_test2')); $this->_nav3 = new Navigation($config->get('nav_test3')); // setup view $view = new PhpRenderer(); $view->resolver()->addPath($cwd . '/_files/mvc/views'); // create helper $this->_helper = new $this->_helperName(); $this->_helper->setView($view); // set nav1 in helper as default $this->_helper->setContainer($this->_nav1); // setup service manager $smConfig = array('modules' => array(), 'module_listener_options' => array('config_cache_enabled' => false, 'cache_dir' => 'data/cache', 'module_paths' => array(), 'extra_config' => array('service_manager' => array('factories' => array('Config' => function () use($config) { return array('navigation' => array('default' => $config->get('nav_test1'))); }))))); $sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfig()); $sm->setService('ApplicationConfig', $smConfig); $sm->get('ModuleManager')->loadModules(); $sm->get('Application')->bootstrap(); $sm->setFactory('Navigation', 'Zend\\Navigation\\Service\\DefaultNavigationFactory'); $sm->setService('nav1', $this->_nav1); $sm->setService('nav2', $this->_nav2); $app = $this->serviceManager->get('Application'); $app->getMvcEvent()->setRouteMatch(new RouteMatch(array('controller' => 'post', 'action' => 'view', 'id' => '1337'))); }
public function protectPage(MvcEvent $event) { $match = $event->getRouteMatch(); if (!$match) { // we cannot do anything without a resolved route return; } $controller = $match->getParam('controller'); $action = $match->getParam('action'); $namespace = $match->getParam('__NAMESPACE__'); $parts = explode('\\', $namespace); $moduleNamespace = $parts[0]; $services = $event->getApplication()->getServiceManager(); $config = $services->get('config'); $auth = $services->get('auth'); $acl = $services->get('acl'); // get the role of the current user $currentUser = $services->get('user'); $role = $currentUser->getRole(); // This is how we add default acl and role to the navigation view helpers \Zend\View\Helper\Navigation\AbstractHelper::setDefaultAcl($acl); \Zend\View\Helper\Navigation\AbstractHelper::setDefaultRole($role); // check if the current module wants to use the ACL $aclModules = $config['acl']['modules']; if (!empty($aclModules) && !in_array($moduleNamespace, $aclModules)) { return; } // Get the short name of the controller and use it as resource name // Example: User\Controller\Course -> course $resourceAliases = $config['acl']['resource_aliases']; if (isset($resourceAliases[$controller])) { $resource = $resourceAliases[$controller]; } else { $resource = strtolower(substr($controller, strrpos($controller, '\\') + 1)); } // If a resource is not in the ACL add it if (!$acl->hasResource($resource)) { $acl->addResource($resource); } try { if ($acl->isAllowed($role, $resource, $action)) { return; } } catch (AclException $ex) { // @todo: log in the warning log the missing resource } // If the role is not allowed access to the resource we have to redirect the // current user to the log in page. $e = new EventManager('user'); $e->trigger('deny', $this, array('match' => $match, 'role' => $role, 'acl' => $acl)); // Set the response code to HTTP 403: Forbidden $response = $event->getResponse(); $response->setStatusCode(403); // and redirect the current user to the denied action $match->setParam('controller', 'User\\Controller\\Account'); $match->setParam('action', 'denied'); }
/** * {@inheritdoc} */ public function onBootstrap(EventInterface $e) { /** @var ApplicationInterface $app */ $app = $e->getTarget(); $serviceManager = $app->getServiceManager(); $app->getEventManager()->attach([MvcEvent::EVENT_DISPATCH, MvcEvent::EVENT_DISPATCH_ERROR], function () use($serviceManager) { /** @var Authorize $auth */ $auth = $serviceManager->get('BjyAuthorize\\Service\\Authorize'); AbstractHelper::setDefaultAcl($auth->getAcl()); AbstractHelper::setDefaultRole($auth->getIdentity()); }); }
public function setDefaultAclAndRole(MvcEvent $e) { $request = $e->getRequest(); if ($request instanceof \Zend\Console\Request) { return; } $role = \Access\Permissions\Acl\AclBuilder::ROLE_GUEST; if ($this->getAuthService()->hasIdentity()) { $role = $this->getAuthService()->getIdentity()->getRole(); } \Zend\View\Helper\Navigation\AbstractHelper::setDefaultAcl($this->getAcl()); \Zend\View\Helper\Navigation\AbstractHelper::setDefaultRole($role); }
public function onBootstrap(MvcEvent $e) { $eventManager = $e->getApplication()->getEventManager(); $eventManager->attach(new RegisterListener()); $sm = $e->getApplication()->getServiceManager(); $config = $sm->get('Config'); // Add ACL information to the Navigation view helper $authorize = $sm->get('BjyAuthorizeServiceAuthorize'); $acl = $authorize->getAcl(); $role = $authorize->getIdentity(); ZendViewHelperNavigation::setDefaultAcl($acl); ZendViewHelperNavigation::setDefaultRole($role); $this->initSession($config['User']['session']); $this->onLogOut($e); }
/** * Magic overload: Proxy to other navigation helpers or the container * * Examples of usage from a view script or layout: * <code> * // proxy to Menu helper and render container: * echo $this->navigation()->menu(); * * // proxy to Breadcrumbs helper and set indentation: * $this->navigation()->breadcrumbs()->setIndent(8); * * // proxy to container and find all pages with 'blog' route: * $blogPages = $this->navigation()->findAllByRoute('blog'); * </code> * * @param string $method helper name or method name in * container * @param array $arguments [optional] arguments to pass * @return mixed returns what the proxied call returns * @throws \Zend\View\Exception if proxying to a helper, and the * helper is not an instance of the * interface specified in * {@link findHelper()} * @throws \Zend\Navigation\Exception if method does not exist in container */ public function __call($method, array $arguments = array()) { // check if call should proxy to another helper if ($helper = $this->findHelper($method, false)) { return call_user_func_array($helper, $arguments); } // default behaviour: proxy call to container return parent::__call($method, $arguments); }
public function testSetDefaultRoleThrowsExceptionWhenGivenAnArbitraryObject() { try { Navigation\AbstractHelper::setDefaultRole(new \stdClass()); $this->fail('An invalid argument was given, but a ' . 'Zend_View_Exception was not thrown'); } catch (View\Exception\ExceptionInterface $e) { $this->assertContains('$role must be', $e->getMessage()); } }
/** * Set the View object * * @param Renderer $view * @return self */ public function setView(Renderer $view) { parent::setView($view); if ($view && $this->plugins) { $this->plugins->setRenderer($view); } return $this; }
/** * Registriert die Module aus der DB mit Zend/Auth * Setzt die Rechte der Gruppen * * @param $sm */ public function __construct($sm) { $authSessionStorage = new Session('AUTH_IDENTITY'); parent::__construct($authSessionStorage); $em = $sm->get('Doctrine\\ORM\\EntityManager'); $acl = new ZendAcl(); // add roles foreach ($em->getRepository('Auth\\Entity\\Role')->findBy(array(), array('parentId' => 'ASC')) as $role) { if ($role->parent) { $parentName = $role->parent->name; } else { $parentName = null; } $acl->addRole(new GenericRole($role->name), $parentName); } // add resources + action foreach ($em->getRepository('Auth\\Entity\\Resource')->findBy(array(), array('modul' => 'DESC')) as $resource) { $ressouceName = $resource->modul; if ($resource->action) { $ressouceName .= '/' . $resource->action; } if ($resource->subAction) { $ressouceName .= '/' . $resource->subAction; } $acl->addResource(new GenericResource($ressouceName)); } unset($ressouceName); // deny all $acl->deny(null); // add permissions foreach ($em->getRepository('Auth\\Entity\\Permission')->findAll() as $permission) { // allow $permissionName = $permission->resource->modul; if ($permission->resource->action) { $permissionName .= '/' . $permission->resource->action; } if ($permission->resource->subAction) { $permissionName .= '/' . $permission->resource->subAction; } $acl->allow($permission->gruppe->name, $permissionName); } // register identity if (!$this->hasIdentity()) { // register as gast $benutzer = new Benutzer(); $benutzer->setUsername('Unbekannter User'); $benutzer->setId(0); $benutzer->setLoggedIn(false); $gruppe = new Role(); $gruppe->id = 2; $gruppe->name = 'Gast'; $gruppe->supervisor = 0; $benutzer->setGruppe($gruppe); if (!$benutzer) { throw new \Exception('Gastbenutzer mit der ID -1 nicht vorhanden - bitte direkt in der Datenbank anlegen'); } $this->getStorage()->write($benutzer); } // register acl in navigation \Zend\View\Helper\Navigation\AbstractHelper::setDefaultAcl($acl); \Zend\View\Helper\Navigation\AbstractHelper::setDefaultRole($this->getIdentity()->getGruppe()->name); $this->acl = $acl; $this->sm = $sm; $this->em = $em; return $this; }
/** * Magic overload: Proxy to other navigation helpers or the container * * Examples of usage from a view script or layout: * <code> * // proxy to Menu helper and render container: * echo $this->navigation()->menu(); * * // proxy to Breadcrumbs helper and set indentation: * $this->navigation()->breadcrumbs()->setIndent(8); * * // proxy to container and find all pages with 'blog' route: * $blogPages = $this->navigation()->findAllByRoute('blog'); * </code> * * @param string $method helper name or method name in * container * @param array $arguments [optional] arguments to pass * @return mixed returns what the proxied call returns * @throws \Zend\View\Exception\ExceptionInterface if proxying to a helper, and the * helper is not an instance of the * interface specified in * {@link findHelper()} * @throws \Zend\Navigation\Exception\ExceptionInterface if method does not exist in container */ public function __call($method, array $arguments = array()) { // check if call should proxy to another helper $helper = $this->findHelper($method, false); if ($helper) { if ($helper instanceof ServiceLocatorAwareInterface && $this->getServiceLocator()) { $helper->setServiceLocator($this->getServiceLocator()); } return call_user_func_array($helper, $arguments); } // default behaviour: proxy call to container return parent::__call($method, $arguments); }
/** * Magic overload: Proxy calls to {@link findRelation()} or container * * Examples of finder calls: * <code> * // METHOD // SAME AS * $h->findRelNext($page); // $h->findRelation($page, 'rel', 'next') * $h->findRevSection($page); // $h->findRelation($page, 'rev', 'section'); * $h->findRelFoo($page); // $h->findRelation($page, 'rel', 'foo'); * </code> * * @param string $method method name * @param array $arguments method arguments * @throws \Zend\Navigation\Exception if method does not exist in container */ public function __call($method, array $arguments = array()) { if (@preg_match('/find(Rel|Rev)(.+)/', $method, $match)) { return $this->findRelation($arguments[0], strtolower($match[1]), strtolower($match[2])); } return parent::__call($method, $arguments); }
/** * Magic overload: Proxy calls to {@link findRelation()} or container * * Examples of finder calls: * <code> * // METHOD // SAME AS * $h->findRelNext($page); // $h->findRelation($page, 'rel', 'next') * $h->findRevSection($page); // $h->findRelation($page, 'rev', 'section'); * $h->findRelFoo($page); // $h->findRelation($page, 'rel', 'foo'); * </code> * * @param string $method * @param array $arguments * @return mixed * @throws Exception\ExceptionInterface */ public function __call($method, array $arguments = array()) { ErrorHandler::start(E_WARNING); $result = preg_match('/find(Rel|Rev)(.+)/', $method, $match); ErrorHandler::stop(); if ($result) { return $this->findRelation($arguments[0], strtolower($match[1]), strtolower($match[2])); } return parent::__call($method, $arguments); }
/** * Implements Zend_Navigation helper components. */ protected function navigationHelper() { NavigationHelper::setDefaultAcl($this->getAcl()); NavigationHelper::setDefaultRole($this->getRole()); }