153 lines
4.6 KiB
PHP
Executable File
153 lines
4.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use App\Entity\Group 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 GroupSubscriber 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();
|
|
|
|
// On s'assure que le propriétaire est bien membre du groupe avec le role manager
|
|
$this->ctrlOwner();
|
|
}
|
|
|
|
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();
|
|
|
|
// On s'assure que le propriétaire est bien membre du groupe avec le role manager
|
|
$this->ctrlOwner();
|
|
}
|
|
|
|
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 group dans annuaire
|
|
$filter = 'gidnumber='.$this->entity->getId();
|
|
$attributes = $this->ldap->listAttributesGroup();
|
|
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('basegroup'));
|
|
if (empty($ldapentrys)) {
|
|
$this->ldap->addGroup($this->entity);
|
|
} elseif ($this->ldap->ismodifyGroup($this->entity, $ldapentrys[0])) {
|
|
$this->ldap->modifyGroup($this->entity, $ldapentrys[0]['cn']);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function nine2ldapremove()
|
|
{
|
|
if ($this->ldap->isNine2Ldap()) {
|
|
$filter = 'gidnumber='.$this->entity->getId();
|
|
$attributes = $this->ldap->listAttributesGroup();
|
|
$ldapentrys = $this->ldap->search($filter, $attributes, $this->ldap->getParameter('basegroup'));
|
|
if (!empty($ldapentrys)) {
|
|
$this->ldap->deleteGroup($this->entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function ctrlOwner()
|
|
{
|
|
$group = $this->entity;
|
|
|
|
// Le propriétaire passe manager
|
|
$usergroups = $this->em->getRepository("App\Entity\UserGroup")->findBy(['group' => $group, 'rolegroup' => '100']);
|
|
foreach ($usergroups as $usergroup) {
|
|
if ($usergroup->getUser() != $group->getOwner()) {
|
|
$usergroup->setRolegroup(90);
|
|
$this->em->flush();
|
|
}
|
|
}
|
|
|
|
// Le propriétaire prend son role dans le groupe
|
|
if ($group->getOwner()) {
|
|
$usergroup = $this->em->getRepository("App\Entity\UserGroup")->findOneBy(['group' => $group, 'user' => $group->getOwner()]);
|
|
if (!$usergroup) {
|
|
$usergroup = new UserGroup();
|
|
$usergroup->setUser($group->getOwner());
|
|
$usergroup->setGroup($group);
|
|
$usergroup->setApikey(Uuid::uuid4());
|
|
$usergroup->setRolegroup(100);
|
|
$this->em->persist($usergroup);
|
|
$this->em->flush();
|
|
} elseif (100 != $usergroup->getRolegroup()) {
|
|
$usergroup->setRolegroup(100);
|
|
$this->em->flush();
|
|
}
|
|
}
|
|
}
|
|
}
|