Symfony2在kernel.controller事件侦听器上获取用户ID [英] Symfony2 get userID on kernel.controller event listener

查看:129
本文介绍了Symfony2在kernel.controller事件侦听器上获取用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我从event.controller的事件侦听器中获取用户ID吗?

Can someone help me to get userID in event listener from event.controller?

#   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我也无法获取用户ID.在getToken()方法=.之后崩溃了.

I'll clean it later, i try different ways to do it, but even through container and security_context i cannot get userID. It brokes after getToken() method =.

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

At this example $userID is null... Even after getToken()->getUser() give me 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

请帮助.

推荐答案

可以获取用户ID.

包括这个.

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


在这里您获得了userId


Here you get 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();
}

这篇关于Symfony2在kernel.controller事件侦听器上获取用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