Compute allowed centers and regroupment at the time of generating the export

This commit is contained in:
2025-04-03 17:47:46 +02:00
parent 128d365a72
commit e48bec490c
17 changed files with 449 additions and 111 deletions

View File

@@ -13,7 +13,11 @@ namespace Chill\MainBundle\Export;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\Exception\UnauthorizedGenerationException;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Service\Regroupement\CenterRegroupementResolver;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
@@ -27,14 +31,17 @@ final readonly class ExportGenerator
private ExportManager $exportManager,
private ExportConfigNormalizer $configNormalizer,
private LoggerInterface $logger,
private AuthorizationHelperInterface $authorizationHelper,
private CenterRegroupementResolver $centerRegroupementResolver,
) {}
public function generate(string $exportAlias, array $configuration, ?User $byUser = null): FormattedExportGeneration
{
$data = $this->configNormalizer->denormalizeConfig($exportAlias, $configuration);
$centers = $data['centers'];
$export = $this->exportManager->getExport($exportAlias);
$centers = $this->filterCenters($byUser, $data['centers']['centers'], $data['centers']['regroupments'], $export);
$context = new ExportGenerationContext($byUser);
if ($export instanceof DirectExportInterface) {
@@ -94,6 +101,7 @@ final readonly class ExportGenerator
}
}
/** @phpstan-ignore-next-line the method "generate" is not yet implemented on all formatters */
if (method_exists($formatter, 'generate')) {
return $formatter->generate(
$result,
@@ -119,6 +127,29 @@ final readonly class ExportGenerator
return new FormattedExportGeneration($generatedExport->getContent(), $generatedExport->headers->get('content-type'));
}
private function filterCenters(User $byUser, array $centers, array $regroupements, ExportInterface|DirectExportInterface $export): array
{
$authorizedCenters = new ArrayCollection($this->authorizationHelper->getReachableCenters($byUser, $export->requiredRole()));
if ($authorizedCenters->isEmpty()) {
throw new UnauthorizedGenerationException('No authorized centers');
}
$wantedCenters = $this->centerRegroupementResolver->resolveCenters($regroupements, $centers);
$resolvedCenters = [];
foreach ($wantedCenters as $wantedCenter) {
if ($authorizedCenters->contains($wantedCenter)) {
$resolvedCenters[] = $wantedCenter;
}
}
if ([] == $resolvedCenters) {
throw new UnauthorizedGenerationException('No common centers between wanted centers and authorized centers');
}
return $resolvedCenters;
}
/**
* parse the data to retrieve the used filters and aggregators.
*