|
@@ -0,0 +1,105 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace IPv4Bundle\EventListener;
|
|
|
+
|
|
|
+use Doctrine\Common\EventSubscriber;
|
|
|
+use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
+use HostBundle\Entity\Host;
|
|
|
+use IPv4Bundle\Entity\SubNet;
|
|
|
+use KeaBundle\Services\LeaseService;
|
|
|
+
|
|
|
+class SubnetStatusOrHostTypeChangeSubscriber implements EventSubscriber
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ContainerInterface
|
|
|
+ */
|
|
|
+ private $serviceContainer;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ContainerInterface $serviceContainer
|
|
|
+ */
|
|
|
+ public function __construct($serviceContainer)
|
|
|
+ {
|
|
|
+ $this->serviceContainer = $serviceContainer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getSubscribedEvents()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ 'preUpdate',
|
|
|
+ 'preRemove',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function preUpdate(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function preRemove(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->execute($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ * @param boolean $remove
|
|
|
+ */
|
|
|
+ public function execute(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $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 SubNet && (isset($changeset['status']) || isset($changeset['allowedHostType']))) {
|
|
|
+ // $leaseService = $this->serviceContainer->get('kea.lease.service');
|
|
|
+ // // invalidating the host lease
|
|
|
+ // $lease4 = $leaseService->invalidate($entity->getMac());
|
|
|
+ // $flashbag->add("success", $translator->trans('host.lease.invalidate.ok', ['%hwaddr%' => $entity->getMac()], 'KeaBundle'));
|
|
|
+
|
|
|
+ // setting fixed address to null to all hosts in the pools static associated to the subnet
|
|
|
+ $ipRange = $this->serviceContainer->get('pool_ipv4_service')->getStaticPoolIPRangeBySubnet($entity);
|
|
|
+ $hosts = [];
|
|
|
+ foreach ($ipRange as $ip) {
|
|
|
+ $host = $em->getRepository('HostBundle:Host')->findOneBy([
|
|
|
+ 'ipv4Address' => $ip,
|
|
|
+ ]);
|
|
|
+ if ($host) {
|
|
|
+ $hosts[] = $host;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($hosts as $host) {
|
|
|
+ if ($host->getFixedAddress() != '') {
|
|
|
+ $host->setFixedAddress(null);
|
|
|
+ $host->setIpv4Address(null);
|
|
|
+ $host->setFixedIP(false);
|
|
|
+
|
|
|
+ $em->persist($host);
|
|
|
+ $em->flush($host);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (\Exception $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ } catch (\Throwable $ex) {
|
|
|
+ $flashbag->add("error", $ex->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|