src/Controller/AccountController.php line 24

  1. <?php
  2. namespace App\Controller;
  3. use DateTimeImmutable;
  4. use App\Entity\Account;
  5. use App\Entity\AccountLog;
  6. use Symfony\Component\Uid\Ulid;
  7. use App\Entity\CompletedDialogue;
  8. use App\Repository\AccountRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  15. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. class AccountController extends AbstractController
  19. {
  20.     #[Route(path'/change/{hash}'name'api_change')]
  21.     public function change(Request $request$hashAccountRepository $accountRepositoryUserPasswordHasherInterface $accountPasswordHasher): Response
  22.     {
  23.         $isValid Ulid::isValid($hash);
  24.         
  25.         if (!$isValid) {
  26.             return $this->render('security/info.html.twig', [
  27.                 'title' => 'Błąd',
  28.                 'text' => 'Błędny link!',
  29.             ]);
  30.         }
  31.         
  32.         $account $accountRepository->findOneBy(['hash' => $hash]);
  33.         if (is_null($account)) {
  34.             return $this->render('security/info.html.twig', [
  35.                 'title' => 'Błąd',
  36.                 'text' => 'Brak konta!',
  37.             ]);
  38.         }
  39.         $date = new DateTimeImmutable('now -2 hours');
  40.         if ($account->getHashedAt() < $date) {
  41.             return $this->render('security/info.html.twig', [
  42.                 'title' => 'Błąd',
  43.                 'text' => 'Nieważny link!',
  44.             ]);
  45.         }
  46.         $defaultData = ['message' => 'Type your message here'];
  47.         $form $this->createFormBuilder($defaultData)
  48.             ->add('plainPassword'RepeatedType::class, [
  49.                 'type' => PasswordType::class,
  50.                 'invalid_message' => 'The password fields must match.',
  51.                 'options' => ['attr' => ['class' => 'password-field']],
  52.                 'required' => true,
  53.                 'first_options' => ['label' => 'Nowe hasło'],
  54.                 'second_options' => ['label' => 'Powtórz hasło'],
  55.                 'attr' => ['autocomplete' => 'off'],
  56.             ])
  57.             ->add('submit'SubmitType::class)
  58.             ->getForm();
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $data $form->getData();
  62.             $password $data['plainPassword'];
  63.             $account->setPassword($accountPasswordHasher->hashPassword($account$password));
  64.             $account->setHash(null);
  65.             $account->setHashedAt(null);
  66.             $accountRepository->save($accounttrue);
  67.             return $this->render('security/info.html.twig', [
  68.                 'title' => 'Sukces',
  69.                 'text' => 'Hasło zostało zmienione',
  70.             ]);
  71.         }
  72.         
  73.         return $this->render('security/change.html.twig', [
  74.             'form' => $form,
  75.             'error' => false,
  76.         ]);
  77.     
  78.     }
  79.     // #[Route("/account/{id}/delete", name: "app_account_delete")]
  80.     // public function delete(Account $account, EntityManagerInterface $entityManager): Response
  81.     // {
  82.     //     $completedDialogues = $account->getCompletedDialogues();
  83.     //     foreach ($completedDialogues as $completedDialogue) {
  84.     //         $entityManager->remove($completedDialogue); 
  85.     //     }
  86.     //     $accountLogs = $entityManager->getRepository(AccountLog::class)->findBy(['objectId' => $account->getId()]);
  87.     //     foreach ($accountLogs as $accountLog) {
  88.     //         $entityManager->remove($accountLog);
  89.     //     }
  90.     //     $accountBlocks = $account->getAccountBlocks();
  91.     //     foreach ($accountBlocks as $accountBlock) {
  92.     //         $entityManager->remove($accountBlock);
  93.     //     }
  94.     //     $entityManager->remove($account);
  95.     //     $entityManager->flush();
  96.         
  97.     //     return new Response();
  98.     // }
  99. }