Remove custom definition (form.yaml).

This commit is contained in:
Pol Dellaiera 2021-05-06 21:34:02 +02:00
parent a9bdb1fe3b
commit 1f9b4ddd79
10 changed files with 232 additions and 139 deletions

View File

@ -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é <julien.fastre@champs-libres.coop>
*/
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;
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\HasCenterInterface;
use Symfony\Component\Security\Core\Role\Role;
use Chill\MainBundle\Entity\Scope;
interface AuthorizationHelperInterface
{
/**
* Determines if a user is active on this center
*
* @param User $user
* @param Center $center
* @return bool
*/
public function userCanReachCenter(User $user, Center $center);
/**
*
* 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
* @return boolean true if the user has access
*/
public function userHasAccess(User $user, HasCenterInterface $entity, $attribute);
/**
* Get reachable Centers for the given user, role,
* and optionnaly Scope
*
* @param User $user
* @param Role $role
* @param null|Scope $scope
* @return Center[]
*/
public function getReachableCenters(User $user, Role $role, Scope $scope = null);
/**
* Return all reachable scope for a given user, center and role
*
* @deprecated Use getReachableCircles
*
* @param User $user
* @param Role $role
* @param Center $center
* @return Scope[]
*/
public function getReachableScopes(User $user, Role $role, Center $center);
/**
* Return all reachable circle for a given user, center and role
*
* @param User $user
* @param Role $role
* @param Center $center
* @return Scope[]
*/
public function getReachableCircles(User $user, Role $role, Center $center);
/**
*
* @param Role $role
* @param Center $center
* @param Scope $circle
* @return Users
*/
public function findUsersReaching(Role $role, Center $center, Scope $circle = null);
/**
* 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
*/
public function getParentRoles(Role $role);
}

View File

@ -2,7 +2,7 @@
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>
*
* 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

View File

@ -0,0 +1,33 @@
<?php
namespace Chill\MainBundle\Templating\Entity;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFilter;
interface ChillEntityRenderExtensionInterface extends ExtensionInterface
{
/**
* @return array|TwigFilter[]
*/
public function getFilters();
/**
* @param $entity
* @param array $options
* @return string
*/
public function renderString($entity, array $options = []): string;
/**
* @param $entity
* @param array $options
* @return string
*/
public function renderBox($entity, array $options = []): string;
/**
* @param ChillEntityRenderInterface $render
*/
public function addRender(ChillEntityRenderInterface $render);
}

View File

@ -1,4 +1,4 @@
services:
services:
chill.main.security.authorization.helper:
class: Chill\MainBundle\Security\Authorization\AuthorizationHelper
arguments:
@ -6,47 +6,48 @@ services:
$hierarchy: "%security.role_hierarchy.roles%"
$em: '@Doctrine\ORM\EntityManagerInterface'
Chill\MainBundle\Security\Authorization\AuthorizationHelper: '@chill.main.security.authorization.helper'
Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface: Chill\MainBundle\Security\Authorization\AuthorizationHelper
chill.main.role_provider:
class: Chill\MainBundle\Security\RoleProvider
chill.main.user_provider:
class: Chill\MainBundle\Security\UserProvider\UserProvider
arguments:
$em: '@Doctrine\ORM\EntityManagerInterface'
Chill\MainBundle\Security\Authorization\ChillExportVoter:
arguments:
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
tags:
- { name: security.voter }
Chill\MainBundle\Security\PasswordRecover\TokenManager:
Chill\MainBundle\Security\PasswordRecover\TokenManager:
arguments:
$secret: '%kernel.secret%'
$logger: '@Psr\Log\LoggerInterface'
Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper:
arguments:
$tokenManager: '@Chill\MainBundle\Security\PasswordRecover\TokenManager'
$urlGenerator: '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
$mailer: '@Chill\MainBundle\Notification\Mailer'
$routeParameters: "%chill_main.notifications%"
Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEventSubscriber:
arguments:
$locker: '@Chill\MainBundle\Security\PasswordRecover\PasswordRecoverLocker'
tags:
- { name: kernel.event_subscriber }
Chill\MainBundle\Security\PasswordRecover\PasswordRecoverLocker:
arguments:
$chillRedis: '@Chill\MainBundle\Redis\ChillRedis'
$logger: '@Psr\Log\LoggerInterface'
Chill\MainBundle\Security\PasswordRecover\PasswordRecoverVoter:
arguments:
$locker: '@Chill\MainBundle\Security\PasswordRecover\PasswordRecoverLocker'
$requestStack: '@Symfony\Component\HttpFoundation\RequestStack'
tags:
- { name: security.voter }
- { name: security.voter }

