我在de OnKernelRequest中面临一个错误,但这很奇怪:
我有3个角色:ROLE_USER - > ROLE_ADMIN - > ROLE_SUPER_ADMIN(每个角色都有前面的角色)。
在我的APP中,我有维护模式和加载模式(取决于数据库中的参数)。我已经覆盖了OnKernelRequest,看看APP是否处于维护/加载模式,如果用户未被授予ROLE_SUPER_ADMIN,则渲染维护视图(这很完美)
现在我想添加一些条件。条件是即使用户被授予ROLE_SUPER_ADMIN,在某些URL中,如果APP处于加载模式,APP将呈现维护视图(这不起作用并打破APP)。但奇怪的是,我在每种情况下使用完全相同的代码重定向......
我看不到错误,因为APP在发生错误情况时会断开并且不会连接。
我的security.yml代码:
security:
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory:
users:
guest:
password: ....
roles: 'ROLE_USER'
admin:
password: ....
roles: 'ROLE_ADMIN'
superadmin:
password: ....
roles: 'ROLE_SUPER_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12
firewalls:
default:
anonymous: ~
form_login:
login_path: login
check_path: login
use_referer: true
failure_path: login
require_previous_session: false
logout:
path: /logout
target: /
access_control:
#- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/mantenimiento, roles: ROLE_SUPER_ADMIN }
- { path: ^/cargar, roles: ROLE_SUPER_ADMIN }
- { path: ^/subir, roles: ROLE_SUPER_ADMIN }
- { path: ^/productos, roles: ROLE_USER }
- { path: ^/errores_carga, roles: ROLE_ADMIN }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
我的OnKernelRequest代码:
class MantenimientoService{
private $em;
private $container;
public function __construct(ContainerInterface $container, EntityManager $entityManager)
{
$this->container = $container;
$this->em = $entityManager;
}
public function onKernelRequest(GetResponseEvent $event)
{
//$event->getRequest()->get('_route') != 'app_index';
if ($this->container->get('security.context')){
$maintenanceMode = $this->em->getRepository("AppBundle:Configuracion")->EnMantenimiento();
$loadMode = $this->em->getRepository("AppBundle:Configuracion")->EnCarga();
//Si no es el superAdmin y la APP está en proceso de carga o mantenimiento, la bloqueamos
if (!$this->container->get('security.context')->isGranted('ROLE_SUPER_ADMIN')){
if ( $maintenanceMode || $loadMode ){
$vistaMantenimineto = $this->container->get('templating')->render('AppBundle:Default:mantenimiento.html.twig', array('mantenimiento' => true)); //THIS WORKS PERFECTLY
$event->setResponse(new Response($vistaMantenimineto)); //pintamos el template
}
}
else{ //ROLE_SUPER_ADMIN
if ($loadMode){
if ($event->getRequest()->get('_route') != 'subir' && $event->getRequest()->get('_route') != 'mantenimiento'){
/*HERE THE ERROR*/$vistaMantenimineto = $this->container->get('templating')->render('AppBundle:Default:mantenimiento.html.twig', array('mantenimiento' => true)); //THIS DOESN'T WORKS
$event->setResponse(new Response($vistaMantenimineto)); //pintamos el template
}
}
}
}
}
}
非常感谢。