You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.0KB

  1. <?php
  2. namespace Lc\SovBundle\Controller\User;
  3. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  8. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
  13. use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  18. use Lc\SovBundle\Container\User\UserContainer;
  19. use Lc\SovBundle\Controller\AbstractAdminController;
  20. use Lc\SovBundle\Definition\ActionDefinition;
  21. use Lc\SovBundle\Definition\ApplicationDefinition;
  22. use Lc\SovBundle\Definition\RolesDefinition;
  23. use Lc\SovBundle\Definition\RolesDefinitionInterface;
  24. use Lc\SovBundle\Doctrine\EntityManager;
  25. use Lc\SovBundle\Doctrine\Extension\BlameableInterface;
  26. use Lc\SovBundle\Factory\User\UserFactory;
  27. use Lc\SovBundle\Translation\TranslatorAdmin;
  28. use Symfony\Component\HttpFoundation\RequestStack;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. abstract class UserAdminController extends AbstractAdminController
  31. {
  32. public function buildIndexActions(Actions $actions): void
  33. {
  34. parent::buildIndexActions($actions); // TODO: Change the autogenerated stub
  35. $actions->add(Crud::PAGE_INDEX, $this->getSwitchUserAction());
  36. }
  37. public function getSwitchUserAction(): Action
  38. {
  39. $switchAction = Action::new(
  40. ActionDefinition::SWITCH_USER,
  41. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER),
  42. 'fa fa-fw fa-user-secret'
  43. )
  44. ->linkToCrudAction(ActionDefinition::SWITCH_USER)
  45. ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER))
  46. ->setCssClass('in-dropdown text-info action-confirm action_switch');
  47. return $switchAction;
  48. }
  49. public function overrideEntitiesActions(?EntityCollection $entities, string $pageName): void
  50. {
  51. parent::overrideEntitiesActions($entities, $pageName); // TODO: Change the autogenerated stub
  52. foreach ($entities as $entity) {
  53. foreach ($entity->getActions() as $action) {
  54. if ($action->getName() == ActionDefinition::SWITCH_USER) {
  55. $url = $this->generateUrl(
  56. $this->getParameter('lc_sov.homepage_route'),
  57. array('_switch_user' => $entity->getInstance()->getEmail())
  58. );
  59. $action->setLinkUrl($url);
  60. }
  61. }
  62. }
  63. }
  64. public function configureFields(string $pageName): iterable
  65. {
  66. return [
  67. EmailField::new('email'),
  68. TextField::new('lastname'),
  69. TextField::new('firstname'),
  70. ChoiceField::new('roles')
  71. ->allowMultipleChoices()
  72. ->autocomplete()
  73. ->setChoices($this->getUserContainer()->getRoleDefinition()->getRolesList())
  74. ];
  75. }
  76. public function createEntity(string $entityFqcn)
  77. {
  78. return $this->get(UserContainer::class)->getFactory()->create();
  79. }
  80. public function delete(AdminContext $context)
  81. {
  82. $eaBeforeCrudActionEventDelete = $this->eaBeforeCrudActionEventDelete($context);
  83. if(!is_null($eaBeforeCrudActionEventDelete)){
  84. return $eaBeforeCrudActionEventDelete;
  85. }
  86. $entityInstance = $context->getEntity()->getInstance();
  87. $event = new BeforeEntityDeletedEvent($entityInstance);
  88. $this->container->get('event_dispatcher')->dispatch($event);
  89. if ($event->isPropagationStopped()) {
  90. return $event->getResponse();
  91. }
  92. $user = $event->getEntityInstance();
  93. $metas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
  94. foreach ($metas as $meta) {
  95. $entityFqcnList[] = $meta->getName();
  96. }
  97. // Creéer formulaire avec un champ confirm
  98. //si clqiue suppression
  99. $entitiesToDelete = array();
  100. foreach ($entityFqcnList as $entityFqcn){
  101. if(!is_null($this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn))) {
  102. $repositoryQuery = $this->get(
  103. $this->getApplicationDefinition()->getContainerByEntityFqcn($entityFqcn)
  104. )->getRepositoryQuery();
  105. if (new $entityFqcn instanceof BlameableInterface) {
  106. //CreatedBy
  107. $query = $repositoryQuery->create();
  108. $query->filterByCreatedBy($user);
  109. $entitiesToDelete[$entityFqcn]['nullify']['createdBy'] = $query->find();
  110. //UpdatedBy
  111. $query = $repositoryQuery->create();
  112. $query->filterByUpdatedBy($user);
  113. $entitiesToDelete[$entityFqcn]['nullify']['updatedBy'] = $query->find();
  114. }
  115. }
  116. dump($entityFqcn);
  117. }
  118. $responseParameters = $this->configureResponseParameters(KeyValueStore::new([
  119. 'pageName' => Crud::PAGE_DETAIL,
  120. 'templatePath' => '@LcSov/adminlte/crud/delete.html.twig',
  121. 'global_actions' => array(),
  122. 'batch_actions' => array(),
  123. 'entities_delete' => $entitiesToDelete,
  124. ]));
  125. $event = new AfterCrudActionEvent($context, $responseParameters);
  126. $this->get('event_dispatcher')->dispatch($event);
  127. if ($event->isPropagationStopped()) {
  128. return $event->getResponse();
  129. }
  130. return $responseParameters;
  131. }
  132. }