Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony OnKernelRequest因角色而失败_Symfony_Listener_Roles - Fatal编程技术网

Symfony OnKernelRequest因角色而失败

Symfony OnKernelRequest因角色而失败,symfony,listener,roles,Symfony,Listener,Roles,我在de OnKernelRequest中遇到了一个错误,但很奇怪: 我有3个角色:角色用户->角色管理员->角色超级管理员(每个角色都有前面的角色) 在我的应用程序中,我有一个维护模式和一个加载模式(取决于数据库中的参数)。我已经超越了OnKernelRequest,以查看应用程序是否处于维护/加载模式,如果用户未被授予角色\u SUPER\u ADMIN,则呈现维护视图(这非常有效) 现在我想添加一些条件。条件是,即使用户在某些URL中被授予了角色\u SUPER\u ADMIN,如果应用程

我在de OnKernelRequest中遇到了一个错误,但很奇怪:

我有3个角色:角色用户->角色管理员->角色超级管理员(每个角色都有前面的角色)

在我的应用程序中,我有一个维护模式和一个加载模式(取决于数据库中的参数)。我已经超越了OnKernelRequest,以查看应用程序是否处于维护/加载模式,如果用户未被授予角色\u SUPER\u ADMIN,则呈现维护视图(这非常有效)

现在我想添加一些条件。条件是,即使用户在某些URL中被授予了角色\u SUPER\u ADMIN,如果应用程序处于加载模式,应用程序也会呈现维护视图(这不起作用,会破坏应用程序)。但奇怪的是,我在每种情况下都使用完全相同的代码重定向

我看不到错误,因为在触发错误条件时,应用程序会中断并且无法连接

My 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
                    }
                }
            }

        }

    }

}

非常感谢。

您检查过symfony日志文件中的一些错误吗?是的,我检查过,但没有打印错误。应用程序中断,并获得一个连接错误的空白页。