|
@@ -0,0 +1,50 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace RadiusBundle\EventListener;
|
|
|
+
|
|
|
+use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
+use RadiusBundle\Entity\NAS;
|
|
|
+
|
|
|
+class NASPreUpdateListener
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check if host ip of a NAS changes
|
|
|
+ * and try to delete it from freeradius DB
|
|
|
+ *
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
+ */
|
|
|
+ public function preUpdate(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $this->em = $args->getEntityManager();
|
|
|
+ $entity = $args->getEntity();
|
|
|
+ if ($entity instanceof NAS) {
|
|
|
+ $meta = $this->em->getClassMetadata(get_class($entity));
|
|
|
+ $uow = $this->em->getUnitOfWork();
|
|
|
+ $uow->recomputeSingleEntityChangeSet($meta, $entity);
|
|
|
+ $changeset = $uow->getEntityChangeSet($entity);
|
|
|
+
|
|
|
+ if (isset($changeset['host'])) {
|
|
|
+ $radiusJSON = [
|
|
|
+ 'acct_enabled' => $entity->isAcctEnabled(),
|
|
|
+ 'community' => $entity->getSnmpComunity(),
|
|
|
+ 'description' => 'NAS-' . $entity->getId(),
|
|
|
+ 'nasname' => $changeset['host'][0],
|
|
|
+ 'ports' => null,
|
|
|
+ 'secret' => $entity->getRadiusPassword(),
|
|
|
+ 'server' => 'radius',
|
|
|
+ 'shortname' => $changeset['host'][0],
|
|
|
+ 'type' => 'Mikrotik',
|
|
|
+ ];
|
|
|
+ $file = '/tmp/radius-client-' . $entity->getId() . '.json';
|
|
|
+ file_put_contents($file, json_encode($radiusJSON));
|
|
|
+ shell_exec('/opt/json-wsdl/console wsdl:op "http://freeradius/radius.php?class=AccessServiceManager&wsdl" deleteRadiusClient ' . $file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (\Exception $ex) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|