nineskeletor/src/EventListener/UserGroupSubscriber.php

72 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\EventListener;
use App\Entity\UserGroup as Entity;
use App\Service\LdapService;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
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);
}
}
}