From 2ce145cace112ba8200cdc605761c5b35f0589c8 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 12 Jul 2022 16:41:12 +0200 Subject: [PATCH] wip.. setting acl for new accompanying course exports --- .../Export/Export/CountAccompanyingCourse.php | 69 ++++++++++++++----- .../Authorization/AccompanyingPeriodVoter.php | 5 ++ .../config/services/exports.yaml | 3 +- .../translations/messages.fr.yml | 1 + 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php b/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php index 51eeee6fb..4984fd9ae 100644 --- a/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php +++ b/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php @@ -12,56 +12,93 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Export; use Chill\MainBundle\Export\ExportInterface; +use Chill\MainBundle\Export\FormatterInterface; +use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Query; +use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Security\Core\Role\Role; +use LogicException; class CountAccompanyingCourse implements ExportInterface { + /** + * @var EntityManagerInterface + */ + protected $entityManager; - public function buildForm(FormBuilderInterface $builder) + public function __construct( + EntityManagerInterface $em + ) { + $this->entityManager = $em; + } + + public function buildForm(FormBuilderInterface $builder): void { // TODO: Implement buildForm() method. } - public function getTitle() + public function getTitle(): string { return 'Count accompanying courses'; } - public function getAllowedFormattersTypes() + public function getAllowedFormattersTypes(): array { - // TODO: Implement getAllowedFormattersTypes() method. + return [FormatterInterface::TYPE_TABULAR]; } - public function getDescription() + public function getDescription(): string { return 'Count accompanying courses by various parameters'; } public function getLabels($key, array $values, $data) { - // TODO: Implement getLabels() method. + if ('export_result' !== $key) { + throw new LogicException("the key {$key} is not used by this export"); + } + + $labels = array_combine($values, $values); + $labels['_header'] = $this->getTitle(); + + return static function ($value) use ($labels) { + return $labels[$value]; + }; } - public function getQueryKeys($data) + public function getQueryKeys($data): array { - // TODO: Implement getQueryKeys() method. + return ['export_result']; } - public function getResult($query, $data) + public function getResult($qb, $data) { - // TODO: Implement getResult() method. + return $qb->getQuery()->getResult(Query::HYDRATE_SCALAR); } - public function getType() + public function getType(): string { - // TODO: Implement getType() method. + return 'accompanying_period'; } - public function initiateQuery(array $requiredModifiers, array $acl, array $data = []) + public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder { - // TODO: Implement initiateQuery() method. + $centers = array_map(static function ($el) { + return $el['center']; + }, $acl); + + $qb = $this->entityManager->createQueryBuilder(); + + $qb->select('COUNT(acp.id) AS export_result') + ->from('ChillPersonBundle:AccompanyingPeriod', 'acp') + ->join('acp.center', 'center') + ->andWhere('center IN (:authorized_centers)') + ->setParameter('authorized_centers', $centers); + + return $qb; } public function requiredRole(): Role @@ -69,8 +106,8 @@ class CountAccompanyingCourse implements ExportInterface return new Role(AccompanyingPeriodVoter::STATS); } - public function supportsModifiers() + public function supportsModifiers(): array { - // TODO: Implement supportsModifiers() method. + return []; } } \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/Security/Authorization/AccompanyingPeriodVoter.php b/src/Bundle/ChillPersonBundle/Security/Authorization/AccompanyingPeriodVoter.php index 549f18f85..0d8deb569 100644 --- a/src/Bundle/ChillPersonBundle/Security/Authorization/AccompanyingPeriodVoter.php +++ b/src/Bundle/ChillPersonBundle/Security/Authorization/AccompanyingPeriodVoter.php @@ -219,6 +219,11 @@ class AccompanyingPeriodVoter extends AbstractChillVoter implements ProvideRoleH return $token->getUser() === $subject->getUser(); } + + if (self::STATS === $attribute) { + return true; + } + } return $this->voterHelper->voteOnAttribute($attribute, $subject, $token); diff --git a/src/Bundle/ChillPersonBundle/config/services/exports.yaml b/src/Bundle/ChillPersonBundle/config/services/exports.yaml index 2424c34c4..23a62f914 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports.yaml @@ -26,9 +26,10 @@ services: tags: - { name: chill.export, alias: list_person_duplicate } - chill.person.export.export_count_accompanying_course: + chill.person.export.count_accompanying_course: class: Chill\PersonBundle\Export\Export\CountAccompanyingCourse arguments: + - "@doctrine.orm.entity_manager" tags: - { name: chill.export, alias: count_accompanying_course } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index aad43627f..25dc94665 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -315,6 +315,7 @@ CHILL_PERSON_ACCOMPANYING_PERIOD_UPDATE: Modifier une période d'accompagnement CHILL_PERSON_ACCOMPANYING_PERIOD_FULL: Voir les détails, créer, supprimer et mettre à jour une période d'accompagnement CHILL_PERSON_ACCOMPANYING_COURSE_REASSIGN_BULK: Réassigner les parcours en lot CHILL_PERSON_ACCOMPANYING_PERIOD_SEE_DETAILS: Voir les détails d'une période d'accompagnement +CHILL_PERSON_ACCOMPANYING_PERIOD_STATS: Statistiques sur les parcours d'accompagnement #period Period closed!: Période clôturée!