update index of org page load logic

This commit is contained in:
Charles 2026-02-11 14:34:29 +01:00
parent fe6e4b44e5
commit 184bfa2604
2 changed files with 40 additions and 26 deletions

View File

@ -43,35 +43,38 @@ class OrganizationController extends AbstractController
private readonly ActionService $actionService, private readonly ActionService $actionService,
private readonly UserOrganizationService $userOrganizationService, private readonly UserOrganizationService $userOrganizationService,
private readonly OrganizationsRepository $organizationsRepository, 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'])] #[Route(path: '/', name: 'index', methods: ['GET'])]
public function index(): Response public function index(): Response
{ {
$this->denyAccessUnlessGranted('ROLE_ADMIN'); $this->denyAccessUnlessGranted('ROLE_USER');
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
if($this->userService->hasAccessTo($actingUser, true)){ if ($this->userService->isAdminInAnyOrganization($actingUser)) {
$orgCount = $this->organizationsRepository->count(['isDeleted' => false]); $orgs = $this->userOrganizationService->getAdminOrganizationsForUser($actingUser);
if(!$this->isGranted("ROLE_SUPER_ADMIN")){ }
$userUO = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $actingUser, 'isActive' => true]); if (!$this->isGranted("ROLE_ADMIN") && !empty($orgs)) {
$uoAdmin = 0; if (count($orgs) === 1) {
foreach($userUO as $u){ return $this->redirectToRoute('organization_show', ['id' => $orgs[0]->getId()]);
if($this->userService->isAdminOfOrganization($u->getOrganization())){
$uoAdmin++;
}
}
if($uoAdmin === 1){
return $this->redirectToRoute('organization_show', ['id' => $userUO[0]->getOrganization()->getId()]);
}
} }
return $this->render('organization/index.html.twig', [ return $this->render('organization/index.html.twig', [
'hasOrganizations' => $orgCount > 0 'hasOrganizations' => $orgs > 1
]); ]);
} }
$this->loggerService->logAccessDenied($actingUser->getId()); if ($this->isgranted("ROLE_ADMIN")) {
throw new AccessDeniedHttpException('Access denied'); 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 public function new(Request $request): Response
{ {
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
if ($request->isMethod('POST')) { if ($request->isMethod('POST')) {
$organization = new Organizations(); $organization = new Organizations();
$form = $this->createForm(OrganizationForm::class, $organization); $form = $this->createForm(OrganizationForm::class, $organization);
@ -117,7 +120,7 @@ class OrganizationController extends AbstractController
public function edit(Request $request, $id): Response public function edit(Request $request, $id): Response
{ {
$this->denyAccessUnlessGranted('ROLE_ADMIN'); $this->denyAccessUnlessGranted('ROLE_ADMIN');
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
$organization = $this->organizationsRepository->find($id); $organization = $this->organizationsRepository->find($id);
if (!$organization) { if (!$organization) {
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
@ -184,7 +187,7 @@ class OrganizationController extends AbstractController
{ {
$this->denyAccessUnlessGranted('ROLE_USER'); $this->denyAccessUnlessGranted('ROLE_USER');
$organization = $this->organizationsRepository->find($id); $organization = $this->organizationsRepository->find($id);
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
if (!$organization) { if (!$organization) {
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
'org_id' => $id, 'org_id' => $id,
@ -220,7 +223,7 @@ class OrganizationController extends AbstractController
public function delete($id): Response public function delete($id): Response
{ {
$this->denyAccessUnlessGranted("ROLE_ADMIN"); $this->denyAccessUnlessGranted("ROLE_ADMIN");
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
$organization = $this->organizationsRepository->find($id); $organization = $this->organizationsRepository->find($id);
if (!$organization) { if (!$organization) {
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
@ -257,7 +260,7 @@ class OrganizationController extends AbstractController
public function deactivate($id): Response public function deactivate($id): Response
{ {
$this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN"); $this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN");
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
$organization = $this->organizationsRepository->find($id); $organization = $this->organizationsRepository->find($id);
if (!$organization) { if (!$organization) {
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
@ -281,7 +284,7 @@ class OrganizationController extends AbstractController
public function activate($id): Response public function activate($id): Response
{ {
$this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN"); $this->denyAccessUnlessGranted("ROLE_SUPER_ADMIN");
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
$organization = $this->organizationsRepository->find($id); $organization = $this->organizationsRepository->find($id);
if (!$organization) { if (!$organization) {
$this->loggerService->logEntityNotFound('Organization', [ $this->loggerService->logEntityNotFound('Organization', [
@ -325,7 +328,7 @@ class OrganizationController extends AbstractController
->setParameter('email', '%' . $filters['email'] . '%'); ->setParameter('email', '%' . $filters['email'] . '%');
} }
if (!$this->isGranted('ROLE_ADMIN')) { if (!$this->isGranted('ROLE_ADMIN')) {
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->getUser();
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $actingUser]); $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $actingUser]);
$allowedOrgIds = []; $allowedOrgIds = [];

View File

@ -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;
}
} }