View File

@ -30,6 +30,7 @@ services:
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
tags:
- { name: twig.extension }
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtensionInterface: Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension
Chill\MainBundle\Templating\Entity\CommentRender:
arguments:
@ -37,7 +38,7 @@ services:
- '@Symfony\Component\Templating\EngineInterface'
tags:
- { name: 'chill.render_entity' }
Chill\MainBundle\Templating\ChillMarkdownRenderExtension:
tags:
- { name: twig.extension }

View File

@ -13,30 +13,3 @@ services:
- '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
tags:
- { name: form.type, alias: '@chill.main.form.person_creation' }
chill.person.accompanying_period_closing_motive:
class: Chill\PersonBundle\Form\Type\ClosingMotivePickerType
arguments:
$translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
$chillEntityRenderExtension: '@Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension'
$closingMotiveRepository: '@Chill\PersonBundle\Repository\AccompanyingPeriod\ClosingMotiveRepository'
tags:
- { name: form.type, alias: closing_motive }
chill.person.form.type.pick_person:
class: Chill\PersonBundle\Form\Type\PickPersonType
arguments:
- Chill\PersonBundle\Repository\PersonRepository"
- "@security.token_storage"
- "@chill.main.security.authorization.helper"
- '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
- '@Symfony\Component\Translation\TranslatorInterface'
tags:
- { name: form.type }
Chill\PersonBundle\Form\Type\PersonAltNameType:
arguments:
$configHelper: '@Chill\PersonBundle\Config\ConfigPersonAltNamesHelper'
$translatableStringHelper: '@chill.main.helper.translatable_string'
tags:
- { name: form.type }

View File

@ -3,13 +3,11 @@
namespace Chill\PersonBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtensionInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Component\OptionsResolver\Options;
use Chill\PersonBundle\Repository\AccompanyingPeriod\ClosingMotiveRepository;
@ -23,12 +21,12 @@ class ClosingMotivePickerType extends AbstractType
{
/**
* @var TranslatableStringHelper
* @var TranslatableStringHelperInterface
*/
protected $translatableStringHelper;
/**
* @var ChillEntityRenderExtension
* @var ChillEntityRenderExtensionInterface
*/
protected $entityRenderExtension;
@ -40,20 +38,20 @@ class ClosingMotivePickerType extends AbstractType
/**
* ClosingMotivePickerType constructor.
*
* @param TranslatableStringHelper $translatableStringHelper
* @param ChillEntityRenderExtension $chillEntityRenderExtension
* @param TranslatableStringHelperInterface $translatableStringHelper
* @param ChillEntityRenderExtensionInterface $chillEntityRenderExtension
* @param ClosingMotiveRepository $closingMotiveRepository
*/
public function __construct(
TranslatableStringHelper $translatableStringHelper,
ChillEntityRenderExtension $chillEntityRenderExtension,
TranslatableStringHelperInterface $translatableStringHelper,
ChillEntityRenderExtensionInterface $chillEntityRenderExtension,
ClosingMotiveRepository $closingMotiveRepository
) {
$this->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) {

View File

@ -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;
}

View File

@ -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;