improve userPicker: delegate query to authorization helper

This commit is contained in:
2018-06-05 09:45:22 +02:00
parent 514b619f8a
commit 13dbaa0b78
5 changed files with 67 additions and 35 deletions

View File

@@ -87,34 +87,10 @@ class UserPickerType extends AbstractType
->setDefault('choice_label', function(User $u) {
return $u->getUsername();
})
->setNormalizer('query_builder', function(Options $options) {
$roles = $this->authorizationHelper->getParentRoles($options['role']);
$roles[] = $options['role'];
->setNormalizer('choices', function(Options $options) {
$qb = $this->userRepository->createQueryBuilder('u');
$qb
// add center constraint
->join('u.groupCenters', 'ug')
->where($qb->expr()->eq('ug.center', ':center'))
->setParameter('center', $options['center'])
// link to permission groups
->join('ug.permissionsGroup', 'pg')
// role constraints
->join('pg.roleScopes', 'roleScope')
->andWhere($qb->expr()->in('roleScope.role', ':roles'))
->setParameter(
'roles',
\array_map(
function(Role $role) { return $role->getRole(); },
$roles
)
)
// add active constraint
->andWhere('u.enabled = :enabled')
->setParameter('enabled', true)
;
return $qb;
return $this->authorizationHelper
->findUsersReaching($options['role'], $options['center']);
})
;
}