From 930a76cc666087d195dda9f9216d4b104fb92af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 27 Nov 2023 13:24:19 +0100 Subject: [PATCH] use a PickPersonDynamic type in event bundle --- .../Controller/EventController.php | 66 +++++-------------- .../Controller/ParticipationController.php | 52 +++++++-------- .../ChillEventExtension.php | 4 +- .../Resources/views/Event/show.html.twig | 10 +++ .../config/services/controller.yaml | 16 ----- 5 files changed, 54 insertions(+), 94 deletions(-) delete mode 100644 src/Bundle/ChillEventBundle/config/services/controller.yaml diff --git a/src/Bundle/ChillEventBundle/Controller/EventController.php b/src/Bundle/ChillEventBundle/Controller/EventController.php index e383c5432..03069eaad 100644 --- a/src/Bundle/ChillEventBundle/Controller/EventController.php +++ b/src/Bundle/ChillEventBundle/Controller/EventController.php @@ -18,9 +18,9 @@ use Chill\EventBundle\Form\Type\PickEventType; use Chill\EventBundle\Security\Authorization\EventVoter; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Pagination\PaginatorFactory; -use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\PersonBundle\Entity\Person; -use Chill\PersonBundle\Form\Type\PickPersonType; +use Chill\PersonBundle\Form\Type\PickPersonDynamicType; use Chill\PersonBundle\Privacy\PrivacyEvent; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Csv; @@ -42,49 +42,18 @@ use Symfony\Contracts\Translation\TranslatorInterface; /** * Class EventController. */ -class EventController extends AbstractController +final class EventController extends AbstractController { - /** - * @var AuthorizationHelper - */ - protected $authorizationHelper; - - /** - * @var EventDispatcherInterface - */ - protected $eventDispatcher; - - /** - * @var FormFactoryInterface - */ - protected $formFactoryInterface; - - /** - * @var PaginatorFactory - */ - protected $paginator; - - /** - * @var TranslatorInterface - */ - protected $translator; - /** * EventController constructor. */ public function __construct( - EventDispatcherInterface $eventDispatcher, - AuthorizationHelper $authorizationHelper, - FormFactoryInterface $formFactoryInterface, - TranslatorInterface $translator, - PaginatorFactory $paginator - ) { - $this->eventDispatcher = $eventDispatcher; - $this->authorizationHelper = $authorizationHelper; - $this->formFactoryInterface = $formFactoryInterface; - $this->translator = $translator; - $this->paginator = $paginator; - } + private readonly EventDispatcherInterface $eventDispatcher, + private readonly AuthorizationHelperInterface $authorizationHelper, + private readonly FormFactoryInterface $formFactoryInterface, + private readonly TranslatorInterface $translator, + private readonly PaginatorFactory $paginator + ) {} /** * @\Symfony\Component\Routing\Annotation\Route(path="/{_locale}/event/event/{event_id}/delete", name="chill_event__event_delete", requirements={"event_id"="\d+"}, methods={"GET", "DELETE"}) @@ -181,7 +150,7 @@ class EventController extends AbstractController $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); - $reachablesCircles = $this->authorizationHelper->getReachableCircles( + $reachablesCircles = $this->authorizationHelper->getReachableScopes( $this->getUser(), EventVoter::SEE, $person->getCenter() @@ -323,7 +292,7 @@ class EventController extends AbstractController } $this->denyAccessUnlessGranted( - 'CHILL_EVENT_SEE_DETAILS', + EventVoter::SEE_DETAILS, $event, 'You are not allowed to see details on this event' ); @@ -430,11 +399,9 @@ class EventController extends AbstractController */ protected function createAddParticipationByPersonForm(Event $event) { - /** @var \Symfony\Component\Form\FormBuilderInterface $builder */ - $builder = $this - ->get('form.factory') + $builder = $this->formFactoryInterface ->createNamedBuilder( - null, + '', FormType::class, null, [ @@ -444,10 +411,7 @@ class EventController extends AbstractController ] ); - $builder->add('person_id', PickPersonType::class, [ - 'role' => 'CHILL_EVENT_CREATE', - 'centers' => $event->getCenter(), - ]); + $builder->add('person_id', PickPersonDynamicType::class, ['as_id' => true, 'multiple' => false]); $builder->add('event_id', HiddenType::class, [ 'data' => $event->getId(), @@ -469,7 +433,7 @@ class EventController extends AbstractController */ protected function createExportByFormatForm() { - $builder = $this->createFormBuilder() + $builder = $this->createFormBuilder(['format' => 'xlsx']) ->add('format', ChoiceType::class, [ 'choices' => [ 'xlsx' => 'xlsx', diff --git a/src/Bundle/ChillEventBundle/Controller/ParticipationController.php b/src/Bundle/ChillEventBundle/Controller/ParticipationController.php index 9785c20b0..ff6ad92df 100644 --- a/src/Bundle/ChillEventBundle/Controller/ParticipationController.php +++ b/src/Bundle/ChillEventBundle/Controller/ParticipationController.php @@ -14,7 +14,10 @@ namespace Chill\EventBundle\Controller; use Chill\EventBundle\Entity\Event; use Chill\EventBundle\Entity\Participation; use Chill\EventBundle\Form\ParticipationType; +use Chill\EventBundle\Repository\EventRepository; use Chill\EventBundle\Security\Authorization\ParticipationVoter; +use Chill\PersonBundle\Repository\PersonRepository; +use Chill\PersonBundle\Security\Authorization\PersonVoter; use Doctrine\Common\Collections\Collection; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -28,14 +31,17 @@ use Symfony\Contracts\Translation\TranslatorInterface; /** * Class ParticipationController. */ -class ParticipationController extends AbstractController +final class ParticipationController extends AbstractController { /** * ParticipationController constructor. */ - public function __construct(private readonly LoggerInterface $logger, private readonly TranslatorInterface $translator) - { - } + public function __construct( + private readonly LoggerInterface $logger, + private readonly TranslatorInterface $translator, + private readonly EventRepository $eventRepository, + private readonly PersonRepository $personRepository, + ) {} /** * @\Symfony\Component\Routing\Annotation\Route(path="/{_locale}/event/participation/create", name="chill_event_participation_create") @@ -321,7 +327,7 @@ class ParticipationController extends AbstractController */ public function editMultipleAction($event_id): Response|\Symfony\Component\HttpFoundation\RedirectResponse { - $event = $this->getDoctrine()->getRepository(Event::class) + $event = $this->getDoctrine()->getRepository(\Chill\EventBundle\Entity\Event::class) ->find($event_id); if (null === $event) { @@ -539,7 +545,7 @@ class ParticipationController extends AbstractController * If the request is multiple, the $participation object is cloned. * Limitations: the $participation should not be persisted. * - * @return Participation|Participation[] return one single participation if $multiple == false + * @return Participation|list return one single participation if $multiple == false * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the event/person is not found * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException if the user does not have access to event/person @@ -556,30 +562,25 @@ class ParticipationController extends AbstractController } $event_id = $request->query->getInt('event_id', 0); // sf4 check: - // prevent error: `Argument 2 passed to ::getInt() must be of the type int, null given` - if (null !== $event_id) { - $event = $em->getRepository(Event::class) - ->find($event_id); + $event = $this->eventRepository->find($event_id); - if (null === $event) { - throw $this->createNotFoundException('The event with id '.$event_id.' is not found'); - } - - $this->denyAccessUnlessGranted( - 'CHILL_EVENT_SEE', - $event, - 'The user is not allowed to see the event' - ); - - $participation->setEvent($event); + if (null === $event) { + throw $this->createNotFoundException('The event with id '.$event_id.' is not found'); } + $this->denyAccessUnlessGranted( + 'CHILL_EVENT_SEE', + $event, + 'The user is not allowed to see the event' + ); + + $participation->setEvent($event); + // this script should be able to handle multiple, so we translate // single person_id in an array $persons_ids = $request->query->has('person_id') ? - [$request->query->getInt('person_id', 0)] // sf4 check: - // prevent error: `Argument 2 passed to ::getInt() must be of the type int, null given` + [$request->query->get('person_id', 0)] : explode(',', (string) $request->query->get('persons_ids')); $participations = []; @@ -588,15 +589,14 @@ class ParticipationController extends AbstractController $participation = \count($persons_ids) > 1 ? clone $participation : $participation; if (null !== $person_id) { - $person = $em->getRepository(\Chill\PersonBundle\Entity\Person::class) - ->find($person_id); + $person = $this->personRepository->find($person_id); if (null === $person) { throw $this->createNotFoundException('The person with id '.$person_id.' is not found'); } $this->denyAccessUnlessGranted( - 'CHILL_PERSON_SEE', + PersonVoter::SEE, $person, 'The user is not allowed to see the person' ); diff --git a/src/Bundle/ChillEventBundle/DependencyInjection/ChillEventExtension.php b/src/Bundle/ChillEventBundle/DependencyInjection/ChillEventExtension.php index 07c431f2f..8d35454ec 100644 --- a/src/Bundle/ChillEventBundle/DependencyInjection/ChillEventExtension.php +++ b/src/Bundle/ChillEventBundle/DependencyInjection/ChillEventExtension.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\EventBundle\DependencyInjection; use Chill\EventBundle\Security\Authorization\EventVoter; +use Chill\EventBundle\Security\Authorization\ParticipationVoter; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; @@ -33,7 +34,6 @@ class ChillEventExtension extends Extension implements PrependExtensionInterface $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config')); $loader->load('services.yaml'); $loader->load('services/authorization.yaml'); - $loader->load('services/controller.yaml'); $loader->load('services/fixtures.yaml'); $loader->load('services/forms.yaml'); $loader->load('services/menu.yaml'); @@ -61,6 +61,8 @@ class ChillEventExtension extends Extension implements PrependExtensionInterface EventVoter::SEE_DETAILS => [EventVoter::SEE], EventVoter::UPDATE => [EventVoter::SEE_DETAILS], EventVoter::CREATE => [EventVoter::SEE_DETAILS], + ParticipationVoter::SEE_DETAILS => [ParticipationVoter::SEE], + ParticipationVoter::UPDATE => [ParticipationVoter::SEE_DETAILS], ], ]); } diff --git a/src/Bundle/ChillEventBundle/Resources/views/Event/show.html.twig b/src/Bundle/ChillEventBundle/Resources/views/Event/show.html.twig index f6b84cf68..b7e3b7d10 100644 --- a/src/Bundle/ChillEventBundle/Resources/views/Event/show.html.twig +++ b/src/Bundle/ChillEventBundle/Resources/views/Event/show.html.twig @@ -4,6 +4,16 @@ {% import '@ChillPerson/Person/macro.html.twig' as person_macro %} +{% block js %} + {{ parent() }} + {{ encore_entry_script_tags('mod_pickentity_type') }} +{% endblock %} + +{% block css %} + {{ parent() }} + {{ encore_entry_link_tags('mod_pickentity_type') }} +{% endblock %} + {% block event_content -%}

{{ 'Details of an event'|trans }}

diff --git a/src/Bundle/ChillEventBundle/config/services/controller.yaml b/src/Bundle/ChillEventBundle/config/services/controller.yaml deleted file mode 100644 index 0ba2fac8e..000000000 --- a/src/Bundle/ChillEventBundle/config/services/controller.yaml +++ /dev/null @@ -1,16 +0,0 @@ -services: - - Chill\EventBundle\Controller\EventController: - arguments: - $eventDispatcher: '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface' - $authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper' - $formFactoryInterface: '@Symfony\Component\Form\FormFactoryInterface' - $translator: '@Symfony\Contracts\Translation\TranslatorInterface' - $paginator: '@chill_main.paginator_factory' - public: true - tags: ['controller.service_arguments'] - - Chill\EventBundle\Controller\ParticipationController: - arguments: - $logger: '@Psr\Log\LoggerInterface' - tags: ['controller.service_arguments']