mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 14:54:57 +00:00
Merge branch 'rector/rules-up-to-php82' into rector/rules-symfony
This commit is contained in:
@@ -13,12 +13,16 @@ namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\SavedExport;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Export\DirectExportInterface;
|
||||
use Chill\MainBundle\Export\ExportFormHelper;
|
||||
use Chill\MainBundle\Export\ExportInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Form\SavedExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\ExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\FormatterType;
|
||||
use Chill\MainBundle\Form\Type\Export\PickCenterType;
|
||||
use Chill\MainBundle\Redis\ChillRedis;
|
||||
use Chill\MainBundle\Repository\SavedExportRepositoryInterface;
|
||||
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use LogicException;
|
||||
@@ -36,6 +40,7 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function count;
|
||||
use function serialize;
|
||||
@@ -47,20 +52,29 @@ use function unserialize;
|
||||
*/
|
||||
class ExportController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly ChillRedis $redis, private readonly ExportManager $exportManager, private readonly FormFactoryInterface $formFactory, private readonly LoggerInterface $logger, private readonly SessionInterface $session, private readonly TranslatorInterface $translator, private readonly EntityManagerInterface $entityManager)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ChillRedis $redis,
|
||||
private readonly ExportManager $exportManager,
|
||||
private readonly FormFactoryInterface $formFactory,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly SessionInterface $session,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly ExportFormHelper $exportFormHelper,
|
||||
private readonly SavedExportRepositoryInterface $savedExportRepository,
|
||||
private readonly Security $security
|
||||
) {
|
||||
}
|
||||
|
||||
public function downloadResultAction(Request $request, $alias)
|
||||
{
|
||||
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
$export = $exportManager->getExport($alias);
|
||||
|
||||
$key = $request->query->get('key', null);
|
||||
$savedExport = $this->getSavedExportFromRequest($request);
|
||||
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key);
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key, $savedExport);
|
||||
|
||||
$formatterAlias = $exportManager->getFormatterAlias($dataExport['export']);
|
||||
|
||||
@@ -74,6 +88,7 @@ class ExportController extends AbstractController
|
||||
'alias' => $alias,
|
||||
'export' => $export,
|
||||
'export_group' => $this->getExportGroup($export),
|
||||
'saved_export' => $savedExport,
|
||||
];
|
||||
|
||||
if ($formater instanceof \Chill\MainBundle\Export\Formatter\CSVListFormatter) {
|
||||
@@ -98,8 +113,9 @@ class ExportController extends AbstractController
|
||||
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$key = $request->query->get('key', null);
|
||||
$savedExport = $this->getSavedExportFromRequest($request);
|
||||
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key);
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key, $savedExport);
|
||||
|
||||
return $exportManager->generate(
|
||||
$alias,
|
||||
@@ -158,12 +174,8 @@ class ExportController extends AbstractController
|
||||
* 3. 'generate': gather data from session from the previous steps, and
|
||||
* make a redirection to the "generate" action with data in query (HTTP GET)
|
||||
*
|
||||
* @param string $request
|
||||
* @param Request $alias
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function newAction(Request $request, $alias)
|
||||
public function newAction(Request $request, string $alias): Response
|
||||
{
|
||||
// first check for ACL
|
||||
$exportManager = $this->exportManager;
|
||||
@@ -173,17 +185,41 @@ class ExportController extends AbstractController
|
||||
throw $this->createAccessDeniedException('The user does not have access to this export');
|
||||
}
|
||||
|
||||
$savedExport = $this->getSavedExportFromRequest($request);
|
||||
|
||||
$step = $request->query->getAlpha('step', 'centers');
|
||||
|
||||
return match ($step) {
|
||||
'centers' => $this->selectCentersStep($request, $export, $alias),
|
||||
'export' => $this->exportFormStep($request, $export, $alias),
|
||||
'formatter' => $this->formatterFormStep($request, $export, $alias),
|
||||
'generate' => $this->forwardToGenerate($request, $export, $alias),
|
||||
'centers' => $this->selectCentersStep($request, $export, $alias, $savedExport),
|
||||
'export' => $this->exportFormStep($request, $export, $alias, $savedExport),
|
||||
'formatter' => $this->formatterFormStep($request, $export, $alias, $savedExport),
|
||||
'generate' => $this->forwardToGenerate($request, $export, $alias, $savedExport),
|
||||
default => throw $this->createNotFoundException("The given step '{$step}' is invalid"),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{_locale}/export/saved/update-from-key/{id}/{key}", name="chill_main_export_saved_edit_options_from_key")
|
||||
*/
|
||||
public function editSavedExportOptionsFromKey(SavedExport $savedExport, string $key): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$data = $this->rebuildRawData($key);
|
||||
|
||||
$savedExport
|
||||
->setOptions($data);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_saved_edit', ['id' => $savedExport->getId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{_locale}/export/save-from-key/{alias}/{key}", name="chill_main_export_save_from_key")
|
||||
*/
|
||||
@@ -227,44 +263,55 @@ class ExportController extends AbstractController
|
||||
/**
|
||||
* create a form to show on different steps.
|
||||
*
|
||||
* @param string $alias
|
||||
* @param array $data the data from previous step. Required for steps 'formatter' and 'generate_formatter'
|
||||
*/
|
||||
protected function createCreateFormExport($alias, mixed $step, $data = []): FormInterface
|
||||
protected function createCreateFormExport(string $alias, string $step, array $data, ?SavedExport $savedExport): FormInterface
|
||||
{
|
||||
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$isGenerate = str_starts_with((string) $step, 'generate_');
|
||||
$isGenerate = str_starts_with($step, 'generate_');
|
||||
|
||||
$options = match ($step) {
|
||||
'export', 'generate_export' => [
|
||||
'export_alias' => $alias,
|
||||
'picked_centers' => $exportManager->getPickedCenters($data['centers'])
|
||||
],
|
||||
'formatter', 'generate_formatter' => [
|
||||
'export_alias' => $alias,
|
||||
'formatter_alias' => $exportManager->getFormatterAlias($data['export']),
|
||||
'aggregator_aliases' => $exportManager->getUsedAggregatorsAliases($data['export']),
|
||||
],
|
||||
default => [
|
||||
'export_alias' => $alias,
|
||||
],
|
||||
};
|
||||
|
||||
$defaultFormData = match ($savedExport) {
|
||||
null => $this->exportFormHelper->getDefaultData($step, $exportManager->getExport($alias), $options),
|
||||
default => $this->exportFormHelper->savedExportDataToFormData($savedExport, $step, $options),
|
||||
};
|
||||
|
||||
$builder = $this->formFactory
|
||||
->createNamedBuilder(null, FormType::class, [], [
|
||||
'method' => $isGenerate ? 'GET' : 'POST',
|
||||
'csrf_protection' => $isGenerate ? false : true,
|
||||
]);
|
||||
|
||||
// TODO: add a condition to be able to select a regroupment of centers?
|
||||
->createNamedBuilder(
|
||||
null,
|
||||
FormType::class,
|
||||
$defaultFormData,
|
||||
[
|
||||
'method' => $isGenerate ? 'GET' : 'POST',
|
||||
'csrf_protection' => !$isGenerate,
|
||||
]
|
||||
);
|
||||
|
||||
if ('centers' === $step || 'generate_centers' === $step) {
|
||||
$builder->add('centers', PickCenterType::class, [
|
||||
'export_alias' => $alias,
|
||||
]);
|
||||
$builder->add('centers', PickCenterType::class, $options);
|
||||
}
|
||||
|
||||
if ('export' === $step || 'generate_export' === $step) {
|
||||
$builder->add('export', ExportType::class, [
|
||||
'export_alias' => $alias,
|
||||
'picked_centers' => $exportManager->getPickedCenters($data['centers']),
|
||||
]);
|
||||
$builder->add('export', ExportType::class, $options);
|
||||
}
|
||||
|
||||
if ('formatter' === $step || 'generate_formatter' === $step) {
|
||||
$builder->add('formatter', FormatterType::class, [
|
||||
'formatter_alias' => $exportManager
|
||||
->getFormatterAlias($data['export']),
|
||||
'export_alias' => $alias,
|
||||
'aggregator_aliases' => $exportManager
|
||||
->getUsedAggregatorsAliases($data['export']),
|
||||
]);
|
||||
$builder->add('formatter', FormatterType::class, $options);
|
||||
}
|
||||
|
||||
$builder->add('submit', SubmitType::class, [
|
||||
@@ -279,13 +326,8 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* When the method is POST, the form is stored if valid, and a redirection
|
||||
* is done to next step.
|
||||
*
|
||||
* @param string $alias
|
||||
* @param \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function exportFormStep(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias)
|
||||
private function exportFormStep(Request $request, DirectExportInterface|ExportInterface $export, string $alias, ?SavedExport $savedExport = null): Response
|
||||
{
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
@@ -301,7 +343,7 @@ class ExportController extends AbstractController
|
||||
|
||||
$export = $exportManager->getExport($alias);
|
||||
|
||||
$form = $this->createCreateFormExport($alias, 'export', $data);
|
||||
$form = $this->createCreateFormExport($alias, 'export', $data, $savedExport);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
@@ -323,6 +365,7 @@ class ExportController extends AbstractController
|
||||
$this->generateUrl('chill_main_export_new', [
|
||||
'step' => $this->getNextStep('export', $export),
|
||||
'alias' => $alias,
|
||||
'from_saved' => $request->get('from_saved', '')
|
||||
])
|
||||
);
|
||||
}
|
||||
@@ -343,13 +386,8 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* If the form is posted and valid, store the data in session and
|
||||
* redirect to the next step.
|
||||
*
|
||||
* @param \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export
|
||||
* @param string $alias
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected function formatterFormStep(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias)
|
||||
private function formatterFormStep(Request $request, DirectExportInterface|ExportInterface $export, string $alias, ?SavedExport $savedExport = null): Response
|
||||
{
|
||||
// check we have data from the previous step (export step)
|
||||
$data = $this->session->get('export_step', null);
|
||||
@@ -361,7 +399,7 @@ class ExportController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
$form = $this->createCreateFormExport($alias, 'formatter', $data);
|
||||
$form = $this->createCreateFormExport($alias, 'formatter', $data, $savedExport);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
@@ -380,6 +418,7 @@ class ExportController extends AbstractController
|
||||
[
|
||||
'alias' => $alias,
|
||||
'step' => $this->getNextStep('formatter', $export),
|
||||
'from_saved' => $request->get('from_saved', ''),
|
||||
]
|
||||
));
|
||||
}
|
||||
@@ -406,7 +445,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
||||
*/
|
||||
protected function forwardToGenerate(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias)
|
||||
private function forwardToGenerate(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias, ?SavedExport $savedExport)
|
||||
{
|
||||
$dataCenters = $this->session->get('centers_step_raw', null);
|
||||
$dataFormatter = $this->session->get('formatter_step_raw', null);
|
||||
@@ -414,7 +453,9 @@ class ExportController extends AbstractController
|
||||
|
||||
if (null === $dataFormatter && $export instanceof \Chill\MainBundle\Export\ExportInterface) {
|
||||
return $this->redirectToRoute('chill_main_export_new', [
|
||||
'alias' => $alias, 'step' => $this->getNextStep('generate', $export, true),
|
||||
'alias' => $alias,
|
||||
'step' => $this->getNextStep('generate', $export, true),
|
||||
'from_saved' => $savedExport?->getId() ?? '',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -435,20 +476,24 @@ class ExportController extends AbstractController
|
||||
$this->session->remove('formatter_step_raw');
|
||||
$this->session->remove('formatter_step');
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_download', ['key' => $key, 'alias' => $alias]);
|
||||
return $this->redirectToRoute('chill_main_export_download', [
|
||||
'key' => $key,
|
||||
'alias' => $alias,
|
||||
'from_saved' => $savedExport?->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function rebuildData($key)
|
||||
private function rebuildData($key, ?SavedExport $savedExport)
|
||||
{
|
||||
$rawData = $this->rebuildRawData($key);
|
||||
|
||||
$alias = $rawData['alias'];
|
||||
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers', [], $savedExport);
|
||||
$formCenters->submit($rawData['centers']);
|
||||
$dataCenters = $formCenters->getData();
|
||||
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters, $savedExport);
|
||||
$formExport->submit($rawData['export']);
|
||||
$dataExport = $formExport->getData();
|
||||
|
||||
@@ -456,7 +501,8 @@ class ExportController extends AbstractController
|
||||
$formFormatter = $this->createCreateFormExport(
|
||||
$alias,
|
||||
'generate_formatter',
|
||||
$dataExport
|
||||
$dataExport,
|
||||
$savedExport
|
||||
);
|
||||
$formFormatter->submit($rawData['formatter']);
|
||||
$dataFormatter = $formFormatter->getData();
|
||||
@@ -469,12 +515,12 @@ class ExportController extends AbstractController
|
||||
* @param string $alias
|
||||
* @return Response
|
||||
*/
|
||||
protected function selectCentersStep(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias)
|
||||
private function selectCentersStep(Request $request, \Chill\MainBundle\Export\DirectExportInterface|\Chill\MainBundle\Export\ExportInterface $export, $alias, ?SavedExport $savedExport = null)
|
||||
{
|
||||
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
$form = $this->createCreateFormExport($alias, 'centers');
|
||||
$form = $this->createCreateFormExport($alias, 'centers', [], $savedExport);
|
||||
|
||||
if ($request->getMethod() === 'POST') {
|
||||
$form->handleRequest($request);
|
||||
@@ -506,6 +552,7 @@ class ExportController extends AbstractController
|
||||
return $this->redirectToRoute('chill_main_export_new', [
|
||||
'step' => $this->getNextStep('centers', $export),
|
||||
'alias' => $alias,
|
||||
'from_saved' => $request->get('from_saved', ''),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -612,4 +659,18 @@ class ExportController extends AbstractController
|
||||
|
||||
return $rawData;
|
||||
}
|
||||
|
||||
private function getSavedExportFromRequest(Request $request): ?SavedExport
|
||||
{
|
||||
$savedExport = match ($savedExportId = $request->query->get('from_saved', '')) {
|
||||
'' => null,
|
||||
default => $this->savedExportRepository->find($savedExportId),
|
||||
};
|
||||
|
||||
if (null !== $savedExport && !$this->security->isGranted(SavedExportVoter::EDIT, $savedExport)) {
|
||||
throw new AccessDeniedHttpException("saved export edition not allowed");
|
||||
}
|
||||
|
||||
return $savedExport;
|
||||
}
|
||||
}
|
||||
|
@@ -16,14 +16,18 @@ use Chill\MainBundle\Entity\RoleScope;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Form\PermissionsGroupType;
|
||||
use Chill\MainBundle\Form\Type\ComposedRoleScopeType;
|
||||
use Chill\MainBundle\Repository\PermissionsGroupRepository;
|
||||
use Chill\MainBundle\Repository\RoleScopeRepository;
|
||||
use Chill\MainBundle\Security\RoleProvider;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Security\Core\Role\RoleHierarchy;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
@@ -32,27 +36,28 @@ use function array_key_exists;
|
||||
/**
|
||||
* Class PermissionsGroupController.
|
||||
*/
|
||||
class PermissionsGroupController extends AbstractController
|
||||
final class PermissionsGroupController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* PermissionsGroupController constructor.
|
||||
*/
|
||||
public function __construct(private readonly TranslatableStringHelper $translatableStringHelper, private readonly RoleProvider $roleProvider, private readonly RoleHierarchy $roleHierarchy, private readonly TranslatorInterface $translator, private readonly ValidatorInterface $validator)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TranslatableStringHelper $translatableStringHelper,
|
||||
private readonly RoleProvider $roleProvider,
|
||||
private readonly RoleHierarchyInterface $roleHierarchy,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly ValidatorInterface $validator,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly PermissionsGroupRepository $permissionsGroupRepository,
|
||||
private readonly RoleScopeRepository $roleScopeRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @throws type
|
||||
*
|
||||
* @return Respon
|
||||
*/
|
||||
public function addLinkRoleScopeAction(Request $request, $id)
|
||||
public function addLinkRoleScopeAction(Request $request, int $id): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
|
||||
$permissionsGroup = $this->permissionsGroupRepository->find($id);
|
||||
|
||||
if (!$permissionsGroup) {
|
||||
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
|
||||
@@ -71,7 +76,7 @@ class PermissionsGroupController extends AbstractController
|
||||
$violations = $this->validator->validate($permissionsGroup);
|
||||
|
||||
if ($violations->count() === 0) {
|
||||
$em->flush();
|
||||
$this->em->flush();
|
||||
|
||||
$this->addFlash(
|
||||
'notice',
|
||||
@@ -131,16 +136,15 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Creates a new PermissionsGroup entity.
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
public function createAction(Request $request): Response
|
||||
{
|
||||
$permissionsGroup = new PermissionsGroup();
|
||||
$form = $this->createCreateForm($permissionsGroup);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($permissionsGroup);
|
||||
$em->flush();
|
||||
$this->em->persist($permissionsGroup);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->redirect($this->generateUrl(
|
||||
'admin_permissionsgroup_edit',
|
||||
@@ -156,18 +160,11 @@ class PermissionsGroupController extends AbstractController
|
||||
|
||||
/**
|
||||
* remove an association between permissionsGroup and roleScope.
|
||||
*
|
||||
* @param int $pgid permissionsGroup id
|
||||
* @param int $rsid roleScope id
|
||||
*
|
||||
* @return redirection to edit form
|
||||
*/
|
||||
public function deleteLinkRoleScopeAction($pgid, $rsid)
|
||||
public function deleteLinkRoleScopeAction(int $pgid, int $rsid): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($pgid);
|
||||
$roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class)->find($rsid);
|
||||
$permissionsGroup = $this->permissionsGroupRepository->find($pgid);
|
||||
$roleScope = $this->roleScopeRepository->find($rsid);
|
||||
|
||||
if (!$permissionsGroup) {
|
||||
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
|
||||
@@ -196,7 +193,7 @@ class PermissionsGroupController extends AbstractController
|
||||
));
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$this->em->flush();
|
||||
|
||||
if ($roleScope->getScope() !== null) {
|
||||
$this->addFlash(
|
||||
@@ -226,11 +223,9 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Displays a form to edit an existing PermissionsGroup entity.
|
||||
*/
|
||||
public function editAction(mixed $id)
|
||||
public function editAction(int $id): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
|
||||
$permissionsGroup = $this->permissionsGroupRepository->find($id);
|
||||
|
||||
if (!$permissionsGroup) {
|
||||
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
|
||||
@@ -274,11 +269,9 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Lists all PermissionsGroup entities.
|
||||
*/
|
||||
public function indexAction()
|
||||
public function indexAction(): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entities = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->findAll();
|
||||
$entities = $this->permissionsGroupRepository->findAllOrderedAlphabetically();
|
||||
|
||||
return $this->render('@ChillMain/PermissionsGroup/index.html.twig', [
|
||||
'entities' => $entities,
|
||||
@@ -288,7 +281,7 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Displays a form to create a new PermissionsGroup entity.
|
||||
*/
|
||||
public function newAction()
|
||||
public function newAction(): Response
|
||||
{
|
||||
$permissionsGroup = new PermissionsGroup();
|
||||
$form = $this->createCreateForm($permissionsGroup);
|
||||
@@ -302,11 +295,9 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Finds and displays a PermissionsGroup entity.
|
||||
*/
|
||||
public function showAction(mixed $id)
|
||||
public function showAction(int $id): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$permissionsGroup = $em->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)->find($id);
|
||||
$permissionsGroup = $this->permissionsGroupRepository->find($id);
|
||||
|
||||
if (!$permissionsGroup) {
|
||||
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
|
||||
@@ -355,12 +346,9 @@ class PermissionsGroupController extends AbstractController
|
||||
/**
|
||||
* Edits an existing PermissionsGroup entity.
|
||||
*/
|
||||
public function updateAction(Request $request, mixed $id)
|
||||
public function updateAction(Request $request, int $id): Response
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$permissionsGroup = $em
|
||||
->getRepository(\Chill\MainBundle\Entity\PermissionsGroup::class)
|
||||
$permissionsGroup = $this->permissionsGroupRepository
|
||||
->find($id);
|
||||
|
||||
if (!$permissionsGroup) {
|
||||
@@ -372,7 +360,7 @@ class PermissionsGroupController extends AbstractController
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
$this->em->flush();
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit', ['id' => $id]));
|
||||
}
|
||||
@@ -411,18 +399,11 @@ class PermissionsGroupController extends AbstractController
|
||||
|
||||
/**
|
||||
* get a role scope by his parameters. The role scope is persisted if it
|
||||
* doesn't exists in database.
|
||||
*
|
||||
* @param Scope $scope
|
||||
* @param string $role
|
||||
*
|
||||
* @return RoleScope
|
||||
* doesn't exist in database.
|
||||
*/
|
||||
protected function getPersistentRoleScopeBy($role, ?Scope $scope = null)
|
||||
protected function getPersistentRoleScopeBy(string $role, ?Scope $scope = null): RoleScope
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$roleScope = $em->getRepository(\Chill\MainBundle\Entity\RoleScope::class)
|
||||
$roleScope = $this->roleScopeRepository
|
||||
->findOneBy(['role' => $role, 'scope' => $scope]);
|
||||
|
||||
if (null === $roleScope) {
|
||||
@@ -430,7 +411,7 @@ class PermissionsGroupController extends AbstractController
|
||||
->setRole($role)
|
||||
->setScope($scope);
|
||||
|
||||
$em->persist($roleScope);
|
||||
$this->em->persist($roleScope);
|
||||
}
|
||||
|
||||
return $roleScope;
|
||||
@@ -438,10 +419,8 @@ class PermissionsGroupController extends AbstractController
|
||||
|
||||
/**
|
||||
* creates a form to add a role scope to permissionsgroup.
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createAddRoleScopeForm(PermissionsGroup $permissionsGroup)
|
||||
private function createAddRoleScopeForm(PermissionsGroup $permissionsGroup): FormInterface
|
||||
{
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl(
|
||||
@@ -458,10 +437,8 @@ class PermissionsGroupController extends AbstractController
|
||||
* Creates a form to create a PermissionsGroup entity.
|
||||
*
|
||||
* @param PermissionsGroup $permissionsGroup The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(PermissionsGroup $permissionsGroup)
|
||||
private function createCreateForm(PermissionsGroup $permissionsGroup): FormInterface
|
||||
{
|
||||
$form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [
|
||||
'action' => $this->generateUrl('admin_permissionsgroup_create'),
|
||||
@@ -477,13 +454,11 @@ class PermissionsGroupController extends AbstractController
|
||||
* Creates a form to delete a link to roleScope.
|
||||
*
|
||||
* @param mixed $permissionsGroup The entity id
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createDeleteRoleScopeForm(
|
||||
PermissionsGroup $permissionsGroup,
|
||||
RoleScope $roleScope
|
||||
) {
|
||||
): FormInterface {
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl(
|
||||
'admin_permissionsgroup_delete_role_scope',
|
||||
@@ -496,12 +471,8 @@ class PermissionsGroupController extends AbstractController
|
||||
|
||||
/**
|
||||
* Creates a form to edit a PermissionsGroup entity.
|
||||
*
|
||||
* @param PermissionsGroup $permissionsGroup The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(PermissionsGroup $permissionsGroup)
|
||||
private function createEditForm(PermissionsGroup $permissionsGroup): FormInterface
|
||||
{
|
||||
$form = $this->createForm(PermissionsGroupType::class, $permissionsGroup, [
|
||||
'action' => $this->generateUrl('admin_permissionsgroup_update', ['id' => $permissionsGroup->getId()]),
|
||||
@@ -515,10 +486,8 @@ class PermissionsGroupController extends AbstractController
|
||||
|
||||
/**
|
||||
* expand roleScopes to be easily shown in template.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getExpandedRoles(array $roleScopes)
|
||||
private function getExpandedRoles(array $roleScopes): array
|
||||
{
|
||||
$expandedRoles = [];
|
||||
|
||||
@@ -526,10 +495,10 @@ class PermissionsGroupController extends AbstractController
|
||||
if (!array_key_exists($roleScope->getRole(), $expandedRoles)) {
|
||||
$expandedRoles[$roleScope->getRole()] =
|
||||
array_map(
|
||||
static fn (Role $role) => $role->getRole(),
|
||||
static fn ($role) => $role,
|
||||
$this->roleHierarchy
|
||||
->getReachableRoles(
|
||||
[new Role($roleScope->getRole())]
|
||||
->getReachableRoleNames(
|
||||
[$roleScope->getRole()]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
142
src/Bundle/ChillMainBundle/Controller/UserExportController.php
Normal file
142
src/Bundle/ChillMainBundle/Controller/UserExportController.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Repository\UserRepositoryInterface;
|
||||
use League\Csv\Writer;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final readonly class UserExportController
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepositoryInterface $userRepository,
|
||||
private Security $security,
|
||||
private TranslatorInterface $translator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws \League\Csv\UnavailableStream
|
||||
*
|
||||
* @Route("/{_locale}/admin/main/users/export/list.{_format}", requirements={"_format": "csv"}, name="chill_main_users_export_list")
|
||||
*/
|
||||
public function userList(Request $request, string $_format = 'csv'): StreamedResponse
|
||||
{
|
||||
if (!$this->security->isGranted('ROLE_ADMIN')) {
|
||||
throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list');
|
||||
}
|
||||
|
||||
$users = $this->userRepository->findAllAsArray($request->getLocale());
|
||||
|
||||
$csv = Writer::createFromPath('php://temp', 'r+');
|
||||
$csv->insertOne(
|
||||
array_map(
|
||||
fn (string $e) => $this->translator->trans('admin.users.export.' . $e),
|
||||
[
|
||||
'id',
|
||||
'username',
|
||||
'email',
|
||||
'enabled',
|
||||
'civility_id',
|
||||
'civility_abbreviation',
|
||||
'civility_name',
|
||||
'label',
|
||||
'mainCenter_id' ,
|
||||
'mainCenter_name',
|
||||
'mainScope_id',
|
||||
'mainScope_name',
|
||||
'userJob_id',
|
||||
'userJob_name',
|
||||
'currentLocation_id',
|
||||
'currentLocation_name',
|
||||
'mainLocation_id',
|
||||
'mainLocation_name',
|
||||
'absenceStart'
|
||||
]
|
||||
)
|
||||
);
|
||||
$csv->addFormatter(fn (array $row) => null !== ($row['absenceStart'] ?? null) ? array_merge($row, ['absenceStart' => $row['absenceStart']->format('Y-m-d')]) : $row);
|
||||
$csv->insertAll($users);
|
||||
|
||||
return new StreamedResponse(
|
||||
function () use ($csv) {
|
||||
foreach ($csv->chunk(1024) as $chunk) {
|
||||
echo $chunk;
|
||||
flush();
|
||||
}
|
||||
},
|
||||
Response::HTTP_OK,
|
||||
[
|
||||
'Content-Encoding' => 'none',
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Content-Disposition' => 'attachment; users.csv',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \League\Csv\CannotInsertRecord
|
||||
* @throws \League\Csv\Exception
|
||||
* @throws \League\Csv\UnavailableStream
|
||||
* @Route("/{_locale}/admin/main/users/export/permissions.{_format}", requirements={"_format": "csv"}, name="chill_main_users_export_permissions")
|
||||
*/
|
||||
public function userPermissionsList(string $_format = 'csv'): StreamedResponse
|
||||
{
|
||||
if (!$this->security->isGranted('ROLE_ADMIN')) {
|
||||
throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list');
|
||||
}
|
||||
|
||||
$userPermissions = $this->userRepository->findAllUserACLAsArray();
|
||||
|
||||
$csv = Writer::createFromPath('php://temp', 'r+');
|
||||
$csv->insertOne(
|
||||
array_map(
|
||||
fn (string $e) => $this->translator->trans('admin.users.export.' . $e),
|
||||
[
|
||||
'id',
|
||||
'username',
|
||||
'email',
|
||||
'label',
|
||||
'enabled',
|
||||
'center_id',
|
||||
'center_name',
|
||||
'permissionsGroup_id',
|
||||
'permissionsGroup_name',
|
||||
]
|
||||
)
|
||||
);
|
||||
$csv->insertAll($userPermissions);
|
||||
|
||||
return new StreamedResponse(
|
||||
function () use ($csv) {
|
||||
foreach ($csv->chunk(1024) as $chunk) {
|
||||
echo $chunk;
|
||||
flush();
|
||||
}
|
||||
},
|
||||
Response::HTTP_OK,
|
||||
[
|
||||
'Content-Encoding' => 'none',
|
||||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||||
'Content-Disposition' => 'attachment; users.csv',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
@@ -335,9 +335,9 @@ class WorkflowController extends AbstractController
|
||||
}
|
||||
|
||||
// TODO symfony 5: add those "future" on context ($workflow->apply($entityWorkflow, $transition, $context)
|
||||
$entityWorkflow->futureCcUsers = $transitionForm['future_cc_users']->getData();
|
||||
$entityWorkflow->futureDestUsers = $transitionForm['future_dest_users']->getData();
|
||||
$entityWorkflow->futureDestEmails = $transitionForm['future_dest_emails']->getData();
|
||||
$entityWorkflow->futureCcUsers = $transitionForm['future_cc_users']->getData() ?? [];
|
||||
$entityWorkflow->futureDestUsers = $transitionForm['future_dest_users']->getData() ?? [];
|
||||
$entityWorkflow->futureDestEmails = $transitionForm['future_dest_emails']->getData() ?? [];
|
||||
|
||||
$workflow->apply($entityWorkflow, $transition);
|
||||
|
||||
|
Reference in New Issue
Block a user