2013-10-02 60 views
1

有人可以幫助我從event.controller中獲取事件監聽器中的用戶ID嗎?Symfony2在kernel.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我不能得到用戶ID。它在getToken()方法=之後中斷。

在這個例子$用戶ID是空...即使在爲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 

請幫助。

回答

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(); 
} 
1

必須使用令牌的檢查是這樣的:

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

基本上你沒有身份驗證的用戶。

+0

我不認爲$ context-> getToken() - > getUser()!=='anon。'是非常好的選擇。 – user1954544

1

我認爲是一樣的,但在symfony中,如果您在登錄之前放入「var_dump($ user)」,則結果爲:string'anon。'。 (長度= 5)。 也許你可以使用「is_object($用戶)(對我來說是更好的選擇)

像:

if ($securityContext->getToken() && is_object($securityContext->getToken()->getUser())){ 
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