From 92e6506ecbba4d2ff12acbcf142e720738ecdcce Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Thu, 30 Sep 2021 14:46:21 +0200 Subject: [PATCH] tp: adapt controller/entity/formtype to works with new ACL (TO BE CHECKED) * rename type by types -> getTypes() getter * adapt controller to fix centers errors * remove voteOnAttribute return always true in voter --- .../Controller/ThirdPartyController.php | 53 +++++-------------- .../Entity/ThirdParty.php | 2 +- .../Form/ThirdPartyType.php | 6 +-- .../views/ThirdParty/index.html.twig | 2 +- .../Resources/views/ThirdParty/new.html.twig | 2 +- .../Resources/views/ThirdParty/show.html.twig | 2 +- .../views/ThirdParty/update.html.twig | 2 +- .../Security/Voter/ThirdPartyVoter.php | 2 - 8 files changed, 22 insertions(+), 49 deletions(-) diff --git a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php index d16934b18..23b8d3542 100644 --- a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php +++ b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php @@ -60,22 +60,12 @@ class ThirdPartyController extends Controller $this->denyAccessUnlessGranted(ThirdPartyVoter::SHOW); $repository = $this->getDoctrine()->getManager() ->getRepository(ThirdParty::class); - - $centers = $this->authorizationHelper - ->getReachableCenters( - $this->getUser(), - new Role(ThirdPartyVoter::SHOW) - ); - - $nbThirdParties = $repository->countByMemberOfCenters($centers); + + $nbThirdParties = $repository->count([]); //$repository->countByMemberOfCenters($centers); $pagination = $this->paginatorFactory->create($nbThirdParties); - - $thirdParties = $repository->findByMemberOfCenters( - $centers, - $pagination->getCurrentPage()->getFirstItemNumber(), - $pagination->getItemsPerPage() - ); - + + $thirdParties = $repository->findAll(); + return $this->render('ChillThirdPartyBundle:ThirdParty:index.html.twig', array( 'third_parties' => $thirdParties, 'pagination' => $pagination @@ -88,18 +78,9 @@ class ThirdPartyController extends Controller public function newAction(Request $request) { $this->denyAccessUnlessGranted(ThirdPartyVoter::CREATE); - - $centers = $this->authorizationHelper - ->getReachableCenters( - $this->getUser(), - new Role(ThirdPartyVoter::CREATE) - ); - - if (count($centers) === 0) { - throw new \LogicException("There should be at least one center reachable " - . "if role ".ThirdPartyVoter::CREATE." is granted"); - } - + + $centers = []; + $thirdParty = new ThirdParty(); $thirdParty->setCenters(new ArrayCollection($centers)); @@ -141,18 +122,12 @@ class ThirdPartyController extends Controller public function updateAction(ThirdParty $thirdParty, Request $request) { $this->denyAccessUnlessGranted(ThirdPartyVoter::CREATE); - - $centers = $this->authorizationHelper - ->getReachableCenters( - $this->getUser(), - new Role(ThirdPartyVoter::CREATE) - ); - - if (count($centers) === 0) { - throw new \LogicException("There should be at least one center reachable " - . "if role ".ThirdPartyVoter::CREATE." is granted"); - } - + + $repository = $this->getDoctrine()->getManager() + ->getRepository(ThirdParty::class); + + $centers = $repository->findAll(); + // we want to keep centers the users has no access to. So we will add them // later if they are removed. (this is a ugly hack but it will works $centersAssociatedNotForUsers = \array_diff( diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 30f28791b..21d74f17e 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -341,7 +341,7 @@ class ThirdParty */ public function getTypes() { - return $this->type; + return $this->types; } /** diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php index 2ec113eb9..8eae938f6 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php @@ -64,10 +64,10 @@ class ThirdPartyType extends AbstractType } if (count($types) === 1) { $builder - ->add('type', HiddenType::class, [ + ->add('types', HiddenType::class, [ 'data' => array_values($types) ]) - ->get('type') + ->get('types') ->addModelTransformer(new CallbackTransformer( function (?array $typeArray): ?string { if (null === $typeArray) { @@ -84,7 +84,7 @@ class ThirdPartyType extends AbstractType )) ; } else { - $builder->add('type', ChoiceType::class, [ + $builder->add('types', ChoiceType::class, [ 'choices' => $types, 'expanded' => true, 'multiple' => true, diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/index.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/index.html.twig index fa9ad8b8b..1cd1a6257 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/index.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/index.html.twig @@ -51,7 +51,7 @@ {{ (tp.active ? '' : '')|raw }} {{ tp.name }} {% set types = [] %} - {% for t in tp.type %} + {% for t in tp.types %} {% set types = types|merge( [ ('chill_3party.key_label.'~t)|trans ] ) %} {% endfor %} {{ types|join(', ') }} diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new.html.twig index 002cc87e8..319083649 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new.html.twig @@ -26,7 +26,7 @@ {{ form_row(form.profession) }} {% endif %} - {{ form_row(form.type) }} + {{ form_row(form.types) }} {{ form_row(form.categories) }} {{ form_row(form.telephone) }} diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/show.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/show.html.twig index d711bdefa..a8dc587d7 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/show.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/show.html.twig @@ -48,7 +48,7 @@
{{ 'Type'|trans }}
{% set types = [] %} - {% for t in thirdParty.type %} + {% for t in thirdParty.types %} {% set types = types|merge( [ ('chill_3party.key_label.'~t)|trans ] ) %} {% endfor %}
diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/update.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/update.html.twig index 01837db05..4433d934b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/update.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/update.html.twig @@ -43,7 +43,7 @@ {{ form_row(form.profession) }} {% endif %} - {{ form_row(form.type) }} + {{ form_row(form.types) }} {{ form_row(form.categories) }} {{ form_row(form.telephone) }} diff --git a/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php b/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php index ab78c7701..986bfb2b9 100644 --- a/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php +++ b/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php @@ -56,8 +56,6 @@ class ThirdPartyVoter extends AbstractChillVoter implements ProvideRoleHierarchy */ protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { - return true; - $user = $token->getUser(); if (!$user instanceof User) {