diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index 7ecb5e4..db6adba 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -43,35 +43,38 @@ class OrganizationController extends AbstractController private readonly ActionService $actionService, private readonly UserOrganizationService $userOrganizationService, private readonly OrganizationsRepository $organizationsRepository, - private readonly AwsService $awsService, private readonly LoggerService $loggerService, private readonly LoggerInterface $logger) + private readonly LoggerService $loggerService) { } #[Route(path: '/', name: 'index', methods: ['GET'])] public function index(): Response { - $this->denyAccessUnlessGranted('ROLE_ADMIN'); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); - if($this->userService->hasAccessTo($actingUser, true)){ - $orgCount = $this->organizationsRepository->count(['isDeleted' => false]); - if(!$this->isGranted("ROLE_SUPER_ADMIN")){ - $userUO = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $actingUser, 'isActive' => true]); - $uoAdmin = 0; - foreach($userUO as $u){ - if($this->userService->isAdminOfOrganization($u->getOrganization())){ - $uoAdmin++; - } - } - if($uoAdmin === 1){ - return $this->redirectToRoute('organization_show', ['id' => $userUO[0]->getOrganization()->getId()]); - } + $this->denyAccessUnlessGranted('ROLE_USER'); + $actingUser = $this->getUser(); + if ($this->userService->isAdminInAnyOrganization($actingUser)) { + $orgs = $this->userOrganizationService->getAdminOrganizationsForUser($actingUser); + } + if (!$this->isGranted("ROLE_ADMIN") && !empty($orgs)) { + if (count($orgs) === 1) { + return $this->redirectToRoute('organization_show', ['id' => $orgs[0]->getId()]); } return $this->render('organization/index.html.twig', [ - 'hasOrganizations' => $orgCount > 0 + 'hasOrganizations' => $orgs > 1 ]); } - $this->loggerService->logAccessDenied($actingUser->getId()); - throw new AccessDeniedHttpException('Access denied'); + if ($this->isgranted("ROLE_ADMIN")) { + return $this->render('organization/index.html.twig', [ + 'hasOrganizations' => $orgs > 1 + ]); + } + $this->loggerService->logEntityNotFound('Organization', [ + 'user_id' => $actingUser->getUserIdentifier(), + 'message' => 'No admin organizations found for user in organization index' + ], $actingUser->getUserIdentifier()); + $this->addFlash('danger', 'Erreur, aucune organisation trouvée.'); + return $this->redirectToRoute('home'); + } @@ -79,7 +82,7 @@ class OrganizationController extends AbstractController public function new(Request $request): Response { $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); if ($request->isMethod('POST')) { $organization = new Organizations(); $form = $this->createForm(OrganizationForm::class, $organization); @@ -117,7 +120,7 @@ class OrganizationController extends AbstractController public function edit(Request $request, $id): Response { $this->denyAccessUnlessGranted('ROLE_ADMIN'); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); $organization = $this->organizationsRepository->find($id); if (!$organization) { $this->loggerService->logEntityNotFound('Organization', [ @@ -184,7 +187,7 @@ class OrganizationController extends AbstractController { $this->denyAccessUnlessGranted('ROLE_USER'); $organization = $this->organizationsRepository->find($id); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); if (!$organization) { $this->loggerService->logEntityNotFound('Organization', [ 'org_id' => $id, @@ -220,7 +223,7 @@ class OrganizationController extends AbstractController public function delete($id): Response { $this->denyAccessUnlessGranted("ROLE_ADMIN"); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); $organization = $this->organizationsRepository->find($id); if (!$organization) { $this->loggerService->logEntityNotFound('Organization', [ @@ -257,7 +260,7 @@ class OrganizationController extends AbstractController public function deactivate($id): Response { $this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN"); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); $organization = $this->organizationsRepository->find($id); if (!$organization) { $this->loggerService->logEntityNotFound('Organization', [ @@ -281,7 +284,7 @@ class OrganizationController extends AbstractController public function activate($id): Response { $this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN"); - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); $organization = $this->organizationsRepository->find($id); if (!$organization) { $this->loggerService->logEntityNotFound('Organization', [ @@ -325,7 +328,7 @@ class OrganizationController extends AbstractController ->setParameter('email', '%' . $filters['email'] . '%'); } if (!$this->isGranted('ROLE_ADMIN')) { - $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); + $actingUser = $this->getUser(); $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $actingUser]); $allowedOrgIds = []; diff --git a/src/Service/UserOrganizationService.php b/src/Service/UserOrganizationService.php index b5caca9..11d186d 100644 --- a/src/Service/UserOrganizationService.php +++ b/src/Service/UserOrganizationService.php @@ -55,6 +55,17 @@ readonly class UserOrganizationService } + public function getAdminOrganizationsForUser(User $user): array + { + $adminRole = 'ADMIN'; // Assuming 'ADMIN' is the role name for administrators + $uos = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user, 'role' => $adminRole, 'isActive' => true]); + $adminOrgs = []; + foreach ($uos as $uo) { + $adminOrgs[] = $uo->getOrganization(); + } + return $adminOrgs; + } + }