From 946b30b486d93c519aad6309adf8e6bb7d0e8a57 Mon Sep 17 00:00:00 2001 From: thisfro Date: Mon, 17 Jan 2022 21:25:31 +0100 Subject: [PATCH] Use uniqid for wishes and users --- migrations/Version20220117193804.php | 33 ++++++++++++++++++++++ src/Controller/RegistrationController.php | 1 + src/Controller/TradeController.php | 6 ++-- src/Controller/UserController.php | 4 +-- src/Entity/User.php | 17 +++++++++++ src/Entity/Wish.php | 17 +++++++++++ templates/offer/index.html.twig | 2 +- templates/user/public.html.twig | 2 +- templates/user/trade/offer_email.html.twig | 2 +- templates/user/wish.html.twig | 2 +- 10 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 migrations/Version20220117193804.php diff --git a/migrations/Version20220117193804.php b/migrations/Version20220117193804.php new file mode 100644 index 0000000..4eb7858 --- /dev/null +++ b/migrations/Version20220117193804.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE user ADD url_id VARCHAR(13) NOT NULL'); + $this->addSql('ALTER TABLE wish ADD url_id VARCHAR(13) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE `user` DROP url_id'); + $this->addSql('ALTER TABLE wish DROP url_id'); + } +} diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 65e17d8..c228033 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -33,6 +33,7 @@ class RegistrationController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $user->setUrlId(uniqid()); // encode the plain password $user->setPassword( $passwordEncoder->hashPassword( diff --git a/src/Controller/TradeController.php b/src/Controller/TradeController.php index f8f7882..31f798f 100644 --- a/src/Controller/TradeController.php +++ b/src/Controller/TradeController.php @@ -19,7 +19,7 @@ use Symfony\Component\Routing\Annotation\Route; class TradeController extends AbstractController { - #[Route('/trade/{id}', name: 'trade')] + #[Route('/trade/{urlId}', name: 'trade')] public function sendEmail(MailerInterface $mailer, Offering $offer, OfferingRepository $offeringRepository, WishRepository $wishRepository): Response { $user = $this->getUser(); @@ -35,7 +35,7 @@ class TradeController extends AbstractController ->htmlTemplate('user/trade/offer_email.html.twig') ->context([ 'user' => $user, - 'id' => $user->getId(), + 'urlId' => $user->getUrlId(), ]) ; try @@ -49,6 +49,6 @@ class TradeController extends AbstractController $this->addFlash('error','You can\'t trade with yourself!'); } - return $this->redirectToRoute('show_offer', ['urlId' => $offer->getId()]); + return $this->redirectToRoute('show_offer', ['urlId' => $offer->getUrlId()]); } } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index de1df3a..6353c35 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -53,7 +53,7 @@ class UserController extends AbstractController ]); } - #[Route('/user/{id}', name: 'user_public')] + #[Route('/user/{urlId}', name: 'user_public')] public function show_user(User $user, OfferingRepository $offeringRepository, WishRepository $wishRepository): Response { return $this->render('user/public.html.twig', [ @@ -89,7 +89,7 @@ class UserController extends AbstractController ]); } - #[Route('/wish/delete/{id}', name: 'delete_wish')] + #[Route('/wish/delete/{urlId}', name: 'delete_wish')] public function deleteWish(Wish $wish): Response { $user = $this->getUser(); diff --git a/src/Entity/User.php b/src/Entity/User.php index b31daf0..6ced665 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -65,6 +65,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface */ private $zipCode; + /** + * @ORM\Column(type="string", length=13) + */ + private $urlId; + public function __construct() { $this->offerings = new ArrayCollection(); @@ -255,4 +260,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + + public function getUrlId(): ?string + { + return $this->urlId; + } + + public function setUrlId(string $urlId): self + { + $this->urlId = $urlId; + + return $this; + } } diff --git a/src/Entity/Wish.php b/src/Entity/Wish.php index fac0288..cb42a86 100644 --- a/src/Entity/Wish.php +++ b/src/Entity/Wish.php @@ -27,6 +27,11 @@ class Wish */ private $byUser; + /** + * @ORM\Column(type="string", length=13) + */ + private $urlId; + public function getId(): ?int { return $this->id; @@ -60,4 +65,16 @@ class Wish { return (string) $this->getTitle(); } + + public function getUrlId(): ?string + { + return $this->urlId; + } + + public function setUrlId(string $urlId): self + { + $this->urlId = $urlId; + + return $this; + } } diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig index d917e2f..d61a397 100644 --- a/templates/offer/index.html.twig +++ b/templates/offer/index.html.twig @@ -41,7 +41,7 @@