delete and set delete user

This commit is contained in:
Charles 2025-07-17 14:10:55 +02:00
parent d8df0bc1f4
commit 3271da59fa
1 changed files with 49 additions and 23 deletions

View File

@ -43,7 +43,7 @@ class UserController extends AbstractController
/**
* GET /user/{id} - Show specific user (show/member)
*/
#[Route('/{id}', name: 'show', methods: ['GET'], requirements: ['id' => '\d+'])]
#[Route('/{id}', name: 'show', requirements: ['id' => '\d+'], methods: ['GET'])]
public function show(int $id, EntityManagerInterface $entityManager): Response
{
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
@ -132,28 +132,54 @@ class UserController extends AbstractController
]);
}
//
// /**
// * DELETE /user/{id} - Delete user
// */
// #[Route('/{id}', name: 'delete', methods: ['DELETE'], requirements: ['id' => '\d+'])]
// public function delete(int $id, EntityManagerInterface $entityManager): Response
// {
// if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
// throw $this->createAccessDeniedException('Access denied');
// }
//
// $user = $entityManager->getRepository(User::class)->find($id);
// if (!$user) {
// throw $this->createNotFoundException(self::NOT_FOUND));
// }
//
// // Handle user deletion logic
// $entityManager->remove($user);
// $entityManager->flush();
//
// return $this->redirectToRoute('user_index');
// }
/**
* DELETE /user/{id} - Delete user
*/
#[Route('/{id}', name: 'setDelete', requirements: ['id' => '\d+'], methods: ['POST'])]
public function setDelete(int $id, EntityManagerInterface $entityManager): Response
{
//This method is used to set a user as deleted without actually removing them from the database.
//Handle access control
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
throw $this->createAccessDeniedException('Access denied');
}
//Fetch user by ID and handle not found case
$user = $entityManager->getRepository(User::class)->find($id);
if (!$user) {
throw $this->createNotFoundException(self::NOT_FOUND);
}
// Handle user deletion logic
$user->setIsDeleted(true);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('user_index');
}
/**
* DELETE /user/{id} - Delete user
*/
#[Route('/{id}', name: 'delete', requirements: ['id' => '\d+'], methods: ['DELETE'])]
public function delete(int $id, EntityManagerInterface $entityManager): Response
{
if (!$this->isGranted('ROLE_SUDALYS_ADMIN')) {
throw $this->createAccessDeniedException('Access denied');
}
$user = $entityManager->getRepository(User::class)->find($id);
if (!$user) {
throw $this->createNotFoundException(self::NOT_FOUND);
}
// Handle user deletion logic
$entityManager->remove($user);
$entityManager->flush();
return $this->redirectToRoute('user_index');
}
#[Route('/deactivate/{id}', name: 'deactivate', methods: ['GET'])]