[FIX] use AuthorizationHelperInterface instead of implementation in PickCenterType

This commit is contained in:
Julien Fastré 2022-09-09 18:36:13 +02:00
parent 38cb1fe357
commit 524123c701
2 changed files with 13 additions and 31 deletions

View File

@ -15,6 +15,7 @@ use Chill\MainBundle\Center\GroupingCenterInterface;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
@ -24,6 +25,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use function array_intersect;
use function array_key_exists;
use function array_merge;
@ -38,30 +40,24 @@ class PickCenterType extends AbstractType
{
public const CENTERS_IDENTIFIERS = 'c';
/**
* @var AuthorizationHelper
*/
protected $authorizationHelper;
protected AuthorizationHelperInterface $authorizationHelper;
protected ExportManager $exportManager;
/**
* @var ExportManager
* @var array|GroupingCenterInterface[]
*/
protected $exportManager;
/**
* @var GroupingCenterInterface[]
*/
protected $groupingCenters = [];
protected array $groupingCenters = [];
/**
* @var \Symfony\Component\Security\Core\User\UserInterface
*/
protected $user;
protected UserInterface $user;
public function __construct(
TokenStorageInterface $tokenStorage,
ExportManager $exportManager,
AuthorizationHelper $authorizationHelper
AuthorizationHelperInterface $authorizationHelper
) {
$this->exportManager = $exportManager;
$this->user = $tokenStorage->getToken()->getUser();
@ -78,22 +74,12 @@ class PickCenterType extends AbstractType
$export = $this->exportManager->getExport($options['export_alias']);
$centers = $this->authorizationHelper->getReachableCenters(
$this->user,
(string) $export->requiredRole()
$export->requiredRole()
);
$builder->add(self::CENTERS_IDENTIFIERS, EntityType::class, [
'class' => Center::class,
'query_builder' => static function (EntityRepository $er) use ($centers) {
$qb = $er->createQueryBuilder('c');
$ids = array_map(
static function (Center $el) {
return $el->getId();
},
$centers
);
return $qb->where($qb->expr()->in('c.id', $ids));
},
'choices' => $centers,
'multiple' => true,
'expanded' => true,
'choice_label' => static function (Center $c) {

View File

@ -81,12 +81,8 @@ services:
chill.main.form.pick_centers_type:
class: Chill\MainBundle\Form\Type\Export\PickCenterType
arguments:
- "@security.token_storage"
- '@Chill\MainBundle\Export\ExportManager'
- "@chill.main.security.authorization.helper"
tags:
- { name: form.type }
autowire: true
autoconfigure: true
chill.main.form.formatter_type:
class: Chill\MainBundle\Form\Type\Export\FormatterType