update logic to fit new role rework
This commit is contained in:
parent
42bee789ba
commit
e536a5ebc5
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue