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

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

View File

@ -7,4 +7,9 @@ Version 1.5.1
- spare ressource with recursive trigger on inserting user ;
- fix error when no flags are used during edit and creation of permission group ;
Master branch
=============
- allow to filters users shown by `UserPickerType` based on flags. This flags do an additional filter based on the flags assigned in permissions groups;
- add a method to filters users by permissions groups flags in `UserRepository`

View File

@ -55,7 +55,7 @@ class UserPickerType extends AbstractType
/**
*
* @var EntityRepository
* @var \Chill\MainBundle\Repository\UserRepository
*/
protected $userRepository;
@ -82,6 +82,8 @@ class UserPickerType extends AbstractType
;
$resolver
->setDefault('having_permissions_group_flag', null)
->setAllowedTypes('having_permissions_group_flag', ['string', 'null'])
->setDefault('class', User::class)
->setDefault('placeholder', 'Choose an user')
->setDefault('choice_label', function(User $u) {
@ -89,8 +91,16 @@ class UserPickerType extends AbstractType
})
->setNormalizer('choices', function(Options $options) {
return $this->authorizationHelper
$users = $this->authorizationHelper
->findUsersReaching($options['role'], $options['center']);
if (NULL !== $options['having_permissions_group_flag']) {
return $this->userRepository
->findUsersHavingFlags($options['having_permissions_group_flag'], $users)
;
}
return $users;
})
;
}

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');