|
@@ -0,0 +1,106 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace IPv4Bundle\EventListener;
|
|
|
+
|
|
|
+use Doctrine\Common\EventSubscriber;
|
|
|
+use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
+use HostBundle\Entity\Host;
|
|
|
+use IPv4Bundle\Entity\Pool;
|
|
|
+use KeaBundle\Services\LeaseService;
|
|
|
+
|
|
|
+class PoolIPRangeSubscriber implements EventSubscriber
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ContainerInterface
|
|
|
+ */
|
|
|
+ private $serviceContainer;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ContainerInterface $serviceContainer
|
|
|
+ */
|
|
|
+ public function __construct($serviceContainer)
|
|
|
+ {
|
|
|
+ $this->serviceContainer = $serviceContainer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getSubscribedEvents()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ 'postUpdate',
|
|
|
+ 'preRemove',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function postUpdate(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function preRemove(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ * @param boolean $remove
|
|
|
+ */
|
|
|
+ public function execute(LifecycleEventArgs $args, $remove = false)
|
|
|
+ {
|
|
|
+ $flashbag = $this->serviceContainer->get('session')->getFlashBag();
|
|
|
+ $translator = $this->serviceContainer->get('translator');
|
|
|
+ try {
|
|
|
+ // checking if the entity is a Host and the status has changed
|
|
|
+ $em = $args->getEntityManager();
|
|
|
+ $uow = $em->getUnitOfWork();
|
|
|
+ $uow->computeChangeSets();
|
|
|
+ $entity = $args->getEntity();
|
|
|
+ $changeset = $uow->getEntityChangeSet($entity);
|
|
|
+ if ($entity instanceof Pool && $entity->getIsStatic() == true) {
|
|
|
+ $hosts = [];
|
|
|
+ if ($remove == true) {
|
|
|
+ $hosts = $em->getRepository(Host::class)->findAllBetweenIp($entity->getFirstIp(), $entity->getLastIp());
|
|
|
+ } else {
|
|
|
+ $firstIp = $firstIpNew = $entity->getFirstIp();
|
|
|
+ if (isset($changeset['firstIp'])) {
|
|
|
+ $firstIp = $changeset['firstIp'][0];
|
|
|
+ $firstIpNew = $changeset['firstIp'][1];
|
|
|
+ }
|
|
|
+
|
|
|
+ $lastIp = $lastIpNew = $entity->getLastIp();
|
|
|
+ if (isset($changeset['lastIp'])) {
|
|
|
+ $lastIp = $changeset['lastIp'][0];
|
|
|
+ $lastIpNew = $changeset['lastIp'][1];
|
|
|
+ }
|
|
|
+
|
|
|
+ $hosts = $em->getRepository(Host::class)->findAllBetweenIps($firstIp, $lastIp, $firstIpNew, $lastIpNew);
|
|
|
+ }
|
|
|
+
|
|
|
+ // setting fixed address to null to all hosts out of range of the pool
|
|
|
+ foreach ($hosts as $host) {
|
|
|
+ $host->setFixedAddress(null);
|
|
|
+ $host->setIpv4Address(null);
|
|
|
+ $host->setFixedIP(false);
|
|
|
+
|
|
|
+ $em->flush($host);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (\Exception $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|