nineskeletor/src/EventListener/Niveau01Subscriber.php

129 lines
3.9 KiB
PHP
Raw Normal View History

2022-07-21 16:15:47 +02:00
<?php
namespace App\EventListener;
use App\Entity\Niveau01 as Entity;
use App\Service\LdapService;
2022-07-21 16:15:47 +02:00
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
2022-07-21 16:15:47 +02:00
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
class Niveau01Subscriber implements EventSubscriberInterface
{
private $em;
private $entity;
private $ldap;
public function __construct(EntityManagerInterface $em, LdapService $ldap)
2022-07-21 16:15:47 +02:00
{
$this->em = $em;
$this->ldap = $ldap;
}
public function getSubscribedEvents(): array
{
return [
Events::postPersist,
Events::preUpdate,
Events::postUpdate,
Events::preRemove,
Events::postRemove,
];
}
public function postPersist(LifecycleEventArgs $args): void
{
$this->entity = $args->getObject();
if (!$this->entity instanceof Entity) {
return;
}
2022-07-21 16:15:47 +02:00
// Synchronisation nine2ldap
$this->nine2ldap();
2022-07-21 16:15:47 +02:00
}
public function preUpdate(LifecycleEventArgs $args): void
{
$this->entity = $args->getObject();
if (!$this->entity instanceof Entity) {
return;
}
2022-07-21 16:15:47 +02:00
}
public function postUpdate(LifecycleEventArgs $args): void
{
$this->entity = $args->getObject();
if (!$this->entity instanceof Entity) {
return;
}
2022-07-21 16:15:47 +02:00
// Synchronisation nine2ldap
$this->nine2ldap();
2022-07-21 16:15:47 +02:00
}
public function preRemove(LifecycleEventArgs $args): void
{
$this->entity = $args->getObject();
if (!$this->entity instanceof Entity) {
return;
}
2022-07-21 16:15:47 +02:00
// Impossible de supprimer si présence de niveau02 rattaché
if (!$this->entity->getNiveau02s()->isEmpty()) {
throw new \Exception('Impossible de supprimer cet enregistrement. Il est lié à des niveaux de rang 02');
}
2022-07-21 16:15:47 +02:00
// Impossible de supprimer si présence de registration rattaché
if (!$this->entity->getRegistrations()->isEmpty()) {
throw new \Exception('Impossible de supprimer cet enregistrement. Il est lié à des inscriptions');
}
2022-07-21 16:15:47 +02:00
// Impossible de supprimer si présence de user rattaché
if (!$this->entity->getUsers()->isEmpty()) {
throw new \Exception('Impossible de supprimer cet enregistrement. Il est lié à des utilisateurs');
}
2022-07-21 16:15:47 +02:00
// Synchronisation nine2ldap
$this->nine2ldapremove();
2022-07-21 16:15:47 +02:00
}
public function postRemove(LifecycleEventArgs $args): void
{
$this->entity = $args->getObject();
if (!$this->entity instanceof Entity) {
return;
}
2022-07-21 16:15:47 +02:00
}
private function nine2ldap()
{
if ($this->ldap->isNine2Ldap()) {
2022-07-21 16:15:47 +02:00
// On s'assure que la structure organisationnelle est présente
$this->ldap->addOrganisations();
2022-07-21 16:15:47 +02:00
// Ajout / Modification dans annuaire
$filter = 'gidnumber='.$this->entity->getId();
$attributes = $this->ldap->listAttributesNiveau01();
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('baseniveau01'));
if (empty($ldapentrys)) {
2022-07-21 16:15:47 +02:00
$this->ldap->addNiveau01($this->entity);
} elseif ($this->ldap->ismodifyNiveau01($this->entity, $ldapentrys[0])) {
$this->ldap->modifyNiveau01($this->entity, $ldapentrys[0]['cn']);
2022-07-21 16:15:47 +02:00
}
}
}
private function nine2ldapremove()
{
if ($this->ldap->isNine2Ldap()) {
$filter = 'gidnumber='.$this->entity->getId();
$attributes = $this->ldap->listAttributesNiveau01();
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('baseniveau01'));
if (!empty($ldapentrys)) {
2022-07-21 16:15:47 +02:00
$this->ldap->deleteNiveau01($this->entity);
}
}
}
}