From 1f9b4ddd79652993bbee9993325048f96fb41d4d Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 6 May 2021 21:34:02 +0200 Subject: [PATCH] Remove custom definition (form.yaml). --- .../Authorization/AuthorizationHelper.php | 104 +++++++++--------- .../AuthorizationHelperInterface.php | 87 +++++++++++++++ .../Entity/ChillEntityRenderExtension.php | 20 ++-- .../ChillEntityRenderExtensionInterface.php | 33 ++++++ .../config/services/security.yaml | 23 ++-- .../config/services/templating.yaml | 3 +- .../config/services/form.yaml | 27 ----- .../src/Form/Type/ClosingMotivePickerType.php | 28 +++-- .../src/Form/Type/PersonAltNameType.php | 28 ++--- .../src/Form/Type/PickPersonType.php | 18 +-- 10 files changed, 232 insertions(+), 139 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperInterface.php create mode 100644 src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtensionInterface.php diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelper.php b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelper.php index ed2dd499d..a64f67258 100644 --- a/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelper.php +++ b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelper.php @@ -32,34 +32,34 @@ use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\RoleScope; /** - * Helper for authorizations. - * + * Helper for authorizations. + * * Provides methods for user and entities information. * * @author Julien Fastré */ -class AuthorizationHelper +class AuthorizationHelper implements AuthorizationHelperInterface { /** * * @var RoleHierarchyInterface */ protected $roleHierarchy; - + /** - * The role in a hierarchy, given by the parameter + * The role in a hierarchy, given by the parameter * `security.role_hierarchy.roles` from the container. * * @var string[] */ protected $hierarchy; - + /** * * @var EntityManagerInterface */ protected $em; - + public function __construct( RoleHierarchyInterface $roleHierarchy, $hierarchy, @@ -69,10 +69,10 @@ class AuthorizationHelper $this->hierarchy = $hierarchy; $this->em = $em; } - + /** * Determines if a user is active on this center - * + * * @param User $user * @param Center $center * @return bool @@ -81,21 +81,21 @@ class AuthorizationHelper { foreach ($user->getGroupCenters() as $groupCenter) { if ($center->getId() === $groupCenter->getCenter()->getId()) { - + return true; } } - + return false; } - + /** - * + * * Determines if the user has access to the given entity. - * + * * if the entity implements Chill\MainBundle\Entity\HasScopeInterface, * the scope is taken into account. - * + * * @param User $user * @param HasCenterInterface $entity the entity may also implement HasScopeInterface * @param string|Role $attribute @@ -103,15 +103,15 @@ class AuthorizationHelper */ public function userHasAccess(User $user, HasCenterInterface $entity, $attribute) { - + $center = $entity->getCenter(); - + if (!$this->userCanReachCenter($user, $center)) { return false; } - + $role = ($attribute instanceof Role) ? $attribute : new Role($attribute); - + foreach ($user->getGroupCenters() as $groupCenter){ //filter on center if ($groupCenter->getCenter()->getId() === $entity->getCenter()->getId()) { @@ -119,7 +119,7 @@ class AuthorizationHelper //iterate on roleScopes foreach($permissionGroup->getRoleScopes() as $roleScope) { //check that the role allow to reach the required role - if ($this->isRoleReached($role, + if ($this->isRoleReached($role, new Role($roleScope->getRole()))){ //if yes, we have a right on something... // perform check on scope if necessary @@ -137,17 +137,17 @@ class AuthorizationHelper } } } - + } } - + return false; } - + /** * Get reachable Centers for the given user, role, * and optionnaly Scope - * + * * @param User $user * @param Role $role * @param null|Scope $scope @@ -156,13 +156,13 @@ class AuthorizationHelper public function getReachableCenters(User $user, Role $role, Scope $scope = null) { $centers = array(); - + foreach ($user->getGroupCenters() as $groupCenter){ $permissionGroup = $groupCenter->getPermissionsGroup(); //iterate on roleScopes foreach($permissionGroup->getRoleScopes() as $roleScope) { //check that the role is in the reachable roles - if ($this->isRoleReached($role, + if ($this->isRoleReached($role, new Role($roleScope->getRole()))) { if ($scope === null) { $centers[] = $groupCenter->getCenter(); @@ -171,19 +171,19 @@ class AuthorizationHelper if ($scope->getId() == $roleScope->getScope()->getId()){ $centers[] = $groupCenter->getCenter(); break 1; - } + } } } } - + } - + return $centers; } - + /** * Return all reachable scope for a given user, center and role - * + * * @deprecated Use getReachableCircles * * @param User $user @@ -195,10 +195,10 @@ class AuthorizationHelper { return $this->getReachableCircles($user, $role, $center); } - + /** * Return all reachable circle for a given user, center and role - * + * * @param User $user * @param Role $role * @param Center $center @@ -207,7 +207,7 @@ class AuthorizationHelper public function getReachableCircles(User $user, Role $role, Center $center) { $scopes = array(); - + foreach ($user->getGroupCenters() as $groupCenter){ if ($center->getId() === $groupCenter->getCenter()->getId()) { //iterate on permissionGroup @@ -215,7 +215,7 @@ class AuthorizationHelper //iterate on roleScopes foreach($permissionGroup->getRoleScopes() as $roleScope) { //check that the role is in the reachable roles - if ($this->isRoleReached($role, + if ($this->isRoleReached($role, new Role($roleScope->getRole()))) { $scopes[] = $roleScope->getScope(); @@ -223,12 +223,12 @@ class AuthorizationHelper } } } - + return $scopes; } - + /** - * + * * @param Role $role * @param Center $center * @param Scope $circle @@ -239,7 +239,7 @@ class AuthorizationHelper $parents = $this->getParentRoles($role); $parents[] = $role; $parentRolesString = \array_map(function(Role $r) { return $r->getRole(); }, $parents); - + $qb = $this->em->createQueryBuilder(); $qb ->select('u') @@ -250,21 +250,21 @@ class AuthorizationHelper ->where('gc.center = :center') ->andWhere($qb->expr()->in('rs.role', $parentRolesString)) ; - + $qb->setParameter('center', $center); - + if ($circle !== null) { $qb->andWhere('rs.scope = :circle') ->setParameter('circle', $circle) ; } - + return $qb->getQuery()->getResult(); } - + /** * Test if a parent role may give access to a given child role - * + * * @param Role $childRole The role we want to test if he is reachable * @param Role $parentRole The role which should give access to $childRole * @return boolean true if the child role is granted by parent role @@ -273,14 +273,14 @@ class AuthorizationHelper { $reachableRoles = $this->roleHierarchy ->getReachableRoles([$parentRole]); - + return in_array($childRole, $reachableRoles); } - + /** - * Return all the role which give access to the given role. Only the role + * Return all the role which give access to the given role. Only the role * which are registered into Chill are taken into account. - * + * * @param Role $role * @return Role[] the role which give access to the given $role */ @@ -291,18 +291,18 @@ class AuthorizationHelper $roles = \array_map( function($string) { return new Role($string); - }, + }, \array_keys($this->hierarchy) ); - + foreach ($roles as $r) { $childRoles = $this->roleHierarchy->getReachableRoleNames([$r->getRole()]); - + if (\in_array($role, $childRoles)) { $parentRoles[] = $r; } } - + return $parentRoles; } } diff --git a/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperInterface.php b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperInterface.php new file mode 100644 index 000000000..ca1ff1728 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Security/Authorization/AuthorizationHelperInterface.php @@ -0,0 +1,87 @@ + * * This program is free software: you can redistribute it and/or modify @@ -28,18 +28,18 @@ use Twig\TwigFilter; * * @package Chill\MainBundle\Templating\Entity */ -class ChillEntityRenderExtension extends AbstractExtension +class ChillEntityRenderExtension extends AbstractExtension implements ChillEntityRenderExtensionInterface { /** - * @var ChillEntityRenderInterface + * @var ChillEntityRenderInterface */ protected $renders = []; - + /** * @var ChillEntityRender */ protected $defaultRender; - + /** * ChillEntityRenderExtension constructor. */ @@ -47,7 +47,7 @@ class ChillEntityRenderExtension extends AbstractExtension { $this->defaultRender = new ChillEntityRender(); } - + /** * @return array|TwigFilter[] */ @@ -62,7 +62,7 @@ class ChillEntityRenderExtension extends AbstractExtension ]) ]; } - + /** * @param $entity * @param array $options @@ -76,7 +76,7 @@ class ChillEntityRenderExtension extends AbstractExtension return $this->getRender($entity, $options) ->renderString($entity, $options); } - + /** * @param $entity * @param array $options @@ -90,7 +90,7 @@ class ChillEntityRenderExtension extends AbstractExtension return $this->getRender($entity, $options) ->renderBox($entity, $options); } - + /** * @param ChillEntityRenderInterface $render */ @@ -98,7 +98,7 @@ class ChillEntityRenderExtension extends AbstractExtension { $this->renders[] = $render; } - + /** * @param $entity * @param $options diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtensionInterface.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtensionInterface.php new file mode 100644 index 000000000..35a49bca6 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtensionInterface.php @@ -0,0 +1,33 @@ +translatableStringHelper = $translatableStringHelper; $this->entityRenderExtension = $chillEntityRenderExtension; $this->repository = $closingMotiveRepository; } - + /** * @return string */ @@ -61,7 +59,7 @@ class ClosingMotivePickerType extends AbstractType { return 'closing_motive'; } - + /** * @return null|string */ @@ -69,13 +67,13 @@ class ClosingMotivePickerType extends AbstractType { return EntityType::class; } - + /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { - + $resolver->setDefaults([ 'class' => ClosingMotive::class, 'empty_data' => null, @@ -85,7 +83,7 @@ class ClosingMotivePickerType extends AbstractType }, 'only_leaf' => true ]); - + $resolver ->setAllowedTypes('only_leaf', 'bool') ->setNormalizer('choices', function (Options $options) { diff --git a/src/Bundle/ChillPersonBundle/src/Form/Type/PersonAltNameType.php b/src/Bundle/ChillPersonBundle/src/Form/Type/PersonAltNameType.php index 1ba58e86e..448f7eacb 100644 --- a/src/Bundle/ChillPersonBundle/src/Form/Type/PersonAltNameType.php +++ b/src/Bundle/ChillPersonBundle/src/Form/Type/PersonAltNameType.php @@ -7,30 +7,30 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; -use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; -use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Chill\PersonBundle\Config\ConfigPersonAltNamesHelperInterface; /** - * + * * */ class PersonAltNameType extends AbstractType { /** * - * @var ConfigPersonAltNamesHelper + * @var TranslatableStringHelperInterface */ private $configHelper; - + /** * - * @var TranslatableStringHelper + * @var TranslatableStringHelperInterface */ private $translatableStringHelper; - + public function __construct( - ConfigPersonAltNamesHelper $configHelper, - TranslatableStringHelper $translatableStringHelper + ConfigPersonAltNamesHelperInterface $configHelper, + TranslatableStringHelperInterface $translatableStringHelper ) { $this->configHelper = $configHelper; $this->translatableStringHelper = $translatableStringHelper; @@ -40,26 +40,26 @@ class PersonAltNameType extends AbstractType { foreach ($this->getKeyChoices() as $label => $key) { $builder->add( - $key, + $key, $options['force_hidden'] ? HiddenType::class : TextType::class, [ 'label' => $label, 'required' => false ]); } - + $builder->setDataMapper(new \Chill\PersonBundle\Form\DataMapper\PersonAltNameDataMapper()); } - + protected function getKeyChoices() { $choices = $this->configHelper->getChoices(); $translatedChoices = []; - + foreach ($choices as $key => $labels) { $label = $this->translatableStringHelper->localize($labels); $translatedChoices[$label] = $key; } - + return $translatedChoices; } diff --git a/src/Bundle/ChillPersonBundle/src/Form/Type/PickPersonType.php b/src/Bundle/ChillPersonBundle/src/Form/Type/PickPersonType.php index 839c84b69..746d8e8c9 100644 --- a/src/Bundle/ChillPersonBundle/src/Form/Type/PickPersonType.php +++ b/src/Bundle/ChillPersonBundle/src/Form/Type/PickPersonType.php @@ -29,8 +29,8 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Chill\MainBundle\Entity\GroupCenter; use Chill\PersonBundle\Entity\Person; -use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Search\PersonSearch; use Symfony\Component\Translation\TranslatorInterface; @@ -59,7 +59,7 @@ class PickPersonType extends AbstractType * @var PersonRepository */ protected $personRepository; - + /** * * @var \Chill\MainBundle\Entity\User @@ -68,16 +68,16 @@ class PickPersonType extends AbstractType /** * - * @var AuthorizationHelper + * @var AuthorizationHelperInterface */ protected $authorizationHelper; - + /** * * @var UrlGeneratorInterface */ protected $urlGenerator; - + /** * * @var TranslatorInterface @@ -87,7 +87,7 @@ class PickPersonType extends AbstractType public function __construct( PersonRepository $personRepository, TokenStorageInterface $tokenStorage, - AuthorizationHelper $authorizationHelper, + AuthorizationHelperInterface $authorizationHelper, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator ) @@ -133,7 +133,7 @@ class PickPersonType extends AbstractType $selectedCenters[] = $c; } } - + return $selectedCenters; } @@ -165,7 +165,7 @@ class PickPersonType extends AbstractType 'attr' => array('class' => 'select2 '), 'choice_loader' => function(Options $options) { $centers = $this->filterCentersfom($options); - + return new PersonChoiceLoader($this->personRepository, $centers); } )); @@ -175,7 +175,7 @@ class PickPersonType extends AbstractType { return EntityType::class; } - + public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options) { $view->vars['attr']['data-person-picker'] = true;