From 1516e8c890fe8ee57a80e827b240cacce45be432 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 3 Sep 2025 10:13:53 +0200 Subject: [PATCH] Edit organization --- src/Controller/OrganizationController.php | 50 ++++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index 9d4b028..3eb3de1 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -73,12 +73,12 @@ class OrganizationController extends AbstractController if ($logoFile) { $this->organizationService->handleLogo($organization, $logoFile); } - try{ + try { $this->entityManager->persist($organization); $this->entityManager->flush(); $this->actionService->createAction("Create Organization", $actingUser, $organization, $organization->getName()); return $this->redirectToRoute('organization_index'); - }catch (Exception $e){ + } catch (Exception $e) { $this->addFlash('error', 'Error creating organization: ' . $e->getMessage()); } } @@ -93,5 +93,51 @@ class OrganizationController extends AbstractController ]); } + #[Route(path: '/edit/{id}', name: 'edit', methods: ['GET', 'POST'])] + public function edit(Request $request, $id) + { + $this->denyAccessUnlessGranted('ROLE_ADMIN'); + $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $organization = $this->entityManager->getRepository(Organizations::class)->find($id); + if (!$organization) { + $this->addFlash('error', self::NOT_FOUND); + return $this->redirectToRoute('organization_index'); + } + if (!$this->isGranted("ROLE_SUPER_ADMIN")) { + //check if the user is admin of the organization + $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['users' => $user, 'organization' => $organization]); + if (!$uo) { + $this->addFlash('error', self::ACCESS_DENIED); + return $this->redirectToRoute('organization_index'); + } + $roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']); + $uoaAdmin = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, 'role' => $roleAdmin]); + if (!$uoaAdmin) { + $this->addFlash('error', self::ACCESS_DENIED); + return $this->redirectToRoute('organization_index'); + } + } + $form = $this->createForm(OrganizationForm::class, $organization); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $logoFile = $form->get('logoUrl')->getData(); + if ($logoFile) { + $this->organizationsService->handleLogo($organization, $logoFile); + } + try { + $this->entityManager->flush(); + $this->actionService->createAction("Edit Organization", $actingUser, $organization, $organization->getName()); + return $this->redirectToRoute('organization_index'); + } catch (Exception $e) { + $this->addFlash('error', 'Error editing organization: ' . $e->getMessage()); + } + } + return $this->render('organization/edit.html.twig', [ + 'form' => $form->createView(), + 'organization' => $organization, + ]); + } + }