Fused activate and deactived user for organization route

This commit is contained in:
Charles 2025-11-26 09:20:05 +01:00
parent f0ae5a8c8a
commit c673fcd83b
2 changed files with 50 additions and 70 deletions

View File

@ -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) => {

View File

@ -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,12 +323,11 @@ 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());
try {
if ($this->userService->hasAccessTo($actingUser, true)) {
$orgId = $request->get('organizationId');
$org = $this->organizationRepository->find($orgId);
@ -339,48 +339,24 @@ class UserController extends AbstractController
throw $this->createNotFoundException(self::NOT_FOUND);
}
$uo = $this->uoRepository->findOneBy(['users' => $user,
'organization' => $org,
'isActive' => true]);
'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->entityManager->persist($uo);
$this->entityManager->flush();
$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 Response('', Response::HTTP_NO_CONTENT); //204
}
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);
return new JsonResponse(['status' => 'deactivated'], Response::HTTP_OK);
}
if($status === "activate"){
$uo->setIsActive(true);
$this->entityManager->persist($uo);
$this->entityManager->flush();
@ -388,11 +364,13 @@ class UserController extends AbstractController
$data = ['user' => $user,
'organization' => $org];
$this->organizationsService->notifyOrganizationAdmins($data, "USER_ACTIVATED");
return $this->redirectToRoute('user_index');
return new JsonResponse(['status' => 'activated'], Response::HTTP_OK);
}
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
}
}catch (\Exception $exception){
$this->logger->error($exception->getMessage());
}
throw $this->createNotFoundException(self::NOT_FOUND);
}
//TODO : MONOLOG + remove picture from bucket