Reputation: 1980
I'm using Sonata Admin Bundle and I'm having some troubles with the function:
function configureRoutes(RouteCollection $collection){ }
The function works correctly if I write a simple:
$collection->remove('create');
But what I'm triying to do is to remove some routes depending on the role of the user connected, so I tried:
$seguridad = $this->getConfigurationPool()->getContainer()->get('security.context');
if ($seguridad->getToken()->getUser() != "anon."){
if ( !$seguridad->isGranted('ROLE_ADMIN') ) {
$collection->remove('create');
}
}
But logged as Admin I get the error: An exception has been thrown during the rendering of a template ("Route "admin_sademer_core_MIENTITY_create" does not exist.") in SonataAdminBundle:Block:block_admin_list.html.twig at line 31.
It's like in some moment the Sonata remove the route create I before enter in the dashboard, and then when I enter in the dashboard I try to enter in the route create but the route is already removed. But I'm not sure what is happening.
Any idea? Thanks a lot!
Upvotes: 4
Views: 1936
Reputation: 121
I just did it different way, Just overridden isGranted() in entitAdmin.php file. Then you can check permission depending on user roles. But it served our purpose. If we want to use $collection->remove('create'), we can't do that since no way to call the method ("configureRoutes()") in the application run time. Only calling that method ("configureRoutes()") while cache is creating.
/**
* {@inheritdoc}
*/
public function isGranted($name, $object = null)
{
$user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
switch ($name) {
case "CREATE":
if (!$user->hasRole("ROLE_ADMIN")) {
return false;
}
default:
return true;
}
}
Upvotes: 2