Update role logic for action display

This commit is contained in:
Charles 2026-02-11 15:13:47 +01:00
parent 4fc059b2a5
commit d089815069
1 changed files with 13 additions and 9 deletions

View File

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Entity\Actions; use App\Entity\Actions;
use App\Entity\Organizations; use App\Entity\Organizations;
use App\Service\ActionService; use App\Service\ActionService;
use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -15,21 +16,24 @@ class ActionController extends AbstractController
{ {
public function __construct( public function __construct(
private EntityManagerInterface $entityManager, private EntityManagerInterface $entityManager,
private ActionService $actionService private ActionService $actionService, private readonly UserService $userService
) { ) {
} }
#[Route('/organization/{id}/activities-ajax', name: 'app_organization_activities_ajax', methods: ['GET'])] #[Route('/organization/{id}/activities-ajax', name: 'app_organization_activities_ajax', methods: ['GET'])]
public function fetchActivitiesAjax(Organizations $organization): JsonResponse public function fetchActivitiesAjax(Organizations $organization): JsonResponse
{ {
$this->denyAccessUnlessGranted('ROLE_ADMIN'); $this->denyAccessUnlessGranted('ROLE_USER');
$actions = $this->entityManager->getRepository(Actions::class)->findBy( if($this->userService->isAdminOfOrganization($organization)){
['Organization' => $organization], $actions = $this->entityManager->getRepository(Actions::class)->findBy(
['date' => 'DESC'], ['Organization' => $organization],
10 ['date' => 'DESC'],
); 10
$formattedActivities = $this->actionService->formatActivities($actions); );
$formattedActivities = $this->actionService->formatActivities($actions);
return new JsonResponse($formattedActivities); return new JsonResponse($formattedActivities);
}
return new JsonResponse(['error' => 'You are not authorized to access this page.'], 403);
} }
} }