helper function

This commit is contained in:
Charles 2025-07-28 10:23:07 +02:00
parent 4aadaa351a
commit f1b953d005
1 changed files with 34 additions and 1 deletions

View File

@ -3,10 +3,16 @@
namespace App\Service; 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 class UserService
{ {
public function __construct() public function __construct(private readonly EntityManagerInterface $entityManager)
{ {
// Constructor logic if needed // Constructor logic if needed
} }
@ -26,4 +32,31 @@ class UserService
return $randomPassword; 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()]));
}
} }