<?php
namespace App\Command\App;
use App\Entity\Utilisateur\Droit;
use App\Entity\Utilisateur\TypeUtilisateur;
use App\Infrastructure\Doctrine\Annotation\SecuredEntity;
use App\Repository\Utilisateur\DroitRepository;
use App\Repository\Utilisateur\TypeUtilisateurRepository;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
class AppInitRightsCommand extends Command
{
protected static $defaultName = 'app:initRights';
protected static $defaultDescription = 'Add rights on Missing @SecuredEntity';
private string $kernelPath;
private EntityManagerInterface $entityManager;
private TypeUtilisateurRepository $typeUtilisateurRepository;
private DroitRepository $droitRepository;
public function __construct(string $kernelPath, EntityManagerInterface $entityManager, TypeUtilisateurRepository $typeUtilisateurRepository, DroitRepository $droitRepository, string $name = null)
{
parent::__construct($name);
$this->kernelPath = $kernelPath;
$this->entityManager = $entityManager;
$this->typeUtilisateurRepository = $typeUtilisateurRepository;
$this->droitRepository = $droitRepository;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$typesUtilisateurs = $this->typeUtilisateurRepository->findAll();
$RightTitle = [];
$pos = 0;
$reader = new AnnotationReader();
$finder = new Finder();
$finder->files()->name('*.php')->contains('@SecuredEntity')->sortByName();
foreach ($finder->in($this->kernelPath . '/src/Entity') as $file) {
$namespace = $file->getRelativePath();
if (is_array($typesUtilisateurs) && count($typesUtilisateurs) > 0 && !isset($RightTitle[$namespace])) {
$pos+=100;
foreach ($typesUtilisateurs as $typesUtilisateur) {
if (!isset($RightTitle[$namespace][$typesUtilisateur->getId()])) {
$right = $this->droitRepository->findOneBy(['typesUtilisateur' => $typesUtilisateur, 'libelle' => $namespace, 'entite' => '']);
if ($right === null) {
$droit = new Droit();
$droit->setLibelle($namespace);
$droit->setTypesUtilisateur($typesUtilisateur);
$droit->setEntite('');
$droit->setVoir(true);
$droit->setAjouter(true);
$droit->setModifier(true);
$droit->setSupprimer(true);
$droit->setExporter(true);
$droit->setStatut(true);
$droit->setPosition($pos);
$this->entityManager->persist($droit);
$RightTitle[$namespace][$typesUtilisateur->getId()] = $droit;
} else {
$RightTitle[$namespace][$typesUtilisateur->getId()] = $right;
}
}
}
}
$class = 'App\Entity\\' . str_replace(['/', '.php'], ['\\', ''], $file->getRelativePathname());
$reflClass = new \ReflectionClass($class);
/** @var SecuredEntity $myAnnotation */
$securedEntityAnnotation = $reader->getClassAnnotation($reflClass, SecuredEntity::class);
$libelle = $securedEntityAnnotation->getName();
if (is_array($typesUtilisateurs) && count($typesUtilisateurs) > 0) {
foreach ($typesUtilisateurs as $typesUtilisateur) {
if ($this->droitRepository->findOneBy(['typesUtilisateur' => $typesUtilisateur, 'entite' => $class]) === null) {
$pos++;
$droit = new Droit();
$droit->setLibelle($libelle);
$droit->setTypesUtilisateur($typesUtilisateur);
$droit->setParent($RightTitle[$namespace][$typesUtilisateur->getId()]);
$droit->setEntite($class);
$droit->setVoir(true);
$droit->setAjouter(true);
$droit->setModifier(true);
$droit->setSupprimer(true);
$droit->setExporter(true);
$droit->setStatut(true);
$droit->setPosition($pos);
$this->entityManager->persist($droit);
}
}
}
}
$this->entityManager->flush();
$io->success('Rights added on Missing @SecuredEntity.');
return Command::SUCCESS;
}
}