Log actions

This commit is contained in:
Charles 2025-08-07 10:06:15 +02:00
parent 3894d72439
commit b81b168ec3
1 changed files with 30 additions and 35 deletions

View File

@ -9,6 +9,7 @@ use App\Entity\Roles;
use App\Entity\User; use App\Entity\User;
use App\Form\UserForm; use App\Form\UserForm;
use App\Entity\UsersOrganizations; use App\Entity\UsersOrganizations;
use App\Service\ActionService;
use App\Service\UserOrganizationService; use App\Service\UserOrganizationService;
use App\Service\UserService; use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -27,7 +28,7 @@ class UserController extends AbstractController
public function __construct( public function __construct(
private readonly UserOrganizationService $userOrganizationService, private readonly UserOrganizationService $userOrganizationService,
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
private readonly UserService $userService) private readonly UserService $userService, private readonly ActionService $actionService)
{ {
} }
@ -39,15 +40,14 @@ class UserController extends AbstractController
{ {
if ($this->isGranted('ROLE_SUPER_ADMIN')) { if ($this->isGranted('ROLE_SUPER_ADMIN')) {
$usersByOrganization = $this->userOrganizationService->getActiveUsersGroupedByOrganization(); $usersByOrganization = $this->userOrganizationService->getActiveUsersGroupedByOrganization();
// dd($usersByOrganization); } else {
} else{
$user = $this->getUser(); $user = $this->getUser();
if (!$user) { if (!$user) {
return $this->redirectToRoute('app_login'); return $this->redirectToRoute('app_login');
} }
$userIdentifier = $user->getUserIdentifier(); $userIdentifier = $user->getUserIdentifier();
$organizations = $this->entityManager->getRepository(UsersOrganizations::class)->findOrganizationsByUserEmailAndRoleName($userIdentifier, 'ADMIN'); $organizations = $this->entityManager->getRepository(UsersOrganizations::class)->findOrganizationsByUserEmailAndRoleName($userIdentifier, 'ADMIN');
if(!$organizations) { if (!$organizations) {
// if user is not admin in any organization, throw access denied // if user is not admin in any organization, throw access denied
throw $this->createNotFoundException(self::ACCESS_DENIED); throw $this->createNotFoundException(self::ACCESS_DENIED);
} }
@ -73,9 +73,9 @@ class UserController extends AbstractController
if (!$user) { if (!$user) {
throw $this->createNotFoundException(self::NOT_FOUND); throw $this->createNotFoundException(self::NOT_FOUND);
} }
if($request->query->has('organizationId')) { if ($request->query->has('organizationId')) {
$userOrganizations = $this->userOrganizationService->getUserOrganizations($user, $request->query->get('organizationId')); $userOrganizations = $this->userOrganizationService->getUserOrganizations($user, $request->query->get('organizationId'));
}else{ } else {
$userOrganizations = $this->userOrganizationService->getUserOrganizations($user); $userOrganizations = $this->userOrganizationService->getUserOrganizations($user);
} }
@ -119,18 +119,16 @@ class UserController extends AbstractController
$uo->setUsers($data); $uo->setUsers($data);
//log the action //log the action
$action = new Actions(); $user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$action->setActionType('Création utilisateur'); $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$action->setUsers($this->getUser()); $this->actionService->createAction("Création d'une organisation", $user, $organization, "{$user->getIdentifier()} à ajouter l'utilisateur {$data->getUserIdentifier()} à l'organisation {$organization->getName()}");
$action->setOrganization($organization);
$this->entityManager->persist($uo); $this->entityManager->persist($uo);
}else{ } else {
$action = new Actions(); $user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$action->setActionType('Création utilisateur'); $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$action->setUsers($this->getUser()); $this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} à ajouter l'utilisateur {$data->getUserIdentifier()} sans organisation");
} }
$this->entityManager->persist($data); $this->entityManager->persist($data);
$this->entityManager->persist($action);
$this->entityManager->flush(); $this->entityManager->flush();
@ -171,10 +169,10 @@ class UserController extends AbstractController
//Persist changes to the user entity //Persist changes to the user entity
$entityManager->persist($user); $entityManager->persist($user);
//Log the action //Log the action
$action = new Actions();
$action->setActionType('Modification utilisateur'); $user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$action->setUsers($this->getUser()); $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$entityManager->persist($action); $this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a modifié l'utilisateur {$user->getUserIdentifier()}");
$entityManager->flush(); $entityManager->flush();
//Redirect to user profile after successful edit //Redirect to user profile after successful edit
@ -210,10 +208,9 @@ class UserController extends AbstractController
$user->setIsDeleted(true); $user->setIsDeleted(true);
$entityManager->persist($user); $entityManager->persist($user);
// Log the action // Log the action
$action = new Actions(); $user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$action->setActionType('Suppression utilisateur'); $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$action->setUsers($this->getUser()); $this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a supprimé l'utilisateur {$user->getUserIdentifier()}");
$entityManager->persist($action);
$entityManager->flush(); $entityManager->flush();
return $this->redirectToRoute('user_index'); return $this->redirectToRoute('user_index');
@ -264,10 +261,9 @@ class UserController extends AbstractController
$user->setIsActive(false); $user->setIsActive(false);
$entityManager->persist($user); $entityManager->persist($user);
// Log the action // Log the action
$action = new Actions(); $user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$action->setActionType('Désactivation utilisateur'); $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$action->setUsers($this->getUser()); $this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a désactivé l'utilisateur {$user->getUserIdentifier()}");
$entityManager->persist($action);
$entityManager->flush(); $entityManager->flush();
return $this->redirectToRoute('user_index'); return $this->redirectToRoute('user_index');
} }
@ -298,11 +294,10 @@ class UserController extends AbstractController
$selectedApps = $request->request->all('applications'); $selectedApps = $request->request->all('applications');
// order in important here. apps MUST be before roles // order in important here. apps MUST be before roles
$this->userOrganizationService->setUserOrganizationsApps($user, $organization,$selectedApps); $this->userOrganizationService->setUserOrganizationsApps($user, $organization, $selectedApps);
$this->userOrganizationService->setUserOrganizations($user, $organization, $selectedRoles); $this->userOrganizationService->setUserOrganizations($user, $organization, $selectedRoles);
// Redirect to the user profile after successful update // Redirect to the user profile after successful update
return $this->redirectToRoute('user_show', ['id' => $user->getId()]); return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
} }