display users for admin
This commit is contained in:
parent
3ca1446b91
commit
26637e497a
|
|
@ -49,7 +49,14 @@ class UserController extends AbstractController
|
|||
$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
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,7 +78,8 @@ class UserService
|
|||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function hasAccessTo(User $user): bool{
|
||||
public function hasAccessTo(User $user): bool
|
||||
{
|
||||
if ($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -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) {
|
||||
$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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue