setToken() public method

Deprecation: since version 2.6, to be removed in 3.0. Use TokenStorageInterface::setToken() instead. {@inheritdoc}
public setToken ( Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token = null )
$token Symfony\Component\Security\Core\Authentication\Token\TokenInterface
Ejemplo n.º 1
0
 public function register(User $user)
 {
     $user->setSalt(md5(time()));
     $password = $this->securityEncoderFactory->getEncoder($user)->encodePassword($user->getPassword(), $user->getSalt());
     $user->setPassword($password);
     $this->em->persist($user);
     $this->em->flush();
     $token = new UsernamePasswordToken($user, $user->getPassword(), 'app', $user->getRoles());
     $this->securityContext->setToken($token);
 }
Ejemplo n.º 2
0
 public function closeAccount(Response $response)
 {
     $user = $this->securityContext->getToken()->getUser();
     $user->setEnabled(false);
     $this->userManager->updateUser($user);
     $cookieHandler = new CookieClearingLogoutHandler($this->request->cookies->all());
     $cookieHandler->logout($this->request, $response, $this->securityContext->getToken());
     $sessionHandler = new SessionLogoutHandler();
     $sessionHandler->logout($this->request, $response, $this->securityContext->getToken());
     $this->securityContext->setToken(null);
 }
 public function testGetSetToken()
 {
     $context = new SecurityContext($this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\AuthenticationManagerInterface'), $this->getMock('Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface'));
     $this->assertNull($context->getToken());
     $context->setToken($token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface'));
     $this->assertSame($token, $context->getToken());
 }
Ejemplo n.º 4
0
 /**
  * @param $entityManager
  * @param $user
  */
 private function refuseRegistration($entityManager, $user)
 {
     $entityManager->remove($user);
     $entityManager->flush();
     $this->securityContext->setToken(null);
     return $this->redirect($this->generateUrl('base_publichome'));
 }
Ejemplo n.º 5
0
 /**
  * Log in user
  *
  * @param Newscoop\Entity\User $user
  * @param string               $providerKey
  *
  * @return UsernamePasswordToken
  */
 public function loginUser(User $user, $providerKey)
 {
     $roles = $user->getRoles();
     $token = new UsernamePasswordToken($user, null, $providerKey, $roles);
     $this->security->setToken($token);
     return $token;
 }
Ejemplo n.º 6
0
 protected function createSecurityContext($granted = false)
 {
     $authManager = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\AuthenticationManagerInterface');
     $decisionManager = $this->getMock('Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface');
     $decisionManager->expects($this->any())->method('decide')->will($this->returnValue($granted));
     $context = new SecurityContext($authManager, $decisionManager, false);
     $context->setToken($token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface'));
     $token->expects($this->any())->method('isAuthenticated')->will($this->returnValue(true));
     return $context;
 }
Ejemplo n.º 7
0
 /**
  * Called on kernel.request event. Find current user.
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     // User already found ? (Used by tests)
     if ($this->securityContext->getToken() !== null) {
         return;
     }
     // Cookie not found
     if ($event->getRequest()->cookies->has(md5('etuutt-session-cookie-name'))) {
         $cookie = $event->getRequest()->cookies->get(md5('etuutt-session-cookie-name'));
         /** @var EntityManager $em */
         $em = $this->doctrine->getManager();
         // Find session
         /** @var \Etu\Core\UserBundle\Entity\Session $session */
         $session = $em->getRepository('EtuUserBundle:Session')->findOneBy(['token' => $cookie]);
         if ($session && $session->getExpireAt() > new \DateTime()) {
             if ($session->getEntityType() == \Etu\Core\UserBundle\Entity\Session::TYPE_ORGA) {
                 $this->session->set('user', null);
                 $this->session->set('user_data', null);
                 $orga = $em->getRepository('EtuUserBundle:Organization')->find($session->getEntityId());
                 $this->session->set('orga', $orga);
                 $this->securityContext->setToken(new OrgaToken($orga));
                 return;
             } elseif ($session->getEntityType() == \Etu\Core\UserBundle\Entity\Session::TYPE_USER) {
                 $this->session->set('orga', null);
                 $user = $em->getRepository('EtuUserBundle:User')->find($session->getEntityId());
                 $this->session->set('user', $user->getId());
                 $this->session->set('user_data', $user);
                 $this->securityContext->setToken(new UserToken($user));
                 return;
             }
         }
     }
     $this->session->set('user', null);
     $this->session->set('orga', null);
     $this->securityContext->setToken(new AnonymousToken());
 }
Ejemplo n.º 8
0
    public function testGetUser()
    {
        $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
        $token->expects($this->once())
            ->method('getUser')
            ->will($this->returnValue('foo'));

        $context = new SecurityContext(
            $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'),
            $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')
        );

        $this->assertNull($context->getUser(), '->getUser() returns null when there is no token');

        $context->setToken($token);
        $this->assertEquals('foo', $context->getUser(), '->getUser() return the token user');
    }
 protected function getMockSecurityContext()
 {
     $authManager = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\AuthenticationManagerInterface');
     $decisionManager = $this->getMock('Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface');
     $context = new SecurityContext($authManager, $decisionManager);
     $context->setToken($token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface'));
     return $context;
 }
 /**
  * Switches the security context to the previous security token
  *
  * @param TokenInterface|null $originalToken
  */
 protected function undoImpersonation(TokenInterface $originalToken = null)
 {
     if ($originalToken) {
         $this->securityContext->setToken($originalToken);
     }
 }
Ejemplo n.º 11
0
 /**
  * Remove current security token
  */
 public function doLogout()
 {
     $this->token_storage->setToken(null);
 }
Ejemplo n.º 12
0
 /**
  * @param User $user
  */
 protected function setSecurityContext(User $user)
 {
     $token = new UsernamePasswordOrganizationToken($user, $user->getUsername(), 'main', $this->organization);
     $this->securityContext->setToken($token);
 }