diff --git a/src/Controller/ApplicationController.php b/src/Controller/ApplicationController.php index 13d3cbf..a666a8e 100644 --- a/src/Controller/ApplicationController.php +++ b/src/Controller/ApplicationController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Entity\Apps; use App\Entity\Organizations; use App\Service\ActionService; +use App\Service\LoggerService; use App\Service\UserService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -16,7 +17,7 @@ use Symfony\Component\Routing\Attribute\Route; class ApplicationController extends AbstractController { - public function __construct(private readonly EntityManagerInterface $entityManager, private readonly UserService $userService, private readonly ActionService $actionService) + public function __construct(private readonly EntityManagerInterface $entityManager, private readonly UserService $userService, private readonly ActionService $actionService, private readonly LoggerService $loggerService) { } @@ -37,6 +38,10 @@ class ApplicationController extends AbstractController $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $application = $this->entityManager->getRepository(Apps::class)->find($id); if (!$application) { + $this->loggerService->logEntityNotFound('Application', [ + 'applicationId' => $id, + 'message' => "Application not found for editing." + ], $actingUser); $this->addFlash('error', "L'application n'existe pas ou n'est pas reconnu."); return $this->redirectToRoute('application_index'); } @@ -50,12 +55,28 @@ class ApplicationController extends AbstractController if ($request->isMethod('POST')) { - $data = $request->request->all(); - $application->setName($data['name']); - $application->setDescription($data['description']); - $application->setDescriptionSmall($data['descriptionSmall']); - $this->entityManager->persist($application); - $this->actionService->createAction("Modification de l'application ", $actingUser, null, $application->getId()); + try{ + $data = $request->request->all(); + $application->setName($data['name']); + $application->setDescription($data['description']); + $application->setDescriptionSmall($data['descriptionSmall']); + $this->entityManager->persist($application); + $this->actionService->createAction("Modification de l'application ", $actingUser->getId(), null, $application->getId()); + $this->loggerService->logApplicationInformation('Application Edited', [ + 'applicationId' => $application->getId(), + 'applicationName' => $application->getName(), + 'message' => "Application edited successfully." + ], $actingUser->getId()); + $this->addFlash('success', "L'application a été mise à jour avec succès."); + }catch (\Exception $e){ + $this->loggerService->logError('Application Edit Failed', [ + 'applicationId' => $application->getId(), + 'applicationName' => $application->getName(), + 'error' => $e->getMessage(), + 'message' => "Failed to edit application." + ], $actingUser); + $this->addFlash('error', "Une erreur est survenue lors de la mise à jour de l'application."); + } return $this->redirectToRoute('application_index'); } @@ -66,18 +87,35 @@ class ApplicationController extends AbstractController } #[Route(path: '/authorize/{id}', name: 'authorize', methods: ['POST'])] - public function authorize(int $id, Request $request) + public function authorize(int $id, Request $request): Response { $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $application = $this->entityManager->getRepository(Apps::class)->find($id); if (!$application) { + $this->loggerService->logEntityNotFound('Application', [ + 'applicationId' => $id, + 'message' => "Application not found for authorization." + ], $actingUser->getId()); throw $this->createNotFoundException("L'application n'existe pas."); } $orgId = $request->get('organizationId'); $organization = $this->entityManager->getRepository(Organizations::Class)->find($orgId); + if (!$organization) { + $this->loggerService->logEntityNotFound('Organization', [ + 'Organization_id' => $orgId, + 'message' => "Organization not found for authorization." + ], $actingUser->getId()); + throw $this->createNotFoundException("L'Organization n'existe pas."); + } $application->addOrganization($organization); + $this->loggerService->logApplicationInformation('Application Authorized', [ + 'applicationId' => $application->getId(), + 'applicationName' => $application->getName(), + 'organizationId' => $organization->getId(), + 'message' => "Application authorized for organization." + ], $actingUser->getId()); $this->actionService->createAction("Authorization d'accès", $actingUser, $organization, $application->getName()); return new Response('', Response::HTTP_OK); @@ -90,12 +128,28 @@ class ApplicationController extends AbstractController $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $application = $this->entityManager->getRepository(Apps::class)->find($id); if (!$application) { + $this->loggerService->logEntityNotFound('Application', [ + 'applicationId' => $id, + 'message' => "Application not found for authorization removal." + ], $actingUser->getId()); throw $this->createNotFoundException("L'application n'existe pas."); } $orgId = $request->get('organizationId'); $organization = $this->entityManager->getRepository(Organizations::Class)->find($orgId); + if (!$organization) { + $this->loggerService->logEntityNotFound('Organization', [ + 'Organization_id' => $orgId, + 'message' => "Organization not found for authorization removal." + ], $actingUser->getId()); + throw $this->createNotFoundException("L'Organization n'existe pas."); + } $application->removeOrganization($organization); - + $this->loggerService->logApplicationInformation('Application Authorized removed', [ + 'applicationId' => $application->getId(), + 'applicationName' => $application->getName(), + 'organizationId' => $organization->getId(), + 'message' => "Application authorized removed for organization." + ], $actingUser->getId()); $this->actionService->createAction("Authorization retirer", $actingUser, $organization, $application->getName()); return new Response('', Response::HTTP_OK); diff --git a/src/Controller/NotificationController.php b/src/Controller/NotificationController.php index 3c7ac0c..9195577 100644 --- a/src/Controller/NotificationController.php +++ b/src/Controller/NotificationController.php @@ -28,7 +28,7 @@ class NotificationController extends AbstractController #[Route(path: '/', name: 'index', methods: ['GET'])] public function index(): JsonResponse { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN'); $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $notifications = $this->notificationRepository->findRecentByUser($user, 50); diff --git a/src/Service/LoggerService.php b/src/Service/LoggerService.php index db25e2c..8e388d2 100644 --- a/src/Service/LoggerService.php +++ b/src/Service/LoggerService.php @@ -251,4 +251,13 @@ readonly class LoggerService 'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown', ])); } + + public function logApplicationInformation(string $string, array $array, int $actingUser) + { + $this->accessControlLogger->info($string, array_merge($array, [ + 'acting_user_id' => $actingUser, + 'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown', + 'timestamp' => $this->now(), + ])); + } }