/**
  * {@inheritdoc}
  */
 public function onAuthenticationSuccess(Request $request, TokenInterface $token)
 {
     /** @var User $user */
     $user = $token->getUser();
     $token = $this->tokenManager->getOrCreate($user, $request->request->get('token_name') ?: 'API Token');
     $response = new JsonResponse(['token' => $token->getToken()]);
     return $response;
 }
示例#2
0
 public function testGetOrCreate()
 {
     $registry = M::mock(ManagerRegistry::class);
     $user = new User();
     $token1 = new ApiToken();
     $token1->setName('token1');
     $token2 = new ApiToken();
     $token2->setName('token2');
     $user->setApiTokens([$token1, $token2]);
     $manager = M::mock(ObjectManager::class);
     $registry->shouldReceive('getManager')->withNoArgs()->andReturn($manager);
     $manager->shouldReceive('persist')->withAnyArgs()->andReturn();
     $manager->shouldReceive('flush')->withNoArgs();
     $tm = new ApiTokenManager($registry);
     $token = $tm->getOrCreate($user, 'token3');
     $this->assertInstanceOf(ApiToken::class, $token);
     $this->assertNotSame($token1, $token);
     $this->assertNotSame($token2, $token);
     $this->assertSame($user, $token->getUser());
     $this->assertSame('token3', $token->getName());
 }