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\Form\UserForm;
use App\Entity\UsersOrganizations;
use App\Service\ActionService;
use App\Service\UserOrganizationService;
use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface;
@ -27,7 +28,7 @@ class UserController extends AbstractController
public function __construct(
private readonly UserOrganizationService $userOrganizationService,
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')) {
$usersByOrganization = $this->userOrganizationService->getActiveUsersGroupedByOrganization();
// dd($usersByOrganization);
} else{
} else {
$user = $this->getUser();
if (!$user) {
return $this->redirectToRoute('app_login');
}
$userIdentifier = $user->getUserIdentifier();
$organizations = $this->entityManager->getRepository(UsersOrganizations::class)->findOrganizationsByUserEmailAndRoleName($userIdentifier, 'ADMIN');
if(!$organizations) {
if (!$organizations) {
// if user is not admin in any organization, throw access denied
throw $this->createNotFoundException(self::ACCESS_DENIED);
}
@ -73,9 +73,9 @@ class UserController extends AbstractController
if (!$user) {
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'));
}else{
} else {
$userOrganizations = $this->userOrganizationService->getUserOrganizations($user);
}
@ -119,18 +119,16 @@ class UserController extends AbstractController
$uo->setUsers($data);
//log the action
$action = new Actions();
$action->setActionType('Création utilisateur');
$action->setUsers($this->getUser());
$action->setOrganization($organization);
$user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$this->actionService->createAction("Création d'une organisation", $user, $organization, "{$user->getIdentifier()} à ajouter l'utilisateur {$data->getUserIdentifier()} à l'organisation {$organization->getName()}");
$this->entityManager->persist($uo);
}else{
$action = new Actions();
$action->setActionType('Création utilisateur');
$action->setUsers($this->getUser());
} else {
$user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$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($action);
$this->entityManager->flush();
@ -171,10 +169,10 @@ class UserController extends AbstractController
//Persist changes to the user entity
$entityManager->persist($user);
//Log the action
$action = new Actions();
$action->setActionType('Modification utilisateur');
$action->setUsers($this->getUser());
$entityManager->persist($action);
$user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a modifié l'utilisateur {$user->getUserIdentifier()}");
$entityManager->flush();
//Redirect to user profile after successful edit
@ -210,10 +208,9 @@ class UserController extends AbstractController
$user->setIsDeleted(true);
$entityManager->persist($user);
// Log the action
$action = new Actions();
$action->setActionType('Suppression utilisateur');
$action->setUsers($this->getUser());
$entityManager->persist($action);
$user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a supprimé l'utilisateur {$user->getUserIdentifier()}");
$entityManager->flush();
return $this->redirectToRoute('user_index');
@ -264,10 +261,9 @@ class UserController extends AbstractController
$user->setIsActive(false);
$entityManager->persist($user);
// Log the action
$action = new Actions();
$action->setActionType('Désactivation utilisateur');
$action->setUsers($this->getUser());
$entityManager->persist($action);
$user = $this->getUser() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $user->getUserIdentifier()]);
$this->actionService->createAction("Création d'une organisation",$user, null, "{$user->getIdentifier()} a désactivé l'utilisateur {$user->getUserIdentifier()}");
$entityManager->flush();
return $this->redirectToRoute('user_index');
}
@ -298,11 +294,10 @@ class UserController extends AbstractController
$selectedApps = $request->request->all('applications');
// 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);
// Redirect to the user profile after successful update
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
}
@ -362,14 +357,14 @@ class UserController extends AbstractController
#[Route('/organizationDeactivate/{id}', name: 'organization_deactivate', requirements: ['id' => '\d+'], methods: ['GET'])]
public function deactivateUserOrganization(int $id, Request $request, EntityManagerInterface $entityManager): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$userOrganization = $entityManager->getRepository(UsersOrganizations::class)->find($id) ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $userOrganization->getUsers() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$organization = $userOrganization->getOrganization() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$userOrganization = $entityManager->getRepository(UsersOrganizations::class)->find($id) ?? throw $this->createNotFoundException(self::NOT_FOUND);
$user = $userOrganization->getUsers() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$organization = $userOrganization->getOrganization() ?? throw $this->createNotFoundException(self::NOT_FOUND);
$this->userOrganizationService->deactivateAllUserRoles($user, $organization);
$this->userOrganizationService->deactivateAllUserRoles($user, $organization);
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
}
}