Skip to content
Snippets Groups Projects
Commit dd31259d authored by user's avatar user
Browse files
parents 00b332b8 8a07b911
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,17 @@ security: ...@@ -25,7 +25,17 @@ security:
secret: '%kernel.secret%' secret: '%kernel.secret%'
access_control: access_control:
# Allow access to /login without being authenticated
- { path: ^/login, allow_if: "1" }
# Allow users who are admins to access the /admin path
- { path: ^/admin, allow_if: "user and user.isAdmin() == true" } - { path: ^/admin, allow_if: "user and user.isAdmin() == true" }
# Allow users who are not locked to access other pages
- { path: ^/.*, allow_if: "user and user.isLocked() != true" }
# Allow all authenticated users to access other routes (outside /admin or /login)
- { path: ^/, allow_if: "user" }
when@test: when@test:
security: security:
......
...@@ -14,56 +14,54 @@ use Symfony\Component\Routing\Attribute\Route; ...@@ -14,56 +14,54 @@ use Symfony\Component\Routing\Attribute\Route;
#[Route('/wishlist')] #[Route('/wishlist')]
final class WishlistController extends AbstractController final class WishlistController extends AbstractController
{ {
// Method to display all wishlists for the currently logged-in user
#[Route(name: 'app_wishlist_index', methods: ['GET'])] #[Route(name: 'app_wishlist_index', methods: ['GET'])]
public function getWishLists(WishlistRepository $wishlistRepository): Response public function getWishLists(WishlistRepository $wishlistRepository): Response
{ {
$user = $this->getUser() ; $user = $this->getUser(); // Get the currently authenticated user
return $this->render('wishlist/index.html.twig', [ return $this->render('wishlist/index.html.twig', [
'wishlists' => $user->getWishlists() 'wishlists' => $user->getWishlists()->toArray() // Pass the user's wishlists to the template
]); ]);
} }
// Method to create a new wishlist
#[Route('/new', name: 'app_wishlist_new', methods: ['GET', 'POST'])] #[Route('/new', name: 'app_wishlist_new', methods: ['GET', 'POST'])]
public function createWishlist(Request $request, EntityManagerInterface $entityManager): Response public function createWishlist(Request $request, EntityManagerInterface $entityManager): Response
{ {
$wishlist = new Wishlist(); $wishlist = new Wishlist(); // Create a new Wishlist entity
$form = $this->createForm(WishlistType::class, $wishlist); $form = $this->createForm(WishlistType::class, $wishlist); // Create a form for the Wishlist entity
$form->handleRequest($request); $form->handleRequest($request); // Handle the form submission
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($wishlist); $entityManager->persist($wishlist); // Persist the new wishlist to the database
$entityManager->flush(); $entityManager->flush(); // Save changes to the database
return $this->redirectToRoute('app_wishlist_index', [], Response::HTTP_SEE_OTHER); return $this->redirectToRoute('app_wishlist_index', [], Response::HTTP_SEE_OTHER); // Redirect to the wishlist index page
} }
return $this->render('wishlist/new.html.twig', [ return $this->render('wishlist/new.html.twig', [
'wishlists' => $wishlist, 'wishlist' => $wishlist, // Pass the wishlist entity to the template
'form' => $form, 'form' => $form, // Pass the form to the template
]); ]);
} }
// Method to display a specific wishlist
#[Route('/{id}', name: 'app_wishlist_show', methods: ['GET'])] #[Route('/{id}', name: 'app_wishlist_show', methods: ['GET'])]
public function show(Wishlist $wishlist): Response public function show(Wishlist $wishlist): Response
{ {
return $this->render('wishlist/show.html.twig', [ return $this->render('wishlist/show.html.twig', [
'wishlist' => $wishlist, 'wishlist' => $wishlist, // Pass the wishlist entity to the template
]); ]);
} }
// Method to edit an existing wishlist
#[Route('/{id}/edit', name: 'app_wishlist_edit', methods: ['GET', 'POST'])] #[Route('/{id}/edit', name: 'app_wishlist_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response public function edit(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response
{ {
$wishlist->setName($request->get('name'));
$wishlist->setDeadline($request->get('deadline')) ;
$entityManager->persist($wishlist); $form = $this->createForm(WishlistType::class, $wishlist);
$entityManager->flush();
return new Response('wishlist was modified successfully ', Response::HTTP_ACCEPTED) ;
/* $form = $this->createForm(WishlistType::class, $wishlist);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
...@@ -75,31 +73,20 @@ final class WishlistController extends AbstractController ...@@ -75,31 +73,20 @@ final class WishlistController extends AbstractController
return $this->render('wishlist/edit.html.twig', [ return $this->render('wishlist/edit.html.twig', [
'wishlist' => $wishlist, 'wishlist' => $wishlist,
'form' => $form, 'form' => $form,
]); */ ]);
} }
// Method to delete a wishlist
#[Route('/{id}', name: 'app_wishlist_delete', methods: ['POST'])] #[Route('/{id}', name: 'app_wishlist_delete', methods: ['POST'])]
public function delete(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response public function delete(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response
{ {
// Validate the CSRF token before deleting the wishlist
if ($this->isCsrfTokenValid('delete'.$wishlist->getId(), $request->getPayload()->getString('_token'))) { if ($this->isCsrfTokenValid('delete'.$wishlist->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($wishlist); $entityManager->remove($wishlist); // Remove the wishlist from the database
$entityManager->flush(); $entityManager->flush(); // Save changes to the database
} }
return $this->redirectToRoute('app_wishlist_index', [], Response::HTTP_SEE_OTHER); return $this->redirectToRoute('app_wishlist_index', [], Response::HTTP_SEE_OTHER); // Redirect to the wishlist index page
} }
}
\ No newline at end of file
}
/*
$wishlist = new Wishlist();
$name = $request->get(key: 'name');
$wishlist->setName(name: $name);
$deadline = $request->get('deadline') ;
$wishlist->setDeadline($deadline);
$this->getUser()->addToAuthorWhishlists($wishlist);
$entityManager->persist($wishlist);
$entityManager->persist($this->getUser()) ;
$entityManager->flush(); */
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment