From f1b953d00590479f302a35372e14ae5b30ed505c Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 28 Jul 2025 10:23:07 +0200 Subject: [PATCH] helper function --- src/Service/UserService.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 9c9b1cd..61eaf5a 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -3,10 +3,16 @@ namespace App\Service; +use App\Entity\Organizations; +use App\Entity\Roles; +use App\Entity\User; +use App\Entity\UsersOrganizations; +use Doctrine\ORM\EntityManagerInterface; + class UserService { - public function __construct() + public function __construct(private readonly EntityManagerInterface $entityManager) { // Constructor logic if needed } @@ -26,4 +32,31 @@ class UserService return $randomPassword; } + + /** + * Check if the user is an admin in the given organization. + * + * @param int $userId + * @param int $organizationId + * @return bool + */ + public function isUserAdminInOrganization(int $userId, int $organizationId): bool + { + $user = $this->entityManager->getRepository(User::class)->find($userId); + if (!$user) { + return false; + } + $organization = $this->entityManager->getRepository(Organizations::class)->find($organizationId); + if (!$organization) { + return false; + } + $roleAdmin = $this->entityManager->getRepository(Roles::class)->findBy(['name'=> 'ADMIN']); + + // Check if the user is an admin in the organization + return empty($this->entityManager->getRepository(UsersOrganizations::class)->findBy([ + 'userId' => $userId, + 'organizationId' => $organizationId, + 'roleId' => $roleAdmin[0]->getId()])); + + } }