From b06a76784a947f15ff7ed6754a93aea2bf30583d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Nov 2022 11:18:33 +0100 Subject: [PATCH] Feature: [person][creation] api for listing availables centers for person creation --- .../config/services/security.yaml | 5 +++ .../Controller/PersonApiController.php | 32 +++++++++++++--- .../AuthorizedCenterOnPersonCreation.php | 38 +++++++++++++++++++ ...horizedCenterOnPersonCreationInterface.php | 22 +++++++++++ .../ChillPersonBundle/chill.api.specs.yaml | 12 +++++- .../config/services/controller.yaml | 5 +-- .../config/services/security.yaml | 6 +++ 7 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreation.php create mode 100644 src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreationInterface.php diff --git a/src/Bundle/ChillMainBundle/config/services/security.yaml b/src/Bundle/ChillMainBundle/config/services/security.yaml index 824144470..c0d9f98b9 100644 --- a/src/Bundle/ChillMainBundle/config/services/security.yaml +++ b/src/Bundle/ChillMainBundle/config/services/security.yaml @@ -3,6 +3,11 @@ services: autowire: true autoconfigure: true + Chill\MainBundle\Security\: + autoconfigure: true + autowire: true + resource: '../../Security' + Chill\MainBundle\Security\Resolver\CenterResolverDispatcher: arguments: - !tagged_iterator chill_main.center_resolver diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php b/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php index 745a64240..bf0d998ff 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php @@ -13,12 +13,13 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\Entity\Address; -use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Security\Authorization\PersonVoter; +use Chill\PersonBundle\Security\AuthorizedCenterOnPersonCreationInterface; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -26,16 +27,37 @@ use function in_array; class PersonApiController extends ApiController { - private AuthorizationHelper $authorizationHelper; + private AuthorizedCenterOnPersonCreationInterface $authorizedCenterOnPersonCreation; private ConfigPersonAltNamesHelper $configPersonAltNameHelper; + private bool $showCenters; + public function __construct( - AuthorizationHelper $authorizationHelper, - ConfigPersonAltNamesHelper $configPersonAltNameHelper + AuthorizedCenterOnPersonCreationInterface $authorizedCenterOnPersonCreation, + ConfigPersonAltNamesHelper $configPersonAltNameHelper, + ParameterBagInterface $parameterBag ) { - $this->authorizationHelper = $authorizationHelper; + $this->authorizedCenterOnPersonCreation = $authorizedCenterOnPersonCreation; $this->configPersonAltNameHelper = $configPersonAltNameHelper; + $this->showCenters = $parameterBag->get('chill_main')['acl']['form_show_centers']; + } + + /** + * @Route("/api/1.0/person/creation/authorized-centers", + * name="chill_person_person_creation_authorized_centers" + * ) + */ + public function authorizedCentersForCreation(): Response + { + $centers = $this->authorizedCenterOnPersonCreation->getCenters(); + + return $this->json( + ['showCenters' => $this->showCenters, 'centers' => $centers], + Response::HTTP_OK, + [], + ['gropus' => ['read']] + ); } /** diff --git a/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreation.php b/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreation.php new file mode 100644 index 000000000..6f8800d65 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreation.php @@ -0,0 +1,38 @@ +authorizationHelperForCurrentUser = $authorizationHelperForCurrentUser; + $this->showCenter = $parameterBag->get('chill_main')['acl']['form_show_centers']; + } + + public function getCenters(): array + { + if (!$this->showCenter) { + return []; + } + + return $this->authorizationHelperForCurrentUser->getReachableCenters(PersonVoter::CREATE); + } +} diff --git a/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreationInterface.php b/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreationInterface.php new file mode 100644 index 000000000..d0748b8c2 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Security/AuthorizedCenterOnPersonCreationInterface.php @@ -0,0 +1,22 @@ +