1

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

#   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() 方法 = 之后中断。

在这个例子中 $userID 是空的......即使在 getToken()->getUser() 给我空......

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

请帮忙。

4

5 回答 5

1

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

喜欢:

if ($securityContext->getToken() && is_object($securityContext->getToken()->getUser())){
于 2014-09-04T10:27:40.280 回答
1

您必须像这样使用令牌检查:

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

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

于 2013-10-17T11:06:04.133 回答
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();
}
于 2015-05-22T08:55:05.470 回答
0

对于更新,服务security.context自 Symfony 2.6 起已弃用。

所以你的新服务声​​明应该看起来像

#   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

于 2017-08-01T20:18:32.690 回答
0

有一个类似的问题,我在其中发布了一个解决它的答案:

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

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

于 2018-04-12T10:31:12.590 回答