update logic to fit new role rework

This commit is contained in:
Charles 2026-02-11 15:22:11 +01:00
parent 42bee789ba
commit e536a5ebc5
2 changed files with 27 additions and 17 deletions

View File

@ -2,6 +2,7 @@
namespace App\Controller; namespace App\Controller;
use App\Service\UserService;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
@ -11,10 +12,15 @@ use Symfony\Component\Routing\Attribute\Route;
final class IndexController extends AbstractController final class IndexController extends AbstractController
{ {
public function __construct(private readonly UserService $userService)
{
}
#[Route('/', name: 'app_index')] #[Route('/', name: 'app_index')]
public function index(): Response 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'); return $this->redirectToRoute('organization_index');
} }

View File

@ -52,29 +52,33 @@ class OrganizationController extends AbstractController
{ {
$this->denyAccessUnlessGranted('ROLE_USER'); $this->denyAccessUnlessGranted('ROLE_USER');
$actingUser = $this->getUser(); $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)) {
// 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) { if (count($orgs) === 1) {
return $this->redirectToRoute('organization_show', ['id' => $orgs[0]->getId()]); return $this->redirectToRoute('organization_show', ['id' => $orgs[0]->getId()]);
} }
return $this->render('organization/index.html.twig', [
'hasOrganizations' => $orgs > 1 // If multiple orgs, show the list
]); if (count($orgs) > 1) {
} return $this->render('organization/index.html.twig', ['hasOrganizations' => true]);
if ($this->isgranted("ROLE_ADMIN")) {
return $this->render('organization/index.html.twig', [
'hasOrganizations' => true
]);
} }
// 3. Fallback: No access/No orgs found
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
'user_id' => $actingUser->getUserIdentifier(), 'user_id' => $actingUser->getUserIdentifier(),
'message' => 'No admin organizations found for user in organization index' 'message' => 'No admin organizations found'
], $actingUser->getUserIdentifier()); ], $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');
} }