/** * Overridden to be displayed if at least one of the children menus * should be displayed * * @see ioMenuItem */ public function checkUserAccess(sfBasicSecurityUser $user = null) { $normalAccess = parent::checkUserAccess($user); // if we have no children, then just behave normally. This behavior // is intended to hide parents who have no children. if (count($this->getChildren()) == 0) { return $normalAccess; } // if this item is normally accessible, then it still should be if (!$normalAccess) { return false; } // if any of the children are accessible, then this should be also foreach ($this->getChildren() as $child) { if ($child->checkUserAccess($user)) { return true; } } return false; }
$userMenu = new ioMenuItem('user menu'); $user = new sfBasicSecurityUser($configuration->getEventDispatcher(), new sfNoStorage()); $t->is($userMenu->checkUserAccess($user), true, '->checkUserAccess() returns true for a menu with no restrictions.'); $userMenu->requiresAuth(true); $t->is($userMenu->checkUserAccess($user), false, '->checkUserAccess() returns false if the menu requires auth but the user is not authenticated.'); $user->setAuthenticated(true); $t->is($userMenu->checkUserAccess($user), true, '->checkUserAccess() returns true if the menu requires auth and the user is authenticated.'); $userMenu->requiresNoAuth(true); $userMenu->requiresAuth(false); $t->is($userMenu->checkUserAccess($user), false, '->checkUserAccess() returns false if the menu requires NO auth and the user is authenticated.'); $user->setAuthenticated(false); $t->is($userMenu->checkUserAccess($user), true, '->checkUserAccess() returns true if the menu requires NO auth and the user is NOT authenticated.'); $userMenu = new ioMenuItem('user menu'); $userMenu->setCredentials(array('c1', 'c2')); $user->addCredential('c1'); $t->is($userMenu->checkUserAccess($user), false, '->checkUserAccess() returns false when the menu requires a credential the user does not have.'); $user->addCredential('c2'); $t->is($userMenu->checkUserAccess($user), true, '->checkUserAccess() returns true when the menu requires credentials but the user has those credentials.'); $user->removeCredential('c2'); $userMenu->setCredentials(array(array('c1', 'c2'))); $t->is($userMenu->checkUserAccess($user), true, '->checkUserAccess() supports the nesting of credentials to handle OR logic.'); $t->info(' 4.2 - Test actsLikeFirst(), actsLikeLast()'); $user->setAuthenticated(false); $userMenu = new ioMenuItemTest('user menu'); $t->info(' a) Create 3 normal children and as the actsLike methods on them'); $userMenu->addChild('ch1'); $userMenu->addChild('ch2'); $userMenu->addChild('ch3'); $t->is($userMenu['ch1']->actsLikeFirst(), true, '->actsLikeFirst() returns true for the first child.'); $t->is($userMenu['ch2']->actsLikeFirst(), false, '->actsLikeFirst() returns false for the second child.'); $t->is($userMenu['ch3']->actsLikeLast(), true, '->actsLikeLast() returns true for the third child.');