51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\Organizations;
|
|
use App\Entity\User;
|
|
use App\Entity\UsersOrganizations;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<UsersOrganizations>
|
|
*/
|
|
class UsersOrganizationsRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, UsersOrganizations::class);
|
|
}
|
|
|
|
/**
|
|
* Checks if an acting user has administrative rights over a target user
|
|
* based on shared organizational memberships.
|
|
*/
|
|
public function isUserAdminOfTarget(User $actingUser, User $targetUser, $adminRole): bool
|
|
{
|
|
$qb = $this->createQueryBuilder('uo_acting');
|
|
|
|
return (bool) $qb
|
|
->select('COUNT(uo_acting.id)')
|
|
// We join the same table again to find the target user in the same organization
|
|
->innerJoin(
|
|
UsersOrganizations::class,
|
|
'uo_target',
|
|
'WITH',
|
|
'uo_target.organization = uo_acting.organization'
|
|
)
|
|
->where('uo_acting.users = :actingUser')
|
|
->andWhere('uo_acting.role = :role')
|
|
->andWhere('uo_acting.isActive = true')
|
|
->andWhere('uo_target.users = :targetUser')
|
|
->andWhere('uo_target.statut = :status')
|
|
->setParameter('actingUser', $actingUser)
|
|
->setParameter('targetUser', $targetUser)
|
|
->setParameter('role', $adminRole)
|
|
->setParameter('status', 'ACCEPTED')
|
|
->getQuery()
|
|
->getSingleScalarResult() > 0;
|
|
}
|
|
}
|