isGranted('ROLE_SUDALYS_ADMIN')) { $users = $entityManager->getRepository(User::class)->getAllActiveUsers(); } else { $users = 'Not Super Admin'; } return $this->render('user/index.html.twig', [ 'users' => $users, 'controller_name' => 'IndexController', ]); } /** * GET /user/{id} - Show specific user (show/member) */ #[Route('/{id}', name: 'show', requirements: ['id' => '\d+'], methods: ['GET'])] public function show(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); } $userOrganizations = $this->userOrganizationService->getUserOrganizations($user); return $this->render('user/profile.html.twig', [ 'user' => $user, 'userOrganizations' => $userOrganizations, ]); } /** * GET /user/new - Show form to create new user and handle submission */ #[Route('/new', name: 'new', methods: ['GET', 'POST'])] public function new(Request $request): Response { $form = $this->createForm(UserForm::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { //Data is a User object. App\Form\NewUserForm is a form type that maps to User entity $data = $form->getData(); // Handle user creation logic here //FOR DEV PURPOSES ONLY $data->setPictureUrl(""); $data->setPassword($this->userService->generateRandomPassword()); //FOR DEV PURPOSES ONLY $this->entityManager->persist($data); $this->entityManager->flush(); // Redirect to user index return $this->redirectToRoute('user_index'); } return $this->render('user/new.html.twig', [ 'form' => $form->createView(), ]); } /** * GET /user/{id}/edit - Show form to edit user */ #[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'PUT', 'POST'])] public function edit(int $id, EntityManagerInterface $entityManager, Request $request): Response { //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); } //Create form for editing user $form = $this->createForm(UserForm::class, $user); //Handle form submission $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { //Persist changes to the user entity $entityManager->persist($user); $entityManager->flush(); //Redirect to user profile after successful edit return $this->redirectToRoute('user_show', ['id' => $user->getId()]); } return $this->render('user/edit.html.twig', [ 'form' => $form->createView(), 'user' => $user, ]); } /** * 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'])] public function userDeactivate(Request $request, EntityManagerInterface $entityManager): Response { if ($this->isGranted('ROLE_SUDALYS_ADMIN')) { $userId = $request->attributes->get('id'); $user = $entityManager->getRepository(User::class)->find($userId); if (!$user) { throw $this->createNotFoundException(self::NOT_FOUND); } $user->setIsActive(false); $entityManager->persist($user); $entityManager->flush(); return $this->redirectToRoute('user_index'); } return new Response('Unauthorized', Response::HTTP_UNAUTHORIZED); } }