From e536a5ebc5bf06a6e309220ea721f69b981b8d51 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 11 Feb 2026 15:22:11 +0100 Subject: [PATCH] update logic to fit new role rework --- src/Controller/IndexController.php | 8 ++++- src/Controller/OrganizationController.php | 36 +++++++++++++---------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index 5bfd14b..ae0e8b4 100644 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Service\UserService; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; @@ -11,10 +12,15 @@ use Symfony\Component\Routing\Attribute\Route; final class IndexController extends AbstractController { + public function __construct(private readonly UserService $userService) + { + } + #[Route('/', name: 'app_index')] public function index(): Response { - if ($this->isGranted('ROLE_ADMIN')) { + + if ($this->isGranted('ROLE_ADMIN') || ($this->isGranted('ROLE_USER') && $this->userService->isAdminInAnyOrganization($this->getUser()))) { return $this->redirectToRoute('organization_index'); } diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index aa6d27f..f3ce97e 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -52,29 +52,33 @@ class OrganizationController extends AbstractController { $this->denyAccessUnlessGranted('ROLE_USER'); $actingUser = $this->getUser(); - if ($this->userService->isAdminInAnyOrganization($actingUser)) { - $orgs = $this->userOrganizationService->getAdminOrganizationsForUser($actingUser); + + // 1. Super Admin Case: Just show the list + if ($this->isGranted("ROLE_ADMIN")) { + return $this->render('organization/index.html.twig', ['hasOrganizations' => true]); } - 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' => $orgs > 1 - ]); + + // 2. Organization Admin Case: Get their specific orgs + $orgs = $this->userOrganizationService->getAdminOrganizationsForUser($actingUser); + + // If exactly one org, jump straight to it + if (count($orgs) === 1) { + return $this->redirectToRoute('organization_show', ['id' => $orgs[0]->getId()]); } - if ($this->isgranted("ROLE_ADMIN")) { - return $this->render('organization/index.html.twig', [ - 'hasOrganizations' => true - ]); + + // If multiple orgs, show the list + if (count($orgs) > 1) { + return $this->render('organization/index.html.twig', ['hasOrganizations' => true]); } + + // 3. Fallback: No access/No orgs found $this->loggerService->logEntityNotFound('Organization', [ 'user_id' => $actingUser->getUserIdentifier(), - 'message' => 'No admin organizations found for user in organization index' + 'message' => 'No admin organizations found' ], $actingUser->getUserIdentifier()); - $this->addFlash('danger', 'Erreur, aucune organisation trouvée.'); - return $this->redirectToRoute('home'); + $this->addFlash('danger', 'Erreur, aucune organisation trouvée.'); + return $this->redirectToRoute('app_index'); }