Symfony\Bundle\FrameworkBundle\Controller\Controller::getUser PHP Method

getUser() protected method

Get a user from the Security Token Storage.
See also: TokenInterface::getUser()
protected getUser ( ) : mixed
return mixed
    protected function getUser()
    {
        if (!$this->container->has('security.token_storage')) {
            throw new \LogicException('The SecurityBundle is not registered in your application.');
        }
        if (null === ($token = $this->container->get('security.token_storage')->getToken())) {
            return;
        }
        if (!is_object($user = $token->getUser())) {
            // e.g. anonymous authentication
            return;
        }
        return $user;
    }

Usage Example

Example #1
0
 public function getUser($apiKey = null)
 {
     // get the logged user
     $user = parent::getUser();
     if (null === $user) {
         // if the logged user doesn't exist, check if there's an api key
         if (null !== $apiKey) {
             $em = $this->getDoctrine()->getEntityManager();
             $user = $em->getRepository("ZeegaDataBundle:User")->findOneBy(array("apiKey" => $apiKey));
             if (null !== $user) {
                 return $user;
             }
         }
     } else {
         if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
             return $user;
         }
     }
     return null;
 }
All Usage Examples Of Symfony\Bundle\FrameworkBundle\Controller\Controller::getUser