src/Command/App/AppInitRightsCommand.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Command\App;
  3. use App\Entity\Utilisateur\Droit;
  4. use App\Entity\Utilisateur\TypeUtilisateur;
  5. use App\Infrastructure\Doctrine\Annotation\SecuredEntity;
  6. use App\Repository\Utilisateur\DroitRepository;
  7. use App\Repository\Utilisateur\TypeUtilisateurRepository;
  8. use Doctrine\Common\Annotations\AnnotationReader;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Style\SymfonyStyle;
  14. use Symfony\Component\Finder\Finder;
  15. class AppInitRightsCommand extends Command
  16. {
  17.     protected static                  $defaultName        'app:initRights';
  18.     protected static                  $defaultDescription 'Add rights on Missing @SecuredEntity';
  19.     private string                    $kernelPath;
  20.     private EntityManagerInterface    $entityManager;
  21.     private TypeUtilisateurRepository $typeUtilisateurRepository;
  22.     private DroitRepository           $droitRepository;
  23.     public function __construct(string $kernelPathEntityManagerInterface $entityManagerTypeUtilisateurRepository $typeUtilisateurRepositoryDroitRepository $droitRepositorystring $name null)
  24.     {
  25.         parent::__construct($name);
  26.         $this->kernelPath                $kernelPath;
  27.         $this->entityManager             $entityManager;
  28.         $this->typeUtilisateurRepository $typeUtilisateurRepository;
  29.         $this->droitRepository           $droitRepository;
  30.     }
  31.     protected function execute(InputInterface $inputOutputInterface $output): int
  32.     {
  33.         $io = new SymfonyStyle($input$output);
  34.         $typesUtilisateurs $this->typeUtilisateurRepository->findAll();
  35.         $RightTitle        = [];
  36.         $pos               0;
  37.         $reader            = new AnnotationReader();
  38.         $finder            = new Finder();
  39.         $finder->files()->name('*.php')->contains('@SecuredEntity')->sortByName();
  40.         foreach ($finder->in($this->kernelPath '/src/Entity') as $file) {
  41.             $namespace $file->getRelativePath();
  42.             if (is_array($typesUtilisateurs) && count($typesUtilisateurs) > && !isset($RightTitle[$namespace])) {
  43.                 $pos+=100;
  44.                 foreach ($typesUtilisateurs as $typesUtilisateur) {
  45.                     if (!isset($RightTitle[$namespace][$typesUtilisateur->getId()])) {
  46.                         $right $this->droitRepository->findOneBy(['typesUtilisateur' => $typesUtilisateur'libelle' => $namespace'entite' => '']);
  47.                         if ($right === null) {
  48.                             $droit = new Droit();
  49.                             $droit->setLibelle($namespace);
  50.                             $droit->setTypesUtilisateur($typesUtilisateur);
  51.                             $droit->setEntite('');
  52.                             $droit->setVoir(true);
  53.                             $droit->setAjouter(true);
  54.                             $droit->setModifier(true);
  55.                             $droit->setSupprimer(true);
  56.                             $droit->setExporter(true);
  57.                             $droit->setStatut(true);
  58.                             $droit->setPosition($pos);
  59.                             $this->entityManager->persist($droit);
  60.                             $RightTitle[$namespace][$typesUtilisateur->getId()] = $droit;
  61.                         } else {
  62.                             $RightTitle[$namespace][$typesUtilisateur->getId()] = $right;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             $class     'App\Entity\\' str_replace(['/''.php'], ['\\'''], $file->getRelativePathname());
  68.             $reflClass = new \ReflectionClass($class);
  69.             /** @var SecuredEntity $myAnnotation */
  70.             $securedEntityAnnotation $reader->getClassAnnotation($reflClassSecuredEntity::class);
  71.             $libelle                 $securedEntityAnnotation->getName();
  72.             if (is_array($typesUtilisateurs) && count($typesUtilisateurs) > 0) {
  73.                 foreach ($typesUtilisateurs as $typesUtilisateur) {
  74.                     if ($this->droitRepository->findOneBy(['typesUtilisateur' => $typesUtilisateur'entite' => $class]) === null) {
  75.                         $pos++;
  76.                         $droit = new Droit();
  77.                         $droit->setLibelle($libelle);
  78.                         $droit->setTypesUtilisateur($typesUtilisateur);
  79.                         $droit->setParent($RightTitle[$namespace][$typesUtilisateur->getId()]);
  80.                         $droit->setEntite($class);
  81.                         $droit->setVoir(true);
  82.                         $droit->setAjouter(true);
  83.                         $droit->setModifier(true);
  84.                         $droit->setSupprimer(true);
  85.                         $droit->setExporter(true);
  86.                         $droit->setStatut(true);
  87.                         $droit->setPosition($pos);
  88.                         $this->entityManager->persist($droit);
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         $this->entityManager->flush();
  94.         $io->success('Rights added on Missing @SecuredEntity.');
  95.         return Command::SUCCESS;
  96.     }
  97. }