ソースを参照

Merged in FD3-501 (pull request #11)

FD3-501 - agregue opciones básicas dhcp

Approved-by: Guillermo Espinoza <guillermo@interlink.com.ar>
Luciano Andrade 7 年 前
コミット
b3a4da8a10

+ 36 - 0
app/DoctrineMigrations/Version20180316121350.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Migrations\AbstractMigration;
+use Doctrine\DBAL\Schema\Schema;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+class Version20180316121350 extends AbstractMigration
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        // this up() migration is auto-generated, please modify it to your needs
+        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
+
+        $this->addSql('ALTER TABLE host CHANGE options options LONGTEXT DEFAULT NULL');
+        $this->addSql('ALTER TABLE sub_net CHANGE options options LONGTEXT DEFAULT NULL');
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.');
+
+        $this->addSql('ALTER TABLE host CHANGE options options LONGTEXT NOT NULL COLLATE utf8_unicode_ci');
+        $this->addSql('ALTER TABLE sub_net CHANGE options options LONGTEXT NOT NULL COLLATE utf8_unicode_ci');
+    }
+}

+ 13 - 0
app/Resources/translations/messages.es.yml

@@ -6,3 +6,16 @@ Pool: Pool
 SubNet: Subred
 DHCP: DHCP
 DHCPModel: Modelo DHCP
+form:
+    label_filename: Filename
+    label_subnet_mask: Subnet Mask
+    label_time_offset: Time Offset
+    label_routers: Routers
+    label_domain_name_servers: Domain Name Servers
+    label_host_name: Host Name
+    label_domain_name: Domain Name
+    label_broadcast_address: Boradcast Address
+    label_default_lease_time: Default Lease Time
+    label_max_lease_time: Max. Lease Time
+    label_next_server: Next Server
+    label_tftp_server_name: TFTP Server Name

+ 21 - 6
src/IPv4Bundle/Admin/HostAdmin.php

