68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use App\Entity\UserGroup as Entity;
|
|
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
|
use Doctrine\ORM\Events;
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
|
|
|
use App\Service\LdapService;
|
|
|
|
class UserGroupSubscriber implements EventSubscriberInterface
|
|
{
|
|
private $em;
|
|
private $entity;
|
|
private $ldap;
|
|
|
|
public function __construct(EntityManagerInterface $em,LdapService $ldap)
|
|
{
|
|
$this->em = $em;
|
|
$this->ldap = $ldap;
|
|
}
|
|
|
|
public function getSubscribedEvents(): array
|
|
{
|
|
return [
|
|
Events::postPersist,
|
|
Events::preRemove,
|
|
];
|
|
}
|
|
|
|
public function postPersist(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) return;
|
|
|
|
|
|
// Synchronisation nine2ldap
|
|
$this->nine2ldap();
|
|
}
|
|
|
|
public function preRemove(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) return;
|
|
|
|
// Synchronisation nine2ldap
|
|
$this->nine2ldapremove();
|
|
}
|
|
|
|
private function nine2ldap() {
|
|
if($this->ldap->isNine2Ldap()) {
|
|
// On s'assure que la structure organisationnelle est présente
|
|
$this->ldap->addOrganisations();
|
|
|
|
// Ajout / Modification dans annuaire
|
|
$this->ldap->addUserGroup($this->entity);
|
|
}
|
|
}
|
|
|
|
private function nine2ldapremove() {
|
|
if($this->ldap->isNine2Ldap()) {
|
|
$this->ldap->delUserGroup($this->entity);
|
|
}
|
|
}
|
|
}
|