141 lines
4.1 KiB
PHP
141 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use App\Entity\User as Entity;
|
|
use App\Entity\UserGroup;
|
|
use App\Service\LdapService;
|
|
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\Events;
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class UserSubscriber 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::preUpdate,
|
|
Events::postUpdate,
|
|
Events::preRemove,
|
|
Events::postRemove,
|
|
];
|
|
}
|
|
|
|
public function postPersist(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
|
|
// Synchronisation nine2ldap
|
|
$this->nine2ldap();
|
|
|
|
// Recherche du group tout le monde
|
|
$group = $this->em->getRepository("App\Entity\Group")->find(-1);
|
|
$usergroup = new UserGroup();
|
|
$usergroup->setUser($this->entity);
|
|
$usergroup->setGroup($group);
|
|
$usergroup->setApikey(Uuid::uuid4());
|
|
$usergroup->setRolegroup(0);
|
|
|
|
$this->em->persist($usergroup);
|
|
$this->em->flush();
|
|
}
|
|
|
|
public function preUpdate(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function postUpdate(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
|
|
// Synchronisation nine2ldap
|
|
$this->nine2ldap();
|
|
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
public function preRemove(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
|
|
// Synchronisation nine2ldap
|
|
$this->nine2ldapremove();
|
|
}
|
|
|
|
public function postRemove(LifecycleEventArgs $args): void
|
|
{
|
|
$this->entity = $args->getObject();
|
|
if (!$this->entity instanceof Entity) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function nine2ldap()
|
|
{
|
|
if ($this->ldap->isNine2Ldap()) {
|
|
// On s'assure que la structure organisationnelle est présente
|
|
$this->ldap->addOrganisations();
|
|
|
|
// Ajout / Modification dans annuaire
|
|
$filter = str_replace('*', $this->entity->getUsername(), $this->ldap->getParameter('filteruser'));
|
|
$attributes = $this->ldap->listAttributesUser();
|
|
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('baseuser'));
|
|
if (empty($ldapentrys)) {
|
|
if ($this->entity->isIsactive()) {
|
|
$this->ldap->addUser($this->entity);
|
|
}
|
|
} elseif (!$this->entity->isIsactive()) {
|
|
$this->nine2ldapremove();
|
|
} elseif ($this->ldap->ismodifyUser($this->entity, $ldapentrys[0])) {
|
|
$this->ldap->modifyUser($this->entity, $ldapentrys[0]['cn']);
|
|
}
|
|
|
|
// Mise à jour des niveaux du user
|
|
$this->ldap->updateNiveauUser($this->entity);
|
|
}
|
|
}
|
|
|
|
private function nine2ldapremove()
|
|
{
|
|
if ($this->ldap->isNine2Ldap()) {
|
|
$filter = str_replace('*', $this->entity->getUsername(), $this->ldap->getParameter('filteruser'));
|
|
$attributes = $this->ldap->listAttributesUser();
|
|
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('baseuser'));
|
|
if (!empty($ldapentrys)) {
|
|
$this->ldap->deleteUser($this->entity);
|
|
}
|
|
|
|
// Mise à jour des niveaux du user en forçant le détachement
|
|
$this->ldap->updateNiveauUser($this->entity, true);
|
|
}
|
|
}
|
|
}
|