Filter user in user picker by flags, method to get/filter user by flag in UserRepository

This commit is contained in:
2018-09-14 15:41:50 +02:00
parent 24dae9bc9c
commit b3d23bbed8
3 changed files with 71 additions and 2 deletions

View File

@@ -17,6 +17,9 @@
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Entity\User;
/**
*
*
@@ -46,6 +49,57 @@ class UserRepository extends \Doctrine\ORM\EntityRepository
return $qb->getQuery()->getSingleResult();
}
/**
* Get the users having a specific flags
*
* If provided, only the users amongst "filtered users" are searched. This
* allows to make a first search amongst users based on role and center
* and, then filter those users having some flags.
*
* @param \Chill\MainBundle\Entity\User[] $amongstUsers
*/
public function findUsersHavingFlags($flag, $amongstUsers = [])
{
$gcs = $this->_em->createQuery("SELECT DISTINCT gc "
. "FROM ".GroupCenter::class." gc "
. "JOIN gc.permissionsGroup pg "
. "WHERE "
. "JSONB_EXISTS_IN_ARRAY(pg.flags, :flag) = :true ")
->setParameters([
'true' => true,
'flag' => $flag
])
->getResult();
if (count($gcs) === 0) {
return [];
}
$qb = $this->_em->createQueryBuilder();
$qb
->select('DISTINCT u')
->from(User::class, 'u')
->where("u.enabled = 'TRUE'")
;
$orx = $qb->expr()->orX();
foreach($gcs as $i => $gc) {
$orx->add(':gc_'.$i.' MEMBER OF u.groupCenters');
$qb->setParameter('gc_'.$i, $gc);
}
$qb->andWhere($orx);
if (count($amongstUsers) > 0) {
$qb
->andWhere($qb->expr()->in('u', ':amongstUsers'))
->setParameter('amongstUsers', $amongstUsers)
;
}
return $qb->getQuery()->getResult();
}
protected function queryByUsernameOrEmail($pattern)
{
$qb = $this->createQueryBuilder('u');