From 1585a739747c8575103169c66ccf3ec3a2148238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Nov 2022 11:12:44 +0100 Subject: [PATCH 1/5] DX: [authorization helper] Create an authorization helper with current user already available See https://gitlab.com/Chill-Projet/chill-bundles/-/issues/14 --- .../AuthorizationHelperForCurrentUser.php | 46 +++++++++++++++++++ ...orizationHelperForCurrentUserInterface.php | 28 +++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUser.php create mode 100644 src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUserInterface.php diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUser.php b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUser.php new file mode 100644 index 000000000..da9c285bd --- /dev/null +++ b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUser.php @@ -0,0 +1,46 @@ +authorizationHelper = $authorizationHelper; + $this->security = $security; + } + + public function getReachableCenters(string $role, ?Scope $scope = null): array + { + if (!$this->security->getUser() instanceof User) { + return []; + } + + return $this->authorizationHelper->getReachableCenters($this->security->getUser(), $role, $scope); + } + + public function getReachableScopes(string $role, $center): array + { + if (!$this->security->getUser() instanceof User) { + return []; + } + + return $this->authorizationHelper->getReachableScopes($this->security->getUser(), $role, $center); + } +} diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUserInterface.php b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUserInterface.php new file mode 100644 index 000000000..cd3459aa9 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperForCurrentUserInterface.php @@ -0,0 +1,28 @@ + Date: Tue, 22 Nov 2022 11:18:33 +0100 Subject: [PATCH 2/5] 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 @@ + Date: Tue, 22 Nov 2022 22:28:18 +0100 Subject: [PATCH 3/5] Feature: [person][creation] Add center field to OnTheFly/Person --- .../ChillPersonBundle/Entity/Person.php | 8 ++- .../vuejs/HouseholdMembersEditor/index.js | 8 +++ .../Resources/public/vuejs/_api/OnTheFly.js | 5 ++ .../vuejs/_components/OnTheFly/Person.vue | 54 ++++++++++++++++--- .../Resources/public/vuejs/_js/i18n.js | 4 ++ 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index a477f9adb..3843a960f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -1567,7 +1567,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI * * @return $this */ - public function setCenter(Center $center): self + public function setCenter(?Center $center): self { $modification = new DateTimeImmutable('now'); @@ -1577,7 +1577,11 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI } } - $this->centerHistory[] = $new = new PersonCenterHistory($this, $center, $modification); + if (null === $center) { + return $this; + } + + $this->centerHistory[] = new PersonCenterHistory($this, $center, $modification); return $this; } diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/index.js b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/index.js index c9f5bb111..d2b271e17 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/index.js +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/index.js @@ -2,6 +2,8 @@ import { createApp } from 'vue'; import { _createI18n } from 'ChillMainAssets/vuejs/_js/i18n'; import { appMessages } from './js/i18n'; import { store } from './store'; +import VueToast from 'vue-toast-notification'; +import 'vue-toast-notification/dist/theme-sugar.css'; import App from './App.vue'; @@ -12,5 +14,11 @@ const app = createApp({ }) .use(store) .use(i18n) +.use(VueToast, { + position: "bottom-right", + type: "error", + duration: 5000, + dismissible: true +}) .component('app', App) .mount('#household_members_editor'); diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/OnTheFly.js b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/OnTheFly.js index 338094122..c9c77991f 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/OnTheFly.js +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_api/OnTheFly.js @@ -1,3 +1,5 @@ +import {makeFetch} from 'ChillMainAssets/lib/api/apiMethods'; + /* * GET a person by id */ @@ -22,6 +24,8 @@ const getCivilities = () => throw Error('Error with request resource response'); }); +const getCentersForPersonCreation = () => makeFetch('GET', '/api/1.0/person/creation/authorized-centers', null); + /* * POST a new person */ @@ -59,6 +63,7 @@ const patchPerson = (id, body) => { }; export { + getCentersForPersonCreation, getPerson, getPersonAltNames, getCivilities, diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue index 4f8909233..8694da651 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/OnTheFly/Person.vue @@ -87,6 +87,18 @@ +
+ + +
+