From b6e6fc59e5a79a39ed029b0808a37df50870251c Mon Sep 17 00:00:00 2001 From: cajus Date: Mon, 6 Aug 2007 15:22:24 +0000 Subject: [PATCH] Added group and group renaming support in dhcp cache. git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.5@6972 594d385d-05f5-0310-b6e9-bd551577e9d8 --- plugins/admin/systems/class_dhcpGroup.inc | 96 ++++++++--------------- plugins/admin/systems/class_dhcpHost.inc | 6 -- plugins/admin/systems/class_servDHCP.inc | 29 ++++++- plugins/admin/systems/dhcp_group.tpl | 2 +- 4 files changed, 61 insertions(+), 72 deletions(-) diff --git a/plugins/admin/systems/class_dhcpGroup.inc b/plugins/admin/systems/class_dhcpGroup.inc index 8560650cd..bce7875c8 100644 --- a/plugins/admin/systems/class_dhcpGroup.inc +++ b/plugins/admin/systems/class_dhcpGroup.inc @@ -37,25 +37,23 @@ class dhcpGroup extends plugin /* Load statements / options */ if (is_array($attrs)){ $this->dn= $attrs['dn']; + $this->cn= $attrs['cn'][0]; $this->new= FALSE; - /* Load options */ if (isset($attrs['dhcpOption'])){ - for($i= 0; $i<$attrs['dhcpOption']['count']; $i++){ - $tmp= $attrs['dhcpOption'][$i]; - $idx= preg_replace('/\s.+$/', '', $tmp); - $value= preg_replace('/^[^\s]+\s/', '', $tmp); + foreach ($attrs['dhcpOption'] as $opt){ + $idx= preg_replace('/\s.+$/', '', $opt); + $value= preg_replace('/^[^\s]+\s/', '', $opt); $this->options[$idx]= $value; } } /* Load statements */ if (isset($attrs['dhcpStatements'])){ - for($i= 0; $i<$attrs['dhcpStatements']['count']; $i++){ - $tmp= $attrs['dhcpStatements'][$i]; - $idx= preg_replace('/\s.+$/', '', $tmp); - $value= preg_replace('/^[^\s]+\s/', '', $tmp); + foreach ($attrs['dhcpStatements'] as $opt){ + $idx= preg_replace('/\s.+$/', '', $opt); + $value= preg_replace('/^[^\s]+\s/', '', $opt); $this->statements[$idx]= $value; } } @@ -84,10 +82,6 @@ class dhcpGroup extends plugin $smarty= get_smarty(); $smarty->assign("cn", $this->cn); - if ($this->dn != "new"){ - $smarty->assign("mode", "readonly"); - } - /* Show main page */ $display= $smarty->fetch (get_template_path('dhcp_group.tpl', TRUE)).$this->network->execute(); @@ -122,6 +116,11 @@ class dhcpGroup extends plugin /* Save data to object */ function save_object() { + /* Save cn */ + if (isset($_POST['cn'])){ + $this->cn= validate($_POST['cn']); + } + /* Save sub-objects */ $this->network->save_object(); $this->advanced->save_object(); @@ -135,7 +134,7 @@ class dhcpGroup extends plugin /* Check values */ - function check() + function check($cache) { $message= array(); @@ -143,23 +142,8 @@ class dhcpGroup extends plugin if ($this->cn == ""){ $message[]= _("Required field 'Name' is not filled."); } - - /* cn already used? */ - echo "TODO: no ldap check"; - if ($this->dn != "new"){ - $ldap= $this->config->get_ldap_link(); - $ldap->cd($this->config->current['BASE']); - $ldap->search("(&(objectClass=dhcpGroup)(cn=".$this->cn."))"); - if ($ldap->count() >= 1){ - while ($attrs= $ldap->fetch()){ - if ($ldap->getDN() != $this->dn){ - $message[]= _("The name for this group section is already used!"); - break; - } - - } - } - $ldap->fetch(); + if (!preg_match('/^[a-z0-9_-]*$/i', $this->cn)){ + $message[]= _("Field 'Name' contains illegal characters."); } return $message; @@ -169,57 +153,45 @@ class dhcpGroup extends plugin /* Save to LDAP */ function save() { - echo "TODO: save"; - return; - - /* Get ldap mode */ - if ($this->dn == "new"){ - $mode= "add"; - } else { - $mode= "modify"; + /* Merge arrays for network and advanced view */ + foreach (array("options", "statements") as $type){ + $tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type); + $this->$type= $tmp; } - /* Generate new dn */ - if ($this->parent->parent != NULL && $this->dn == "new"){ - $this->dn= "cn=".$this->cn.",".$this->parent->parent; + /* Add cn if we're new */ + if ($this->new){ + $this->dn= "cn=".$this->cn.",".$this->dn; + } else { + $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn); } - /* Assemble new entry - options */ + $this->attrs['dhcpOption']= array(); if (isset ($this->options) && count ($this->options)){ - $this->attrs['dhcpOption']= array(); foreach ($this->options as $key => $val){ $this->attrs['dhcpOption'][]= "$key $val"; } - } else { - if ($mode == "modify"){ - $this->attrs['dhcpOption']= array(); - } } /* Assemble new entry - statements */ + $this->attrs['dhcpStatements']= array(); if (isset ($this->statements) && count ($this->statements)){ - $this->attrs['dhcpStatements']= array(); foreach ($this->statements as $key => $val){ $this->attrs['dhcpStatements'][]= "$key $val"; } - } else { - if ($mode == "modify"){ - $this->attrs['dhcpStatements']= array(); - } } - /* Do LDAP action */ - $ldap= $this->config->get_ldap_link(); - if ($mode == "add"){ - $ldap->cd($this->config->current['BASE']); - $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn)); - } - $ldap->cd($this->dn); - $ldap->$mode($this->attrs); - show_ldap_error($ldap->get_error()); + /* Move dn to the result */ + $this->attrs['dn']= $this->dn; + $this->attrs['cn']= array($this->cn); + $this->attrs['objectClass']= array('top', 'dhcpGroup', 'dhcpOptions'); + $this->attrs['MODIFIED']= TRUE; + + return ($this->attrs); } + } ?> diff --git a/plugins/admin/systems/class_dhcpHost.inc b/plugins/admin/systems/class_dhcpHost.inc index 499e417fc..a6d10b73e 100644 --- a/plugins/admin/systems/class_dhcpHost.inc +++ b/plugins/admin/systems/class_dhcpHost.inc @@ -53,9 +53,6 @@ class dhcpHost extends plugin /* Load options */ if (isset($attrs['dhcpOption'])){ foreach ($attrs['dhcpOption'] as $opt){ - if ($opt == "count"){ - continue; - } $idx= preg_replace('/\s.+$/', '', $opt); $value= preg_replace('/^[^\s]+\s/', '', $opt); $this->options[$idx]= $value; @@ -65,9 +62,6 @@ class dhcpHost extends plugin /* Load statements */ if (isset($attrs['dhcpStatements'])){ foreach ($attrs['dhcpStatements'] as $opt){ - if ($opt == "count"){ - continue; - } $idx= preg_replace('/\s.+$/', '', $opt); $value= preg_replace('/^[^\s]+\s/', '', $opt); $this->statements[$idx]= $value; diff --git a/plugins/admin/systems/class_servDHCP.inc b/plugins/admin/systems/class_servDHCP.inc index 752c5738e..37988c3a3 100644 --- a/plugins/admin/systems/class_servDHCP.inc +++ b/plugins/admin/systems/class_servDHCP.inc @@ -85,9 +85,32 @@ class servdhcp extends plugin if ($dn != $data['dn']){ /* Old object, new name */ $this->dhcpObjectCache[$dn]= array(); - unset($this->dhcpSections[$dn]); $this->dhcpObjectCache[$data['dn']]= $data; - $this->dhcpSections[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'"; + + /* If we renamed a section, we've to rename a couple of objects, too */ + foreach ($this->dhcpObjectCache as $key => $dsc){ + if (preg_match("/,$dn$/", $key)){ + $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key); + $dsc['MODIFIED']= TRUE; + $this->dhcpObjectCache[$new_dn]= $dsc; + unset($this->dhcpObjectCache[$key]); + } + } + $newsects= array(); + foreach ($this->dhcpSections as $key => $dsc){ + if ($key == $dn){ + $newsects[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'"; + continue; + } + if (preg_match("/,$dn$/", $key)){ + $new_dn= preg_replace("/,$dn$/", ",".$data['dn'], $key); + $newsects[$new_dn]= $dsc; + } else { + $newsects[$key]= $dsc; + } + } + $this->dhcpSections= $newsects; + } else { /* Old object, old name */ $this->dhcpObjectCache[$data['dn']]= $data; @@ -244,7 +267,7 @@ class servdhcp extends plugin if (count($data) == 0){ /* Check if exists, then remove... */ if($ldap->cat($dn)){ - $ldap->rmdir($dn); + $ldap->rmdir_recursive($dn); show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!")); } continue; diff --git a/plugins/admin/systems/dhcp_group.tpl b/plugins/admin/systems/dhcp_group.tpl index d3725178a..3d6a4d15d 100644 --- a/plugins/admin/systems/dhcp_group.tpl +++ b/plugins/admin/systems/dhcp_group.tpl @@ -7,7 +7,7 @@ + title='{t}Name of group{/t}'> -- 2.30.2