Refactor monolog of Application controller

This commit is contained in:
Charles 2025-12-09 11:58:16 +01:00
parent 4022e905a8
commit 0cd33e84f8
3 changed files with 73 additions and 10 deletions

View File

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Entity\Apps; use App\Entity\Apps;
use App\Entity\Organizations; use App\Entity\Organizations;
use App\Service\ActionService; use App\Service\ActionService;
use App\Service\LoggerService;
use App\Service\UserService; use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -16,7 +17,7 @@ use Symfony\Component\Routing\Attribute\Route;
class ApplicationController extends AbstractController 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()); $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
$application = $this->entityManager->getRepository(Apps::class)->find($id); $application = $this->entityManager->getRepository(Apps::class)->find($id);
if (!$application) { 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."); $this->addFlash('error', "L'application n'existe pas ou n'est pas reconnu.");
return $this->redirectToRoute('application_index'); return $this->redirectToRoute('application_index');
} }
@ -50,12 +55,28 @@ class ApplicationController extends AbstractController
if ($request->isMethod('POST')) { if ($request->isMethod('POST')) {
try{
$data = $request->request->all(); $data = $request->request->all();
$application->setName($data['name']); $application->setName($data['name']);
$application->setDescription($data['description']); $application->setDescription($data['description']);
$application->setDescriptionSmall($data['descriptionSmall']); $application->setDescriptionSmall($data['descriptionSmall']);
$this->entityManager->persist($application); $this->entityManager->persist($application);
$this->actionService->createAction("Modification de l'application ", $actingUser, null, $application->getId()); $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'); return $this->redirectToRoute('application_index');
} }
@ -66,18 +87,35 @@ class ApplicationController extends AbstractController
} }
#[Route(path: '/authorize/{id}', name: 'authorize', methods: ['POST'])] #[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'); $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
$application = $this->entityManager->getRepository(Apps::class)->find($id); $application = $this->entityManager->getRepository(Apps::class)->find($id);
if (!$application) { if (!$application) {
$this->loggerService->logEntityNotFound('Application', [
'applicationId' => $id,
'message' => "Application not found for authorization."
], $actingUser->getId());
throw $this->createNotFoundException("L'application n'existe pas."); throw $this->createNotFoundException("L'application n'existe pas.");
} }
$orgId = $request->get('organizationId'); $orgId = $request->get('organizationId');
$organization = $this->entityManager->getRepository(Organizations::Class)->find($orgId); $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); $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()); $this->actionService->createAction("Authorization d'accès", $actingUser, $organization, $application->getName());
return new Response('', Response::HTTP_OK); return new Response('', Response::HTTP_OK);
@ -90,12 +128,28 @@ class ApplicationController extends AbstractController
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
$application = $this->entityManager->getRepository(Apps::class)->find($id); $application = $this->entityManager->getRepository(Apps::class)->find($id);
if (!$application) { 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."); throw $this->createNotFoundException("L'application n'existe pas.");
} }
$orgId = $request->get('organizationId'); $orgId = $request->get('organizationId');
$organization = $this->entityManager->getRepository(Organizations::Class)->find($orgId); $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); $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()); $this->actionService->createAction("Authorization retirer", $actingUser, $organization, $application->getName());
return new Response('', Response::HTTP_OK); return new Response('', Response::HTTP_OK);

View File

@ -28,7 +28,7 @@ class NotificationController extends AbstractController
#[Route(path: '/', name: 'index', methods: ['GET'])] #[Route(path: '/', name: 'index', methods: ['GET'])]
public function index(): JsonResponse public function index(): JsonResponse
{ {
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
$notifications = $this->notificationRepository->findRecentByUser($user, 50); $notifications = $this->notificationRepository->findRecentByUser($user, 50);

View File

@ -251,4 +251,13 @@ readonly class LoggerService
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown', '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(),
]));
}
} }