Symfony2在kernel.controller事件监听器上获取userID

时间:2013-10-02 18:48:39

标签: security symfony event-listener

有人可以帮我从event.controller获取事件监听器中的userID吗?

#   EventListener
    kernel.listener.corporation.manage:
        class:  Site\CorporationBundle\Event\SiteCorporationManageListener
        arguments: ["@doctrine.orm.entity_manager", "@user.own.item", "@security.context"]
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelRequest }

听众类

use Doctrine\ORM\EntityManager;
use Site\MainBundle\Service\UserOwnItem;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\SecurityContext;

class SiteCorporationManageListener
{
    private $oEntityManager = null;
    private $oUserOwnItem = null;
    private $oSecurityContext = null;

    public function __construct(EntityManager $oEntityManager, UserOwnItem $oUserOwnItem, SecurityContext $oSecurityContext)
    {
        $this->oEntityManager = $oEntityManager;
        $this->oUserOwnItem = $oUserOwnItem;
        $this->oSecurityContext = $oSecurityContext;
    }

    public function onKernelRequest(FilterControllerEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
            $route = $event->getRequest()->get('_route');
            $corporationID = $event->getRequest()->get('corporationID', null);
            $userID = $this->oSecurityContext->getToken()->getUser()->getID();
            //$userID = 3;
            //var_dump($userID);

            if (strstr($route, 'corporation')) {
                if (!strstr($route, 'corporation_index')) {
                    $bUserOwn = $this->oUserOwnItem->setUserID($userID)->setItemType('corporation')->setItemID($corporationID)->userOwn();
                    //var_dump($bUserOwn);
                }
            }
        }
    }
}

我稍后会清理它,我尝试不同的方法来做,但即使通过容器和security_context,我也无法获得userID。它在getToken()方法=。

之后生成

在此示例中,$ userID为null ...即使在getToken() - > getUser()之后也给我null ...

FatalErrorException: Error: Call to a member function getUser() on a non-object in /home/dev/public_html/git.eve-ceo/src/Site/CorporationBundle/Event/SiteCorporationManageListener.php line 32

请帮助。

5 个答案:

答案 0 :(得分:1)

你必须使用这样的令牌检查:

if ($context->getToken() && $context->getToken()->getUser() !== 'anon.')
    $user = $context->getToken()->getUser();

基本上,您不是经过身份验证的用户。

答案 1 :(得分:1)

我认为相同,但在symfony中,如果你在登录之前输入“var_dump($ user)”,结果是:string'anon。' (长度= 5)。 也许你可以使用“is_object($ user)(对我来说是更好的选择)

像:

if ($securityContext->getToken() && is_object($securityContext->getToken()->getUser())){

答案 2 :(得分:1)

可以通过此获取用户ID。

包含此内容。

use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

这里有userId

$user = $this->oSecurityContext->getToken()->getUser();

if (!is_object($user) || !$user instanceof UserInterface) {
   throw new AccessDeniedException('You are not authorize to access this location.');
}
else{
    $userID = $user->getId();
}

答案 3 :(得分:0)

对于更新,自Symfony 2.6以来,不推荐使用服务security.context

所以你的新服务声​​明应该是

#   EventListener
kernel.listener.corporation.manage:
    class:  Site\CorporationBundle\Event\SiteCorporationManageListener
    arguments: ["@doctrine.orm.entity_manager", "@user.own.item", "@security.token_storage"]
    tags:
        - { name: kernel.event_listener, event: kernel.controller, method: onKernelRequest }

构造就像

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

...

public function __construct(EntityManager $oEntityManager, UserOwnItem $oUserOwnItem, TokenStorage $tokenStorage)
{
    $this->oEntityManager = $oEntityManager;
    $this->oUserOwnItem = $oUserOwnItem;
    $this->tokenStorage = $tokenStorage;
}

然后让用户变成

$user = $this->tokenStorage->getToken()->getUser();

来源:http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

答案 4 :(得分:0)

有一个类似的问题,我在那里发布了解决问题的答案:

https://stackoverflow.com/a/49794146/2564552

简而言之:static上的监听器在令牌初始化之前被调用,因此您必须使用优先级或(在较新的Symfony版本中)只使用(尚未记录的)kernel.controller事件