From 26637e497af7138d432028d4d485fb7a59b6b4b2 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 22 Aug 2025 12:12:50 +0200 Subject: [PATCH] display users for admin --- src/Controller/UserController.php | 11 ++- .../UsersOrganizationsRepository.php | 6 +- src/Service/UserOrganizationService.php | 8 ++ src/Service/UserService.php | 74 ++++++++++++++----- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 471c41e..e524644 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -46,10 +46,17 @@ class UserController extends AbstractController $usersByOrganization += $noOrgUsers; //Log action - $this->actionService->createAction("View all users", $user, null, "All" ); + $this->actionService->createAction("View all users", $user, null, "All"); } elseif ($this->isGranted('ROLE_ADMIN')) { - dd("dsaf"); + $orgIds = $this->userService->getAdminOrganizationsIds($user); + if (empty($orgIds)) { + $usersByOrganization = []; + } else { + $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findUsersWithOrganization($orgIds); + $usersByOrganization = $this->userService->groupByOrganization($uo); + $this->actionService->createAction("View all users for organizations", $user, null, implode(", ", $orgIds)); + } } else { $usersByOrganization = []; } diff --git a/src/Repository/UsersOrganizationsRepository.php b/src/Repository/UsersOrganizationsRepository.php index 064f757..a1a5388 100644 --- a/src/Repository/UsersOrganizationsRepository.php +++ b/src/Repository/UsersOrganizationsRepository.php @@ -22,7 +22,7 @@ class UsersOrganizationsRepository extends ServiceEntityRepository * * @return UsersOrganizations[] */ - public function findUsersWithOrganization(): array + public function findUsersWithOrganization(array $organizationIds = null): array { $qb = $this->createQueryBuilder('uo') ->addSelect('u', 'o') @@ -38,6 +38,10 @@ class UsersOrganizationsRepository extends ServiceEntityRepository ->setParameter('uDeleted', false) ->setParameter('oActive', true) ->setParameter('oDeleted', false); + if (!empty($organizationIds)) { + $qb->andWhere('o.id IN (:orgIds)') + ->setParameter('orgIds', $organizationIds); + } return $qb->getQuery()->getResult(); } diff --git a/src/Service/UserOrganizationService.php b/src/Service/UserOrganizationService.php index 6ea9700..7900c31 100644 --- a/src/Service/UserOrganizationService.php +++ b/src/Service/UserOrganizationService.php @@ -20,5 +20,13 @@ use Symfony\Bundle\SecurityBundle\Security; readonly class UserOrganizationService { + public function __construct( + private EntityManagerInterface $entityManager, + private UserService $userService, + private ActionService $actionService, + private Security $security + ) { + } + } diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 40d744c..0cf2ef1 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -20,8 +20,8 @@ class UserService public const NOT_FOUND = 'Entity not found'; - public function __construct(private readonly EntityManagerInterface $entityManager, - private readonly Security $security, + public function __construct(private readonly EntityManagerInterface $entityManager, + private readonly Security $security, ) { // Constructor logic if needed @@ -31,7 +31,8 @@ class UserService * Generate a random password for a new user until they set their own. * @throws RandomException */ - public function generateRandomPassword(): string{ + public function generateRandomPassword(): string + { $length = 50; // Length of the password $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+'; $charactersLength = strlen($characters); @@ -77,19 +78,20 @@ class UserService * @return bool * @throws Exception */ - public function hasAccessTo(User $user): bool{ - if($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()){ + public function hasAccessTo(User $user): bool + { + if ($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()) { return true; } $userOrganization = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['user' => $user]); - if($userOrganization) { + if ($userOrganization) { foreach ($userOrganization as $uo) { if ($this->isAdminOfOrganization($uo)) { return true; } } } - if($this->security->isGranted('ROLE_SUPER_ADMIN')){ + if ($this->security->isGranted('ROLE_SUPER_ADMIN')) { return true; } return false; @@ -106,13 +108,15 @@ class UserService * @return bool * @throws Exception */ - public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool{ + public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool + { $actingUser = $this->getUserByIdentifier($this->security->getUser()->getUserIdentifier()); $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['user' => $actingUser]); $roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['role' => 'ADMIN']); - if ($uo){ + if ($uo) { $uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, - 'role'=> $roleAdmin]); + 'role' => $roleAdmin, + 'isActive' => true]); if ($uoa && $this->security->isGranted('ROLE_ADMIN')) { return true; } @@ -120,6 +124,36 @@ class UserService return false; } + /** + * Get the Organizations id where the user is admin + * + * @param User $user + * @return array + * @throws Exception + */ + public function getAdminOrganizationsIds(User $user): array + { + $orgIds = []; + try { + $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user]); + $roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']); + if ($uo) { + foreach ($uo as $u) { + $uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $u, + 'role' => $roleAdmin, + 'isActive' => true]); + if ($uoa && $this->security->isGranted('ROLE_ADMIN')) { + $orgIds[] = $u->getOrganization()->getId(); + } + } + } + } catch (EntityNotFoundException $e) { + throw new EntityNotFoundException("Error while fetching organizations ids where the user is admin"); + } + + return array_unique($orgIds); + } + /** * Get the user by their identifier. * @@ -129,7 +163,7 @@ class UserService */ public function getUserByIdentifier(string $userIdentifier): ?User { - $user = $this->entityManager->getRepository(User::class)->findOneBy(['userIdentifier' => $userIdentifier]); + $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $userIdentifier]); if (!$user) { throw new EntityNotFoundException(self::NOT_FOUND); } @@ -152,22 +186,22 @@ class UserService continue; } - $orgId = $org->getId(); + $orgId = $org->getId(); $orgName = $org->getName(); if (!isset($grouped[$orgId])) { $grouped[$orgId] = [ - 'id' => $orgId, - 'name' => $orgName, + 'id' => $orgId, + 'name' => $orgName, 'users' => [], ]; } $user = $userOrg->getUsers(); $grouped[$orgId]['users'][] = [ - 'entity' => $user, - 'connected' => $this->isUserConnected($user->getUserIdentifier()), - 'isActive' => (bool) $userOrg->isActive() + 'entity' => $user, + 'connected' => $this->isUserConnected($user->getUserIdentifier()), + 'isActive' => (bool)$userOrg->isActive() ]; } @@ -183,14 +217,14 @@ class UserService public function formatNoOrgUsersAsAssoc(array $noOrgUsers): array { $group = [ - 'id' => null, - 'name' => 'Utilisateurs', + 'id' => null, + 'name' => 'Utilisateurs', 'users' => [], ]; foreach ($noOrgUsers as $user) { $group['users'][] = [ - 'entity' => $user, + 'entity' => $user, 'connected' => $this->isUserConnected($user->getUserIdentifier()), ]; }