本文整理汇总了PHP中Symfony\Component\Security\Core\SecurityContext::setToken方法的典型用法代码示例。如果您正苦于以下问题:PHP SecurityContext::setToken方法的具体用法?PHP SecurityContext::setToken怎么用?PHP SecurityContext::setToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Security\Core\SecurityContext
的用法示例。
在下文中一共展示了SecurityContext::setToken方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
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);
}
示例2: closeAccount
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);
}
示例3: testGetSetToken
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());
}
示例4: refuseRegistration
/**
* @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'));
}
示例5: loginUser
/**
* 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;
}
示例6: createSecurityContext
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;
}
示例7: onKernelRequest
/**
* 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());
}
示例8: testGetUser
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');
}
示例9: getMockSecurityContext
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;
}
示例10: undoImpersonation
/**
* 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);
}
}
示例11: doLogout
/**
* Remove current security token
*/
public function doLogout()
{
$this->token_storage->setToken(null);
}
示例12: setSecurityContext
/**
* @param User $user
*/
protected function setSecurityContext(User $user)
{
$token = new UsernamePasswordOrganizationToken($user, $user->getUsername(), 'main', $this->organization);
$this->securityContext->setToken($token);
}