diff --git a/assets/controllers/user_controller.js b/assets/controllers/user_controller.js index 3f4df84..e8fd81f 100644 --- a/assets/controllers/user_controller.js +++ b/assets/controllers/user_controller.js @@ -704,9 +704,10 @@ export default class extends Controller { const userId = target.getAttribute('data-id'); if (confirm('Voulez-vous vraiment désactiver cet utilisateur ?')) { const formData = new FormData(); + formData.append('status', 'deactivate'); formData.append('organizationId', target.getAttribute('data-org-id')); - fetch(`/user/organization/deactivate/${userId}`, { + fetch(`/user/organization/activateStatus/${userId}`, { method: 'POST', body: formData, headers: {'X-Requested-With': 'XMLHttpRequest'} @@ -731,9 +732,10 @@ export default class extends Controller { const userId = target.getAttribute('data-id'); if (confirm('Voulez-vous réactiver cet utilisateur ?')) { const formData = new FormData(); + formData.append('status', 'activate'); formData.append('organizationId', target.getAttribute('data-org-id')); - fetch(`/user/organization/activate/${userId}`, { + fetch(`/user/organization/activateStatus/${userId}`, { method: 'POST', body: formData, headers: {'X-Requested-With': 'XMLHttpRequest'} @@ -757,7 +759,7 @@ export default class extends Controller { // columns.push( // { // title: "Statut", field: "role", // or any field you want - // headerSort: false, + // headerSort: false,x // hozAlign: "center", // vertAlign: "middle", // formatter: (cell) => { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index e97a2d7..4da9808 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -21,6 +21,7 @@ use App\Service\UserOrganizationAppService; use App\Service\UserOrganizationService; use App\Service\UserService; use Doctrine\ORM\EntityManagerInterface; +use mysql_xdevapi\Exception; use Psr\Log\LoggerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -322,77 +323,54 @@ class UserController extends AbstractController throw $this->createNotFoundException(self::NOT_FOUND); } - //TODO : MONOLOG - #[Route('/organization/deactivate/{id}', name: 'deactivate_organization', methods: ['GET', 'POST'])] - public function deactivateUserInOrganization(int $id, Request $request): Response - { + #[Route('/organization/activateStatus/{id}', name: 'activate_organization', methods: ['GET', 'POST'])] + public function activateStatusOrganization(int $id, Request $request): JsonResponse{ $this->denyAccessUnlessGranted('ROLE_ADMIN'); $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); - if ($this->userService->hasAccessTo($actingUser, true)) { - $orgId = $request->get('organizationId'); - $org = $this->organizationRepository->find($orgId); - if (!$org) { - throw $this->createNotFoundException(self::NOT_FOUND); + try { + if ($this->userService->hasAccessTo($actingUser, true)) { + $orgId = $request->get('organizationId'); + $org = $this->organizationRepository->find($orgId); + if (!$org) { + throw $this->createNotFoundException(self::NOT_FOUND); + } + $user = $this->userRepository->find($id); + if (!$user) { + throw $this->createNotFoundException(self::NOT_FOUND); + } + $uo = $this->uoRepository->findOneBy(['users' => $user, + 'organization' => $org]); + if (!$uo) { + throw $this->createNotFoundException(self::NOT_FOUND); + } + $status = $request->get('status'); + if ($status === 'deactivate') { + $uo->setIsActive(false); + $this->userOrganizationAppService->deactivateAllUserOrganizationsAppLinks($uo); + $this->entityManager->persist($uo); + $this->entityManager->flush(); + $data = ['user' => $user, + 'organization' => $org]; + $this->organizationsService->notifyOrganizationAdmins($data, "USER_DEACTIVATED"); + $this->logger->notice("User Organizaton deactivated " . $user->getUserIdentifier()); + $this->actionService->createAction("Deactivate user in organization", $actingUser, $org, $org->getName() . " for user " . $user->getUserIdentifier()); + return new JsonResponse(['status' => 'deactivated'], Response::HTTP_OK); + } + if($status === "activate"){ + $uo->setIsActive(true); + $this->entityManager->persist($uo); + $this->entityManager->flush(); + $this->actionService->createAction("Activate user in organization", $actingUser, $org, $org->getName() . " for user " . $user->getUserIdentifier()); + $data = ['user' => $user, + 'organization' => $org]; + $this->organizationsService->notifyOrganizationAdmins($data, "USER_ACTIVATED"); + return new JsonResponse(['status' => 'activated'], Response::HTTP_OK); + } } - $user = $this->userRepository->find($id); - if (!$user) { - throw $this->createNotFoundException(self::NOT_FOUND); - } - $uo = $this->uoRepository->findOneBy(['users' => $user, - 'organization' => $org, - 'isActive' => true]); - if (!$uo) { - throw $this->createNotFoundException(self::NOT_FOUND); - } - $uo->setIsActive(false); - $this->userOrganizationAppService->deactivateAllUserOrganizationsAppLinks($uo); - $data = ['user' => $user, - 'organization' => $org]; - $this->organizationsService->notifyOrganizationAdmins($data, "USER_DEACTIVATED"); - $this->entityManager->persist($uo); - $this->entityManager->flush(); - $this->actionService->createAction("Deactivate user in organization", $actingUser, $org, $org->getName() . " for user " . $user->getUserIdentifier()); - - return new Response('', Response::HTTP_NO_CONTENT); //204 + }catch (\Exception $exception){ + $this->logger->error($exception->getMessage()); } - - throw $this->createAccessDeniedException(self::ACCESS_DENIED); - } - - //TODO : MONOLOG - #[Route('/organization/activate/{id}', name: 'activate_organization', methods: ['GET', 'POST'])] - public function activateUserInOrganization(int $id, Request $request): Response - { - $this->denyAccessUnlessGranted('ROLE_ADMIN'); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); - if ($this->userService->hasAccessTo($actingUser, true)) { - $orgId = $request->get('organizationId'); - $org = $this->organizationRepository->find($orgId); - if (!$org) { - throw $this->createNotFoundException(self::NOT_FOUND); - } - $user = $this->userRepository->find($id); - if (!$user) { - throw $this->createNotFoundException(self::NOT_FOUND); - } - $uo = $this->uoRepository->findOneBy(['users' => $user, - 'organization' => $org, - 'isActive' => false]); - if (!$uo) { - throw $this->createNotFoundException(self::NOT_FOUND); - } - $uo->setIsActive(true); - $this->entityManager->persist($uo); - $this->entityManager->flush(); - $this->actionService->createAction("Activate user in organization", $actingUser, $org, $org->getName() . " for user " . $user->getUserIdentifier()); - $data = ['user' => $user, - 'organization' => $org]; - $this->organizationsService->notifyOrganizationAdmins($data, "USER_ACTIVATED"); - - return $this->redirectToRoute('user_index'); - } - - throw $this->createAccessDeniedException(self::ACCESS_DENIED); + throw $this->createNotFoundException(self::NOT_FOUND); } //TODO : MONOLOG + remove picture from bucket