|
@@ -0,0 +1,122 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+namespace RadiusBundle\EventListener;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+use Doctrine\Common\EventSubscriber;
|
|
|
|
+use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
|
+use Doctrine\ORM\Events;
|
|
|
|
+use RadiusBundle\Entity\NAS;
|
|
|
|
+
|
|
|
|
+class NASSubscriber implements EventSubscriber
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ public function getSubscribedEvents()
|
|
|
|
+ {
|
|
|
|
+ return [
|
|
|
|
+ Events::postPersist,
|
|
|
|
+ Events::postUpdate,
|
|
|
|
+ Events::postRemove,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function postUpdate(LifecycleEventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ $this->insertOrUpdate($args);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function postPersist(LifecycleEventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ $this->insertOrUpdate($args);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function postRemove(LifecycleEventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ $this->remove($args);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Check if host ip of a NAS changes
|
|
|
|
+ * and try to delete it from freeradius DB
|
|
|
|
+ *
|
|
|
|
+ * @param LifecycleEventArgs $args
|
|
|
|
+ */
|
|
|
|
+ public function insertOrUpdate(LifecycleEventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ $this->em = $args->getEntityManager();
|
|
|
|
+ $entity = $args->getEntity();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // only act on some "NAS" entity
|
|
|
|
+ if (!$entity instanceof NAS) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $meta = $this->em->getClassMetadata(get_class($entity));
|
|
|
|
+ $uow = $this->em->getUnitOfWork();
|
|
|
|
+ $uow->recomputeSingleEntityChangeSet($meta, $entity);
|
|
|
|
+ $changeset = $uow->getEntityChangeSet($entity);
|
|
|
|
+ if (isset($changeset['host']) && $changeset['host'][0] != $changeset['host'][1]) {
|
|
|
|
+ $this->removeFreeRadius($entity, $changeset['host'][0]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $this->addOrUpdateFreeRadius($entity);
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function remove(LifecycleEventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ $this->em = $args->getEntityManager();
|
|
|
|
+ $entity = $args->getEntity();
|
|
|
|
+
|
|
|
|
+ // only act on some "NAS" entity
|
|
|
|
+ if (!$entity instanceof NAS) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $this->removeFreeRadius($entity);
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function addOrUpdateFreeRadius(NAS $entity){
|
|
|
|
+ $radiusJSON = [
|
|
|
|
+ 'acct_enabled' => $entity->isAcctEnabled(),
|
|
|
|
+ 'community' => $entity->getSnmpComunity(),
|
|
|
|
+ 'description' => 'NAS-' . $entity->getId(),
|
|
|
|
+ 'nasname' => $entity->getHost(),
|
|
|
|
+ 'ports' => null,
|
|
|
|
+ 'secret' => $entity->getRadiusPassword(),
|
|
|
|
+ 'server' => 'radius',
|
|
|
|
+ 'shortname' => $entity->getHost(),
|
|
|
|
+ '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" addRadiusClient ' . $file);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function removeFreeRadius(NAS $entity, $host){
|
|
|
|
+ $radiusJSON = [
|
|
|
|
+ 'acct_enabled' => $entity->isAcctEnabled(),
|
|
|
|
+ 'community' => $entity->getSnmpComunity(),
|
|
|
|
+ 'description' => 'NAS-' . $entity->getId(),
|
|
|
|
+ 'nasname' => $host != null ? $host : $entity->getHost(),
|
|
|
|
+ 'ports' => null,
|
|
|
|
+ 'secret' => $entity->getRadiusPassword(),
|
|
|
|
+ 'server' => 'radius',
|
|
|
|
+ 'shortname' => $entity->getHost(),
|
|
|
|
+ 'type' => 'Mikrotik',
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ $file = '/tmp/radius-client.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);
|
|
|
|
+ }
|
|
|
|
+}
|