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

View File

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