Espinoza Guillermo 6 anni fa
parent
commit
056344f44d

+ 11 - 3
src/HostBundle/Admin/HostAdmin.php

@@ -150,9 +150,7 @@ class HostAdmin extends BaseAdmin
      */
     function prePersist($object)
     {
-        $object->setOptions(json_encode($object->getDHCPOption()));
-    
-        return parent::preUpdate($object);
+        return $this->updateDHCPOptions($object);
     }
     
     /**
@@ -161,6 +159,16 @@ class HostAdmin extends BaseAdmin
      * @return mixed
      */
     function preUpdate($object)
+    {
+        return $this->updateDHCPOptions($object);
+    }
+    
+    /**
+     * @param Host $object
+     *
+     * @return Response
+     */
+    public function updateDHCPOptions($object)
     {
         if ($object->getFixedAddress() && $object->getFixedIP() == false) {
             $object->setFixedIP(true);

+ 0 - 37
src/HostBundle/Controller/HostController.php

@@ -1,37 +0,0 @@
-<?php
-
-namespace HostBundle\Controller;
-
-use HostBundle\Entity\HostType;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\JsonResponse;
-use FOS\RestBundle\Controller\Annotations\RouteResource;
-
-/**
- * @RouteResource("Host")
- */
-class HostController extends Controller
-{
-
-    /**
-     * @param Request $request
-     *
-     * @return JsonResponse
-     *
-     * @Route("/host/ipv4/ip_range", name="ajax_host_ipv4_range")
-     */
-    public function ajaxHostIPV4RangeAction(Request $request)
-    {
-        $em = $this->getDoctrine();
-        $hostType = $em->getRepository(HostType::class)->find($request->get('id'));
-        $ips = [];
-        if ($hostType) {
-            $ips = $this->get('dhcp.host_service')->getFreeFixedIP($hostType);
-        }
-
-        return new JsonResponse(compact('ips'));
-    }
-
-}

+ 27 - 0
src/HostBundle/Controller/REST/HostRESTController.php

@@ -3,8 +3,10 @@
 namespace HostBundle\Controller\REST;
 
 use HostBundle\Form\HostType;
+use HostBundle\Entity\HostType as HostTypeEntity;
 use FOS\RestBundle\Controller\Annotations\RouteResource;
 use WebserviceBundle\Controller\RESTController;
+use Symfony\Component\HttpFoundation\Request;
 
 /**
  * Host controller.
@@ -28,5 +30,30 @@ class HostRESTController extends RESTController
     {
         return get_class(new HostType());
     }
+    
+    /**
+     * @param Request $request
+     *
+     * @return array
+     */
+    public function postIpv4RangeAction(Request $request)
+    {
+        $em = $this->getDoctrine();
+        $hostTypeRepository = $em->getRepository(HostTypeEntity::class);
+        if ($request->get('id')) {
+            $hostType = $hostTypeRepository->find($request->get('id'));
+        } elseif ($request->get('shortname')) {
+            $hostType = $hostTypeRepository->findOneByShortname($request->get('shortname'));
+        } else {
+            return [];
+        }
+        
+        $ips = [];
+        if ($hostType) {
+            $ips = $this->get('dhcp.host_service')->getFreeFixedIP($hostType);
+        }
+        
+        return compact('ips');
+    }
 
 }

+ 24 - 1
src/HostBundle/Entity/Host.php

@@ -8,6 +8,7 @@ use HostBundle\Traits\DHCPOptionTrait;
 use HostBundle\Utils\HostStatus;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Component\Validator\Context\ExecutionContextInterface;
 use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
 use WorkflowBundle\Entity\Traits\WorkflowTrait;
 
@@ -16,7 +17,8 @@ use WorkflowBundle\Entity\Traits\WorkflowTrait;
  * @ORM\HasLifecycleCallbacks
  *
  * @UniqueEntity("mac")
- * @UniqueEntity("options.fixed_address")
+ *
+ * @Assert\Callback("validateFixedAddress")
  */
 class Host implements WorkflowInterface
 {
@@ -230,5 +232,26 @@ class Host implements WorkflowInterface
         
         return $this;
     }
+    
+    /**
+     * @param ExecutionContextInterface $context
+     */
+    public function validateFixedAddress(ExecutionContextInterface $context)
+    {
+        global $kernel;
+        
+        $em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
+        $hostRepository = $em->getRepository(self::class);
+        $hosts = $hostRepository->findAll();
+        $options = json_decode($this->getOptions(), true);
+        $fixedAddress = isset($options['fixed_address']) ? $options['fixed_address'] : $this->getFixedAddress();
+        $fixedAddress = $fixedAddress ?: '';
+        foreach ($hosts as $host) {
+            if ($host->getId() != $this->id && $fixedAddress === $host->getFixedAddress()) {
+                $context->addViolation('options.fixed_address.not_unique');
+                break;
+            }
+        }
+    }
 
 }

+ 1 - 0
src/HostBundle/Resources/translations/HostBundle.es.yml

@@ -145,3 +145,4 @@ State: Estado
 active: Activo
 suspended: Suspendido
 disabled: Inactivo
+options.fixed_address.not_unique: La IP fija seleccionada ya esta asociada a otro host

+ 1 - 1
src/HostBundle/Resources/views/CRUD/edit.html.twig

@@ -37,7 +37,7 @@ function updateFixedIPs()
     
     if ($hostTypeId) {
         $.ajax({
-            url: '{{ path ('ajax_host_ipv4_range') }}',
+            url: '{{ path ('post_host_ipv4_range') }}',
             type: 'POST',
             data: {
                 id: $hostTypeId,

+ 4 - 4
src/IPv4Bundle/Entity/Pool.php

@@ -232,8 +232,8 @@ class Pool implements TenancyIdTraitInterface, WorkflowInterface
     }
 
     /**
-    * @param ExecutionContextInterface $context
-    */
+     * @param ExecutionContextInterface $context
+     */
     public function validateIPs(ExecutionContextInterface $context)
     {
         if (ip2long($this->lastIp) <= ip2long($this->firstIp)) {
@@ -248,8 +248,8 @@ class Pool implements TenancyIdTraitInterface, WorkflowInterface
     }
 
     /**
-    * @return string
-    */
+     * @return string
+     */
     public function getGroupName()
     {
         if ($this->getSubNet() && $this->getSubNet()->getNetGroup()) {