-
-
Save AntoscencoVladimir/267d0ada9124c0c7be89f5c247545dfc to your computer and use it in GitHub Desktop.
Symfony2 KnpMenu TwitterBootstrap switch user menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\BackofficeBundle\Menu; | |
use Knp\Menu\FactoryInterface; | |
use Symfony\Component\DependencyInjection\ContainerAware; | |
use Ormigo\Bundle\UserBundle\Model\User\UserQuery; | |
use Symfony\Component\Security\Core\Role\SwitchUserRole; | |
class Builder extends ContainerAware | |
{ | |
public function mainMenu(FactoryInterface $factory) | |
{ | |
$menu = $factory->createItem('root'); | |
$menu->setCurrentUri($this->container->get('request')->getRequestUri()); | |
$menu->addChild('Dashboard', array( | |
'uri' => '#', | |
)); | |
$menu->addChild('Demo View', array( | |
'route' => 'demo', | |
)); | |
return $menu; | |
} | |
public function switchUserMenu(FactoryInterface $factory) | |
{ | |
$menu = $factory->createItem('root'); | |
$menu->setAttributes(array( | |
'class' => 'pull-right', | |
'dropdown' => true, | |
'divider_append' => true, | |
)); | |
$menu->setLabel('Switch User'); | |
$securityContext = $this->container->get('security.context'); | |
if ($securityContext->isGranted('ROLE_ALLOWED_TO_SWITCH') or $securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) { | |
// The current (may be switched) username. | |
$username = $securityContext->getToken()->getUser()->getUsername(); | |
// The actual user, if switched, retrieve the correct one. | |
$actualUser = $securityContext->getToken()->getUser(); | |
foreach ($securityContext->getToken()->getRoles() as $role) { | |
if ($role instanceof SwitchUserRole) { | |
$actualUser = $role->getSource(); | |
break; | |
} | |
} | |
foreach (UserQuery::create()->find() as $eachUser) { | |
// only show links to different users | |
if ($username !== $eachUser->getUsername()) { | |
$menu->addChild($eachUser->getUsername(), array('uri' => '?_switch_user='.$eachUser->getUsername())); | |
} | |
} | |
$menu->addChild($actualUser->getUsername(), array('uri' => '?_switch_user=_exit')); | |
} | |
return $menu; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
twitter_bootstrap.menu_renderer: | |
class: Ormigo\Bundle\BackofficeBundle\Menu\TwitterBootstrapRenderer | |
arguments: | |
- @twig | |
- 'BackofficeBundle:Menu:menu_bootstrap.html.twig' | |
tags: | |
- { name: knp_menu.renderer, alias: bootstrap } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ormigo\Bundle\BackofficeBundle\Menu; | |
use Knp\Menu\ItemInterface; | |
use Knp\Menu\Renderer\TwigRenderer; | |
class TwitterBootstrapRenderer extends TwigRenderer | |
{ | |
public function render(ItemInterface $item, array $options = array()) | |
{ | |
$options = array_merge( | |
array('currentClass' => 'active'), | |
$options | |
); | |
if ('root' === $item->getName()) { | |
$item->setAttribute('class', trim('nav '.$item->getAttribute('class'))); | |
} | |
return parent::render($item, $options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment