85 lines
2.9 KiB
PHP
85 lines
2.9 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\ORM\Tools\Pagination\Paginator;
|
|
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;
|
|
}
|
|
|
|
public function findByOrganizationWithFilters(Organizations $org, int $page, int $size, array $filters = []): Paginator
|
|
{
|
|
$qb = $this->createQueryBuilder('uo')
|
|
->innerJoin('uo.users', 'u')
|
|
->where('uo.organization = :org')
|
|
->setParameter('org', $org);
|
|
|
|
// Apply filters
|
|
if (!empty($filters['name'])) {
|
|
$qb->andWhere('u.surname LIKE :name')
|
|
->setParameter('name', '%' . $filters['name'] . '%');
|
|
}
|
|
if (!empty($filters['prenom'])) {
|
|
$qb->andWhere('u.name LIKE :prenom')
|
|
->setParameter('prenom', '%' . $filters['prenom'] . '%');
|
|
}
|
|
if (!empty($filters['email'])) {
|
|
$qb->andWhere('u.email LIKE :email')
|
|
->setParameter('email', '%' . $filters['email'] . '%');
|
|
}
|
|
|
|
// Apply complex sorting
|
|
$qb->orderBy('uo.isActive', 'DESC')
|
|
->addOrderBy("CASE WHEN uo.statut = 'INVITED' THEN 0 ELSE 1 END", 'ASC');
|
|
|
|
// Pagination
|
|
$qb->setFirstResult(($page - 1) * $size)
|
|
->setMaxResults($size);
|
|
|
|
return new Paginator($qb);
|
|
}
|
|
}
|