소스 검색

Merge branch 'hotfix-nas-insert' into 'master'

Hotfix nas insert

See merge request interlink-sa/flowdat3/modules/radius!19
Jean Sumara Leopoldo 5 년 전
부모
커밋
20112b4a0e
3개의 변경된 파일128개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      .gitignore
  2. 122 0
      src/RadiusBundle/EventListener/NASSubscriber.php
  3. 5 0
      src/RadiusBundle/Resources/config/services.yml

+ 1 - 0
.gitignore

@@ -23,3 +23,4 @@ app/Resources/workflows/workflow_list.yml
 /web/workflows_png/*
 !web/workflows_png/.gitkeep
 /app/config/bundles/ik/base-admin-bundle/parameters.yml
+.idea

+ 122 - 0
src/RadiusBundle/EventListener/NASSubscriber.php

@@ -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);
+    }
+}

+ 5 - 0
src/RadiusBundle/Resources/config/services.yml

@@ -30,3 +30,8 @@ services:
             - { name: sonata.admin, manager_type: orm, group: Radius, label: NAS, label_catalogue: RadiusBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
         calls:
             - [setTranslationDomain, [RadiusBundle]]
+
+    radius.nas.subscriber:
+        class: RadiusBundle\EventListener\NASSubscriber
+        tags:
+            - { name: doctrine.event_subscriber, connection: default }