display users for admin
This commit is contained in:
parent
3ca1446b91
commit
26637e497a
|
|
@ -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 = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue