public function testFindOneByHash()
 {
     $activationLink = $this->manager->findOneByHash('test-hash');
     $this->assertInstanceOf(ActivationLink::class, $activationLink);
     $this->assertEquals(1, $activationLink->getId());
     $this->assertEquals('test-hash', $activationLink->getHash());
     $this->assertInstanceOf(\DateTime::class, $activationLink->getCreatedAt());
 }
Example #2
0
 /**
  * @param string $activationHash
  * @throws \InvalidArgumentException
  * @return \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
  */
 public function activateUserAccount(string $activationHash) : UsernamePasswordToken
 {
     $activationLink = $this->activationLinkManager->findOneByHash($activationHash);
     if ($activationLink === null) {
         throw new \InvalidArgumentException('not_found');
     }
     $datetime = new \DateTime();
     $datetime->setTimestamp(time() - 86400);
     if ($activationLink->getCreatedAt() < $datetime) {
         throw new \InvalidArgumentException('invalid');
     }
     $user = $this->em->getRepository(User::class)->findOneByActivationLink($activationLink);
     if ($user === null) {
         $this->em->remove($activationLink);
         $this->em->flush();
         throw new \InvalidArgumentException('unrelated');
     }
     $user->enable(true);
     $user->setActivationLink(null);
     $this->em->persist($user);
     $this->em->remove($activationLink);
     $this->em->flush();
     return new UsernamePasswordToken($user, null, 'main', $user->getRoles());
 }