display users for admin

This commit is contained in:
Charles 2025-08-22 12:12:50 +02:00
parent 3ca1446b91
commit 26637e497a
4 changed files with 76 additions and 23 deletions

View File

@ -46,10 +46,17 @@ class UserController extends AbstractController
$usersByOrganization += $noOrgUsers; $usersByOrganization += $noOrgUsers;
//Log action //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')) { } 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 { } else {
$usersByOrganization = []; $usersByOrganization = [];
} }

View File

@ -22,7 +22,7 @@ class UsersOrganizationsRepository extends ServiceEntityRepository
* *
* @return UsersOrganizations[] * @return UsersOrganizations[]
*/ */
public function findUsersWithOrganization(): array public function findUsersWithOrganization(array $organizationIds = null): array
{ {
$qb = $this->createQueryBuilder('uo') $qb = $this->createQueryBuilder('uo')
->addSelect('u', 'o') ->addSelect('u', 'o')
@ -38,6 +38,10 @@ class UsersOrganizationsRepository extends ServiceEntityRepository
->setParameter('uDeleted', false) ->setParameter('uDeleted', false)
->setParameter('oActive', true) ->setParameter('oActive', true)
->setParameter('oDeleted', false); ->setParameter('oDeleted', false);
if (!empty($organizationIds)) {
$qb->andWhere('o.id IN (:orgIds)')
->setParameter('orgIds', $organizationIds);
}
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }

View File

@ -20,5 +20,13 @@ use Symfony\Bundle\SecurityBundle\Security;
readonly class UserOrganizationService readonly class UserOrganizationService
{ {
public function __construct(
private EntityManagerInterface $entityManager,
private UserService $userService,
private ActionService $actionService,
private Security $security
) {
}
} }

View File

@ -20,8 +20,8 @@ class UserService
public const NOT_FOUND = 'Entity not found'; public const NOT_FOUND = 'Entity not found';
public function __construct(private readonly EntityManagerInterface $entityManager, public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly Security $security, private readonly Security $security,
) )
{ {
// Constructor logic if needed // Constructor logic if needed
@ -31,7 +31,8 @@ class UserService
* Generate a random password for a new user until they set their own. * Generate a random password for a new user until they set their own.
* @throws RandomException * @throws RandomException
*/ */
public function generateRandomPassword(): string{ public function generateRandomPassword(): string
{
$length = 50; // Length of the password $length = 50; // Length of the password
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+'; $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+';
$charactersLength = strlen($characters); $charactersLength = strlen($characters);
@ -77,19 +78,20 @@ class UserService
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function hasAccessTo(User $user): bool{ public function hasAccessTo(User $user): bool
if($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()){ {
if ($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()) {
return true; return true;
} }
$userOrganization = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['user' => $user]); $userOrganization = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['user' => $user]);
if($userOrganization) { if ($userOrganization) {
foreach ($userOrganization as $uo) { foreach ($userOrganization as $uo) {
if ($this->isAdminOfOrganization($uo)) { if ($this->isAdminOfOrganization($uo)) {
return true; return true;
} }
} }
} }
if($this->security->isGranted('ROLE_SUPER_ADMIN')){ if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
return true; return true;
} }
return false; return false;
@ -106,13 +108,15 @@ class UserService
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool{ public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool
{
$actingUser = $this->getUserByIdentifier($this->security->getUser()->getUserIdentifier()); $actingUser = $this->getUserByIdentifier($this->security->getUser()->getUserIdentifier());
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['user' => $actingUser]); $uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['user' => $actingUser]);
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['role' => 'ADMIN']); $roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['role' => 'ADMIN']);
if ($uo){ if ($uo) {
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, $uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo,
'role'=> $roleAdmin]); 'role' => $roleAdmin,
'isActive' => true]);
if ($uoa && $this->security->isGranted('ROLE_ADMIN')) { if ($uoa && $this->security->isGranted('ROLE_ADMIN')) {
return true; return true;
} }
@ -120,6 +124,36 @@ class UserService
return false; 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. * Get the user by their identifier.
* *
@ -129,7 +163,7 @@ class UserService
*/ */
public function getUserByIdentifier(string $userIdentifier): ?User 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) { if (!$user) {
throw new EntityNotFoundException(self::NOT_FOUND); throw new EntityNotFoundException(self::NOT_FOUND);
} }
@ -152,22 +186,22 @@ class UserService
continue; continue;
} }
$orgId = $org->getId(); $orgId = $org->getId();
$orgName = $org->getName(); $orgName = $org->getName();
if (!isset($grouped[$orgId])) { if (!isset($grouped[$orgId])) {
$grouped[$orgId] = [ $grouped[$orgId] = [
'id' => $orgId, 'id' => $orgId,
'name' => $orgName, 'name' => $orgName,
'users' => [], 'users' => [],
]; ];
} }
$user = $userOrg->getUsers(); $user = $userOrg->getUsers();
$grouped[$orgId]['users'][] = [ $grouped[$orgId]['users'][] = [
'entity' => $user, 'entity' => $user,
'connected' => $this->isUserConnected($user->getUserIdentifier()), 'connected' => $this->isUserConnected($user->getUserIdentifier()),
'isActive' => (bool) $userOrg->isActive() 'isActive' => (bool)$userOrg->isActive()
]; ];
} }
@ -183,14 +217,14 @@ class UserService
public function formatNoOrgUsersAsAssoc(array $noOrgUsers): array public function formatNoOrgUsersAsAssoc(array $noOrgUsers): array
{ {
$group = [ $group = [
'id' => null, 'id' => null,
'name' => 'Utilisateurs', 'name' => 'Utilisateurs',
'users' => [], 'users' => [],
]; ];
foreach ($noOrgUsers as $user) { foreach ($noOrgUsers as $user) {
$group['users'][] = [ $group['users'][] = [
'entity' => $user, 'entity' => $user,
'connected' => $this->isUserConnected($user->getUserIdentifier()), 'connected' => $this->isUserConnected($user->getUserIdentifier()),
]; ];
} }