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.

82 lines
3.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\Field\ChoiceField;
  8. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  10. use Lc\SovBundle\Container\User\UserContainer;
  11. use Lc\SovBundle\Controller\AbstractAdminController;
  12. use Lc\SovBundle\Definition\ActionDefinition;
  13. use Lc\SovBundle\Definition\RolesDefinition;
  14. use Lc\SovBundle\Definition\RolesDefinitionInterface;
  15. use Lc\SovBundle\Doctrine\EntityManager;
  16. use Lc\SovBundle\Factory\User\UserFactory;
  17. use Lc\SovBundle\Translation\TranslatorAdmin;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. abstract class UserAdminController extends AbstractAdminController
  21. {
  22. protected RolesDefinitionInterface $rolesDefinition;
  23. public function buildIndexActions(Actions $actions): void
  24. {
  25. parent::buildIndexActions($actions); // TODO: Change the autogenerated stub
  26. $actions->add(Crud::PAGE_INDEX, $this->getSwitchUserAction());
  27. }
  28. public function getSwitchUserAction(): Action
  29. {
  30. $switchAction = Action::new(
  31. ActionDefinition::SWITCH_USER,
  32. $this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER),
  33. 'fa fa-fw fa-user-secret'
  34. )
  35. ->linkToCrudAction(ActionDefinition::SWITCH_USER)
  36. ->setLabel($this->get(TranslatorAdmin::class)->transAction(ActionDefinition::SWITCH_USER))
  37. ->setCssClass('in-dropdown text-info action-confirm action_switch');
  38. return $switchAction;
  39. }
  40. public function overrideEntitiesActions(?EntityCollection $entities): void
  41. {
  42. parent::overrideEntitiesActions($entities); // TODO: Change the autogenerated stub
  43. foreach ($entities as $entity) {
  44. foreach ($entity->getActions() as $action){
  45. if($action->getName() == ActionDefinition::SWITCH_USER){
  46. $url = $this->generateUrl($this->getParameter('lc_sov.homepage_route'), array('_switch_user' => $entity->getInstance()->getEmail()));
  47. $action->setLinkUrl($url);
  48. }
  49. }
  50. }
  51. }
  52. public function configureFields(string $pageName): iterable
  53. {
  54. return [
  55. EmailField::new('email'),
  56. TextField::new('lastname'),
  57. TextField::new('firstname'),
  58. ChoiceField::new('roles')
  59. ->allowMultipleChoices()
  60. ->autocomplete()
  61. ->setChoices($this->rolesDefinition->getRolesList())
  62. ];
  63. }
  64. public function createEntity(string $entityFqcn)
  65. {
  66. return $this->get(UserContainer::class)->getFactory()->create();
  67. }
  68. }