具有授权检查程序的Symfony kernel.response侦听器

时间:2016-03-26 00:47:19

标签: php symfony listener

我正在设置kernel.response事件,我想测试用户是否已登录。

这是我的代码:

services.yml

app.kernel.modal_injection:
    class: App\UserBundle\EventListener\ModalListener
    tags:
        - { name: kernel.event_listener, event: kernel.response }
    arguments:
        - @security.authorization_checker

ModalListener.php

<?php
namespace App\UserBundle\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;


class ConnectModalListener implements EventSubscriberInterface
{
    protected $securityChecker;

    public function __construct( AuthorizationChecker $securityChecker )
    {
        $this->securityChecker = $securityChecker;
    }

    public function onKernelResponse( FilterResponseEvent $event )
    {
        $response = $event->getResponse();
        $request = $event->getRequest();

        if ( !$event->isMasterRequest() ) {
            return;
        }
        if ( $request->isXmlHttpRequest() ) {
            return;
        }

        if ( $this->securityChecker->isGranted( 'IS_AUTHENTICATED_REMEMBERED' ) ) {
            return;
        }

        // CODE HERE


    }

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::RESPONSE => array( 'onKernelResponse', 0 ),
        );
    }
}

我的问题是,在检查用户IS_AUTHENTICATED_REMEMBER是否在我的页面上提供css和js文件时出现错误500(意味着没有js或css)。有没有办法收费?最终在那些?

上过滤kernel.response事件

1 个答案:

答案 0 :(得分:1)

静态文件中的标记为null,因此isGranted返回错误500.所以我必须在之前运行检查:

if ( $this->tokenStorage->getToken() !== null
        && $this->securityChecker->isGranted( 'IS_AUTHENTICATED_REMEMBERED' ) ) {
    return;
}