@@ -28,7 +28,6 @@ class HostAdmin extends BaseAdmin
     {
         $listMapper
             ->add('mac')
-            ->add('options')
             ->add('hostType')
             ->add('state')
             ->add('_action', null, array(
@@ -47,10 +46,13 @@ class HostAdmin extends BaseAdmin
     protected function configureFormFields(FormMapper $formMapper)
     {
         $formMapper
-            ->add('mac')
-            ->add('options')
-            ->add('hostType')
-            ->add('state')
+            ->tab('Host')
+            ->with('Host')
+		    ->add('mac')
+		    ->add('hostType')
+		    ->add('state')
+		->end()
+		->end()
         ;
     }
 
@@ -60,10 +62,23 @@ class HostAdmin extends BaseAdmin
     protected function configureShowFields(ShowMapper $showMapper)
     {
         $showMapper
+            ->tab('Host')
+            ->with('Host')
             ->add('mac')
-            ->add('options')
             ->add('hostType')
             ->add('state')
+		->end()
+		->end()
         ;
     }
+
+    function prePersist($object){
+	    $object->setOptions(json_encode($object->getDHCPOption()));
+	    return parent::preUpdate($object);
+    }
+    
+    function preUpdate($object){
+	    $object->setOptions(json_encode($object->getDHCPOption()));
+	    return parent::preUpdate($object);
+    }
 }

+ 19 - 5
src/IPv4Bundle/Admin/SubNetAdmin.php

@@ -48,11 +48,15 @@ class SubNetAdmin extends BaseAdmin
     protected function configureFormFields(FormMapper $formMapper)
     {
         $formMapper
-            ->add('address')
-            ->add('options')
-            ->add('allowedHostType')
-            ->add('netGroup')
-        ;
+            ->tab('SubNet')
+            ->with('')
+                ->add('address')
+                //->add('options')
+                ->add('allowedHostType')
+                ->add('netGroup')
+            ->end()
+            ->end()
+	;
     }
 
     /**
@@ -68,4 +72,14 @@ class SubNetAdmin extends BaseAdmin
             ->add('ipPool')
         ;
     }
+
+    function prePersist($object){
+	    $object->setOptions(json_encode($object->getDHCPOption()));
+	    return parent::preUpdate($object);
+    }
+    
+    function preUpdate($object){
+	    $object->setOptions(json_encode($object->getDHCPOption()));
+	    return parent::preUpdate($object);
+    }
 }

+ 21 - 2
src/IPv4Bundle/Entity/Host.php

@@ -4,14 +4,33 @@ namespace IPv4Bundle\Entity;
 
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+use IPv4Bundle\Traits\DHCPOptionTrait;
 
 /**
  * @ORM\Entity
- *
+ * @ORM\HasLifecycleCallbacks 
  * @UniqueEntity("mac")
  */
 class Host
 {
+    use DHCPOptionTrait;
+
+    /** 
+     *  @ORM\PreUpdate 
+     *  @ORM\PrePersist
+     */
+    public function doDHCPOptionsPrePersist() 
+    {
+	    $this->setOptions(json_encode($this->getDHCPOption()));
+    }
+    
+    /** 
+     * @ORM\PostLoad 
+     */
+    public function doDHCPOptionOnPostLoad()
+    {
+	   $this->setDHCPOption((array)json_decode($this->getOptions()));
+    }
 
     const STATE_ACTIVE = 'active';
     const STATE_SUSPENDED = 'suspended';
@@ -35,7 +54,7 @@ class Host
     /**
      * @var string $options
      *
-     * @ORM\Column(type="text")
+     * @ORM\Column(type="text", nullable=true)
      */
     protected $options;
 

+ 25 - 1
src/IPv4Bundle/Entity/SubNet.php

@@ -9,6 +9,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\Validator\Context\ExecutionContextInterface;
+use IPv4Bundle\Traits\DHCPOptionTrait;
 
 /**
  * @ORM\Entity
@@ -16,12 +17,35 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
  * @UniqueEntity("address")
  *
  * @Assert\Callback("validateAddress")
+ *
+ * @ORM\HasLifecycleCallbacks 
  */
 class SubNet implements TenancyIdTraitInterface
 {
 
     use TenancyIdTrait;
 
+    use DHCPOptionTrait;
+
+    /** 
+     *  @ORM\PreUpdate 
+     *  @ORM\PrePersist
+     */
+    public function doDHCPOptionsPrePersist() 
+    {
+	    $this->setOptions(json_encode($this->getDHCPOption()));
+    }
+    
+    /** 
+     * @ORM\PostLoad 
+     */
+    public function doDHCPOptionOnPostLoad()
+    {
+	   $this->setDHCPOption((array)json_decode($this->getOptions()));
+    }
+
+
+
     /**
      * @ORM\Column(name="id", type="bigint", nullable=false)
      * @ORM\Id
@@ -49,7 +73,7 @@ class SubNet implements TenancyIdTraitInterface
     /**
      * @var string $options
      *
-     * @ORM\Column(type="text")
+     * @ORM\Column(type="text", nullable=true)
      */
     protected $options;
 

+ 83 - 0
src/IPv4Bundle/EventListener/AdminDHCPOption.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace IPv4Bundle\EventListener;
+
+use Sonata\AdminBundle\Event\ConfigureEvent;
+use IPv4Bundle\Traits\DHCPOptionTrait;
+
+class AdminDHCPOption{ 
+    /**
+     * @param ConfigureEvent $event
+     */
+    public function configureFormFields(ConfigureEvent $event)
+    {
+         $mapper = $event->getMapper();
+         $subject = $mapper->getAdmin()->getSubject();
+         if ($subject && in_array(DHCPOptionTrait::class, class_uses($subject))) {
+             if ($mapper->hasOpenTab()) {
+                 $mapper
+                     ->end()
+                     ->end();
+             }
+             $options = array(
+                 'translation_domain' => 'IPv4Bundle'
+             );
+             $mapper
+                 ->tab('DHCP Option', $options)
+                 ->with('DHCP Option', $options)
+               ->add('filename', 		'text', array('required'=> false, ))
+               ->add('subnet_mask',		'text', array('required'=> false, ))
+               ->add('time_offset',		'text', array('required'=> false, ))
+               ->add('routers',			'text', array('required'=> false, ))
+               ->add('domain_name_servers', 	'text', array('required'=> false, ))
+               ->add('host_name', 		'text', array('required'=> false, ))
+               ->add('domain_name', 		'text', array('required'=> false, ))
+               ->add('broadcast_address', 	'text', array('required'=> false, ))
+               ->add('default_lease_time', 	'text', array('required'=> false, ))
+               ->add('max_lease_time', 		'text', array('required'=> false, ))
+               ->add('next_server', 		'text', array('required'=> false, ))
+               ->add('tftp_server_name', 	'text', array('required'=> false, ))
+
+                 ->end()
+                 ->end();
+         }
+
+    }
+
+    /**
+     * @param ConfigureEvent $event
+     */
+    public function configureShowFields(ConfigureEvent $event)
+    {
+    	$mapper = $event->getMapper();
+	$subject = $mapper->getAdmin()->getSubject();
+        if ($subject && in_array(DHCPOptionTrait::class, class_uses($subject))) {
+	   if ($mapper->hasOpenTab()) {
+	       $mapper
+	         ->end()
+	         ->end();
+	   }
+	   $options = array(
+                 'translation_domain' => 'IPv4Bundle'
+	   );
+        
+           $mapper
+                 ->tab('DHCP Option', $options)
+                 ->with('DHCP Option', $options);
+	   if(!$mapper->has('filename')) $mapper->add('filename', 		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('subnet_mask')) $mapper->add('subnet_mask',		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('time_offset')) $mapper->add('time_offset',		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('routers')) $mapper->add('routers',		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('domain_name_servers')) $mapper->add('domain_name_servers', 	'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('host_name')) $mapper->add('host_name', 		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('domain_name')) $mapper->add('domain_name', 		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('broadcast_address')) $mapper->add('broadcast_address', 	'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('default_lease_time')) $mapper->add('default_lease_time', 	'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('max_lease_time')) $mapper->add('max_lease_time', 	'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('next_server')) $mapper->add('next_server', 		'text', array('required'=> false, 'mapped' => false));
+           if(!$mapper->has('tftp_server_name')) $mapper->add('tftp_server_name', 	'text', array('required'=> false, 'mapped' => false));
+
+           $mapper->end()->end();
+ 	}
+    }
+}

+ 7 - 0
src/IPv4Bundle/Resources/config/services.yml

@@ -44,3 +44,10 @@ services:
         calls:
             - [setTranslationDomain, [IPv4Bundle]]
         public: true
+
+    dhcp_option_admin:
+       class: IPv4Bundle\EventListener\AdminDHCPOption
+       tags:
+           - { name: kernel.event_listener, event: sonata.admin.event.configure.form, method: configureFormFields }
+           - { name: kernel.event_listener, event: sonata.admin.event.configure.show, method: configureShowFields }
+

+ 143 - 0
src/IPv4Bundle/Traits/DHCPOptionTrait.php

@@ -0,0 +1,143 @@
+<?php
+
+namespace IPv4Bundle\Traits;
+
+
+trait DHCPOptionTrait{
+	protected $json_dhcp_option_config;
+	function getDHCPOption(){
+		return $this->json_dhcp_option_config;
+	}
+
+	function setDHCPOption($config){
+		$this->json_dhcp_option_config = $config;
+	}
+
+	function setFilename($value){
+		$this->json_dhcp_option_config['filename'] = $value;
+	}
+
+	function getFilename(){
+		return @$this->json_dhcp_option_config['filename'];
+	}
+
+        function setSubnetMask($value){
+        	$this->json_dhcp_option_config['subnet_mask'] = $value;
+	}
+
+	function getSubnetMask(){
+		return @$this->json_dhcp_option_config['subnet_mask'];
+	}
+
+        function setTimeOffset($value){
+		$this->json_dhcp_option_config['time_offset'] = $value;
+	}
+	
+	function getTimeOffset(){
+		return @$this->json_dhcp_option_config['time_offset'];
+	}
+
+	function setRouters($value){
+		$this->json_dhcp_option_config['routers'] = $value;
+	}
+
+	function getRouters(){
+		return @$this->json_dhcp_option_config['routers'];
+	}
+
+	function setDomainNameServers($value){
+		$this->json_dhcp_option_config['domain_name_servers'] = $value;
+	}
+
+	function getDomainNameServers(){
+        	return @$this->json_dhcp_option_config['domain_name_servers'];
+	}
+
+	function setHostName($value){
+		$this->json_dhcp_option_config['host_name'] = $value;
+	}
+
+	function getHostName(){
+		return @$this->json_dhcp_option_config['host_name'];
+	}
+
+        function setDomainName($value){
+		$this->json_dhcp_option_config['domain_name'] = $value;
+	}
+        function getDomainName(){
+		return @$this->json_dhcp_option_config['domain_name'];
+	}
+
+        function setBroadcastAddress($value){
+		$this->json_dhcp_option_config['broadcast_address'] = $value;
+	}
+
+	function getBroadcastAddress(){
+		return @$this->json_dhcp_option_config['broadcast_address'];
+	}
+
+	function setDefaultLeaseTime($value){
+		$this->json_dhcp_option_config['default_lease_time'] = $value;
+	}
+
+	function getDefaultLeaseTime(){
+		return @$this->json_dhcp_option_config['default_lease_time'];
+	}
+
+	function setMaxLeaseTime($value){
+		$this->json_dhcp_option_config['max_lease_time'] = $value;
+	}
+
+	function getMaxLeaseTime(){
+		return @$this->json_dhcp_option_config['max_lease_time'];
+	}
+
+	function setNextServer($value){
+		$this->json_dhcp_option_config['next_server'] = $value;
+	}
+
+	function getNextServer(){
+		return @$this->json_dhcp_option_config['next_server'];
+	}
+
+	function setTftpServerName($value){
+		$this->json_dhcp_option_config['tftp_server'] = $value;
+	}
+
+	function getTftpServerName(){
+		return @$this->json_dhcp_option_config['tftp_server'];
+	}
+
+	function setOption122DhcpServer($value){
+		$this->json_dhcp_option_config['option122.dhcp-server'] = $value;
+	}
+
+	function getOption122DhcpServer(){
+		return @$this->json_dhcp_option_config['option122.dhcp-server'];
+	}
+
+	function setOption122DhcpServerSeconday($value){
+		$this->json_dhcp_option_config['option122.dhcp-server-secundary'] = $value;
+	}
+
+	function getOption122DhcpServerSeconday(){
+		return @$this->json_dhcp_option_config['option122.dhcp-server-secundary'];
+	}
+
+	function getOption122ProvisioningServer($value){
+		$this->json_dhcp_option_config['option122.provisioning-server'] = $value;
+	}
+
+	function setOption122ProvisioningServer(){
+		return @$this->json_dhcp_option_config['option122.provisioning-server'];
+	}
+
+	function setOption122ProvisioningType($value){
+		$this->json_dhcp_option_config['option122.provisioning-type'] = $value;
+	}
+
+	function getOption122ProvisioningType(){
+		return @$this->json_dhcp_option_config['option122.provisioning-type'];
+	}
+
+}