Code

Added dummy migrator for userRDN
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 12 May 2010 12:50:17 +0000 (12:50 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Wed, 12 May 2010 12:50:17 +0000 (12:50 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18377 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/include/class_configRegistry.inc
gosa-core/include/class_migrate_userRDN.inc
gosa-core/plugins/addons/configViewer/class_configViewer.inc

index fb78771d4361503c9a73ffbdaa2c7b0f68f7f34c..6cf1d32e2436663ed96c61f1049bc08fc18ce479 100644 (file)
@@ -243,7 +243,7 @@ class gosaProperty
                 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
             }else{
                 $class = $this->migrate;
-                $tmp = new $class($this);
+                $tmp = new $class($this->parent->config,$this);
                 if(! $tmp instanceof propertyMigration){ 
                     trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
                 }
@@ -391,7 +391,7 @@ class gosaProperty
 
     static function isRdn($message,$class,$name,$value, $type)
     {
-        $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
+        $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*,$/i", $value);
 
         // Display the reason for failing this check.         
         if($message && ! $match){
@@ -546,7 +546,7 @@ class gosaProperty
 
 interface propertyMigration
 {
-    function __construct($property);
+    function __construct($config,$property);
 }
 
 
index 63815cc218a8a9161e857eb3898837974201bb98..8fc163aac671a73ef9bd95a163bc5e2fc3c4ec05 100644 (file)
 class migrate_userRDN implements propertyMigration
 {
     private $property = NULL;
+    private $config = NULL;
 
-    function __construct($property)
+    private $found = array();
+
+    function __construct($config,$property)
     {
         $this->property = &$property;
+        $this->config = &$config;
     }   
 
     function checkForIssues()
     {
-        return(TRUE);
+        $this->found = array();
+        $ldap= $this->config->get_ldap_link();
+        $ldap->cd($this->config->current['BASE']);
+        $ldap2= $this->config->get_ldap_link();
+        $ldap2->cd($this->config->current['BASE']);
+
+        // If the userRDN wasn't empty, then only search for users inside of the old userRDN.
+        $initialValue = $this->property->getValue();
+        $targetValue = $this->property->getValue(TRUE);
+        $dnMatch = "";
+        if(!empty($initialValue)){
+            list($namingAttrs, $container) = preg_split("/=/",$initialValue,2);
+            $container = trim($container,', ');
+            $dnMatch = "({$namingAttrs}:dn:={$container})";
+        }
+
+        // Search for users
+        $ldap->search("(&(objectClass=gosaAccount)(!(objectClass=sambaSamAccount)){$dnMatch})",array('dn'));
+        $found = FALSE;
+        while($attrs = $ldap->fetch()){
+            $dn = $attrs['dn'];
+            $dnTo = $dn;
+
+            // If there intially was no userDN given then just add the new userRDN to the user dns
+            //  and create the new container objects.
+            if(empty($initialValue)){
+                list($namingAttrs, $container) = preg_split("/=/",$targetValue,2);
+                list($name, $container) = preg_split("/,/",$dn,2);
+
+                // Ensure that we handle a valid gosaDepartment container.
+                while(!isset($this->config->idepartments[$container])){
+                    $container = preg_replace("/^[^,]*+,/","",$container);
+                }
+
+                // Queue new containuer to be created.
+                if(!preg_match("/^".preg_quote($targetValue,'/i')."/", $container)){
+                    $dnTo = $name.",".$targetValue.$container;
+                    if(!$ldap->dn_exists($targetValue.$container)){
+                        $this->found['add'][$targetValue.$container] = array(); 
+                    }
+                    if($dn != $dnTo){
+                        $this->found['move'][$dn] = $dnTo;
+                        $found = TRUE;
+                    }
+                }
+            }
+
+            // If there intially was a userDN given then replace it with the new one.
+            if(!empty($initialValue)){
+
+                list($name, $container) = preg_split("/,/",$dn,2);
+                if(preg_match("/^".preg_quote($initialValue,'/i')."/", $container)){
+                    $containerDN = preg_replace("/^".preg_quote($initialValue,'/')."/",$targetValue,$container);
+                    $dnTo = $name.",".$containerDN;
+                    if(!empty($targetValue) && !$ldap->dn_exists($targetValue.$container)){
+                        $this->found['add'][$containerDN] = array(); 
+                    }
+                    if($dn != $dnTo){
+                        $this->found['move'][$dn] = $dnTo;
+                        $found = TRUE;
+                    }
+                }
+            }    
+        }
+        return($found);
     }
 
     function execute()
     {
-        return("<input type='submit'>");
+        $str = "";
+        if(count($this->found['add'])) {
+            $str.= "<br><h3>"._("Objects that will be added")."</h3>";
+            foreach($this->found['add'] as $dn => $attrs){
+                $str.= $dn."<br>";
+            }
+        }
+        if(count($this->found['move'])) {
+            $str.= "<br><h3>"._("Objects that will be moved")."</h3>";
+            $str.="<pre>";
+            foreach($this->found['move'] as $dn => $dnTo){
+                $str.= sprintf(_("Moving object '%s' to '%s'"), $dn, $dnTo)."<br>";
+            }
+            $str.="</pre>";
+        }
+        $str.= "<button name='migrateNow'>"._("Migrate")."</button>";
+        return($str);
     }
    
     function save_object()
     {
-    
+        if(isset($_POST['migrateNow'])){
+            $ldap = $this->config->get_ldap_link();
+            $ldap->cd($this->config->current['BASE']);
+
+            // Try to add the new container objects
+            foreach($this->found['add'] as $dn => $data){
+                $ldap->cd($this->config->current['BASE']);
+                $ldap->create_missing_trees(ldap::convert($dn));
+            }
+
+            // Now move the objects to the new traget
+            foreach($this->found['move'] as $from => $to){
+                $ldap->rename_dn($from, $to);
+            }
+            $this->checkForIssues();
+        }
     } 
 
     function check()
index 9e3c39e654de63c58970e61f0025d9ec9e8f3175..02d129049fb30078a817d3fd6a682ddc735b5726 100644 (file)
@@ -83,11 +83,26 @@ class configViewer extends management
         if(count($this->toBeMigrated)){
             $first = $this->toBeMigrated[0]->getMigrationClass();
             $first->save_object();
-            $content =  $first->execute();
-            $smarty = get_smarty();
-            $smarty->assign('content', $content);
-            $smarty->assign('leftSteps', count($this->toBeMigrated));
-            return($smarty->fetch(get_template_path('migrate.tpl',TRUE)));
+    
+            // We've no problems with this property anymore.
+            while($first instanceOf propertyMigration && !$first->checkForIssues()){
+                $this->toBeMigrated[0]->save();
+                unset($this->toBeMigrated[0]);
+                $this->toBeMigrated = array_values($this->toBeMigrated);
+                if(count($this->toBeMigrated)){
+                    $first = $this->toBeMigrated[0]->getMigrationClass();
+                }else{
+                    $first = NULL;
+                }
+            }
+
+            if($first){
+                $content =  $first->execute();
+                $smarty = get_smarty();
+                $smarty->assign('content', $content);
+                $smarty->assign('leftSteps', count($this->toBeMigrated));
+                return($smarty->fetch(get_template_path('migrate.tpl',TRUE)));
+            }
         }
 
         return(management::execute());