Reputation: 375
I have been using a fos user Bundle where each of user is assigned to a group and each group is provided with a specific role as per fos user bundle everything works fine.
Now to make a menu system I am trying to use Knp menu bundle.Now to make menu structure I want to pass roles of each group to menu system(dynamically).So that changing role of specific group can allow the menu system to change dynamically.
I have already configured menu bundle as per documentation
here I have added a class named menu builder inside namespace Admin\Bundle\HomeBundle\Menu; Now I need to call group roles of current logged in user and add them to menu dynamically also i need to make some of these roles to sub menu within same main menu. Please improve me if I am on wrong way (if any) and process how I can dynamically include roles of group to menu using Knp menu bundle as a sevice.
Thanks in advance.
Upvotes: 3
Views: 2283
Reputation: 2292
I find out another solution in Symfony 3. You can get security.authorization_checker
from the container and use it in menu
namespace AppBundle\Menu;
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class Builder implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function topHeaderMenu(FactoryInterface $factory, array $options)
{
$checker = $this->container->get('security.authorization_checker');
$menu = $factory->createItem('root');
$menu->setChildrenAttribute('class','links-top list-inline');
if ($checker->isGranted('ROLE_ADMIN')) {
$menu->addChild('admin panel', array('route' => 'admin'));
}
if ($checker->isGranted('ROLE_USER')) {
$menu->addChild('account', array('route' => 'login'));
$menu->addChild('logout', array('route' => 'logout'));
} else {
$menu->addChild('registration', array('route' => 'registration'));
$menu->addChild('login', array('route' => 'login'));
}
return $menu;
}
}
Upvotes: 3
Reputation: 11
In Symfony3 the best and easiest solution for me was in MenuBuilder.php :
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class MenuBuilder implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function __construct(FactoryInterface $factory, AuthorizationCheckerInterface $authorizationChecker)
{
$this->factory = $factory;
$this->checker = $authorizationChecker;
}
public function mainMenu(array $options)
{
$menu = $this->factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('Blog', array('route' => 'blog_homepage'));
$menu['Blog']->addChild('About', array('route' => 'blog_about'));
$menu['Blog']->addChild('Contact Us', array('route' => 'blog_contact'));
if($this->checker->isGranted('ROLE_ADMIN')) {
$menu->addChild('Admin', array('route' => 'sonata_admin_dashboard'));
}
$menu->addChild('User', array('route' => 'fos_user_profile_show'));
$menu['User']->addChild('Register', array('route' => 'fos_user_registration_register'));
$menu['User']->addChild('Change password', array('route' => 'fos_user_change_password'));
$menu['User']->addChild('Log Out', array('route' => 'fos_user_security_logout'));
// ... add more children
return $menu;
}
}
Donf forget to add "@security.authorization_checker" to arguments in your service
Upvotes: 1