Code

Added copy & paste for profiles
[gosa.git] / setup / class_setupStep_Migrate.inc
index b166c1a3767d54843a2e81ede57e5220c5301b25..5dcd8e649d9ccba0c9fdf273b1e8a08f0dc6d26c 100644 (file)
@@ -341,6 +341,21 @@ class Step_Migrate extends setup_step
 
     $group_ou = $cv['groupou'];
     $ldap->cd($cv['base']);
+
+    /***********
+     * Get all gosaDepartments to be able to
+     *  validate correct ldap tree position of every single user
+     ***********/
+    $valid_deps = array();
+    $valid_deps['/'] = $cv['base'];
+    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
+    while($attrs = $ldap->fetch()){
+      $valid_deps[] = $attrs['dn'];
+    }
+
+    /***********
+     * Get all groups
+     ***********/
     $res = $ldap->search("(objectClass=posixGroup)",array("dn"));
     if(!$res){
       $this->checks['outside_groups']['STATUS']    = FALSE;
@@ -349,10 +364,16 @@ class Step_Migrate extends setup_step
       return(false);
     }
 
-
     $this->outside_groups = array();
     while($attrs = $ldap->fetch()){
-      if((!preg_match("/^[^,]+,".normalizePreg($group_ou)."/",$attrs['dn'])) && !preg_match("/,dc=addressbook,/",$attrs['dn'])){
+      $group_db_base = preg_replace("/^[^,]+,".normalizePreg($group_ou)."+,/i","",$attrs['dn']);
+
+      /* Check if entry is not an addressbook only user
+       *  and verify that he is in a valid department
+       */
+      if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$group_db_base) &&
+          !in_array($group_db_base,$valid_deps)
+        ){
         $attrs['selected'] = FALSE;
         $attrs['ldif']     = "";
         $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
@@ -362,7 +383,7 @@ class Step_Migrate extends setup_step
     if(count($this->outside_groups)){
       $this->checks['outside_groups']['STATUS']    = FALSE;
       $this->checks['outside_groups']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
-      $this->checks['outside_groups']['ERROR_MSG'] = 
+      $this->checks['outside_groups']['ERROR_MSG'] =
         sprintf(_("Found %s groups outside the configured tree '%s'."),count($this->outside_groups),$group_ou);
       $this->checks['outside_groups']['ERROR_MSG'].= "&nbsp;<input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
       return(false);
@@ -374,8 +395,7 @@ class Step_Migrate extends setup_step
     }
   }
 
-
-  /* Search for users outside the people ou 
+ /* Search for users outside the people ou
    */
   function search_outside_users()
   {
@@ -385,8 +405,24 @@ class Step_Migrate extends setup_step
         $cv['connection'],
         FALSE,
         $cv['tls']);
-    $people_ou = $cv['peopleou'];
+
     $ldap->cd($cv['base']);
+
+
+    /***********
+     * Get all gosaDepartments to be able to
+     *  validate correct ldap tree position of every single user
+     ***********/
+    $valid_deps = array();
+    $valid_deps['/'] = $cv['base'];
+    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn","ou"));
+    while($attrs = $ldap->fetch()){
+      $valid_deps[] = $attrs['dn'];
+    }
+
+    /***********
+     * Search for all users
+     ***********/
     $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))",array("dn"));
     if(!$res){
       $this->checks['outside_users']['STATUS']    = FALSE;
@@ -395,10 +431,24 @@ class Step_Migrate extends setup_step
       return(false);
     }
 
-
+    /***********
+     * Check if returned users are within a valid GOsa deparmtment. (peopleou,gosaDepartment,base)
+     ***********/
     $this->outside_users = array();
+    $people_ou = trim($cv['peopleou']);
+    if(!empty($people_ou)){
+      $people_ou = $people_ou.",";
+    }
+
     while($attrs = $ldap->fetch()){
-      if((!preg_match("/^[^,]+,".normalizePreg($people_ou)."/",$attrs['dn'])  && !preg_match("/,dc=addressbook,/",$attrs['dn']))){
+      $people_db_base = preg_replace("/^[^,]+,".normalizePreg($people_ou)."/i","",$attrs['dn']);
+
+      /* Check if entry is not an addressbook only user
+       *  and verify that he is in a valid department
+       */
+      if( !preg_match("/".normalizePreg("dc=addressbook,")."/",$people_db_base) &&
+          !in_array($people_db_base,$valid_deps)
+         ){
         $attrs['selected'] = FALSE;
         $attrs['ldif']     = "";
         $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
@@ -408,7 +458,7 @@ class Step_Migrate extends setup_step
     if(count($this->outside_users)){
       $this->checks['outside_users']['STATUS']    = FALSE;
       $this->checks['outside_users']['STATUS_MSG']= "<font style='color:#F0A500'>"._("Warning")."</font>";
-      $this->checks['outside_users']['ERROR_MSG'] = 
+      $this->checks['outside_users']['ERROR_MSG'] =
         sprintf(_("Found %s user(s) outside the configured tree '%s'."),count($this->outside_users),$people_ou);
       $this->checks['outside_users']['ERROR_MSG'].= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
       return(false);
@@ -462,7 +512,8 @@ class Step_Migrate extends setup_step
     $res = $ldap->add($testEntry);
     $ldap->cat($dn);
     if(!$ldap->count()){
-      gosa_log($ldap->get_error());
+      new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
+
       $this->checks['permissions']['STATUS']    = FALSE;
       $this->checks['permissions']['STATUS_MSG']= _("Failed");
       $this->checks['permissions']['ERROR_MSG'] = 
@@ -475,7 +526,7 @@ class Step_Migrate extends setup_step
     $res = $ldap->rmDir($dn);
     $ldap->cat($dn);
     if($ldap->count()){
-      gosa_log($ldap->get_error());
+      new log("view","setup/".get_class($this),$dn,array(),$ldap->get_error());
       $this->checks['permissions']['STATUS']    = FALSE;
       $this->checks['permissions']['STATUS_MSG']= _("Failed");
       $this->checks['permissions']['ERROR_MSG'] = 
@@ -622,12 +673,12 @@ class Step_Migrate extends setup_step
         $cv['tls']);
 
     /* Skip GOsa internal departments */
-    $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
-        "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
+    $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/^ou=people,/","/^ou=groups,/",
+        "/(,|)ou=configs,/","/(,|)ou=systems,/",
+        "/(,|)ou=apps,/","/(,|)ou=mime,/","/(,|)ou=devices/","/^ou=aclroles,/","/^ou=incoming,/",
         "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
         "/(,|)ou=winstations,/");
 
-
     /* Get all invisible departments */
     $ldap->cd($cv['base']); 
     $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
@@ -648,7 +699,7 @@ class Step_Migrate extends setup_step
      */
     foreach($this->deps_to_migrate as $key => $attrs){
       $dn = $attrs['dn'];
-      $skip = false;
+      $skip = false;;
       foreach($skip_dns as $skip_dn){
         if(preg_match($skip_dn,$dn)){
           $skip = true;
@@ -883,9 +934,9 @@ class Step_Migrate extends setup_step
     }
 
     if($only_ldif){
-      $this->acl_create_changes ="\n".$cv['base']."\n";
+      $this->acl_create_changes ="\n".($ldap->fix($cv['base']))."\n";
       $this->acl_create_changes.=$this->array_to_ldif($attrs)."\n";
-      $this->acl_create_changes.="\n".$cv['base']."\n";
+      $this->acl_create_changes.="\n".($ldap->fix($cv['base']))."\n";
       $this->acl_create_changes.=$this->array_to_ldif($attrs_new);
     }else{
    
@@ -937,13 +988,18 @@ class Step_Migrate extends setup_step
     /* Get current base attributes */
     $ldap->cd($cv['base']);
   
+    $people_ou = trim($cv['peopleou']);
+    if(!empty($people_ou)){
+      $people_ou = trim($people_ou).",";
+    }
+
     if($cv['peopledn'] == "cn"){
-      $dn = "cn=System Administrator,".$cv['peopleou'].",".$cv['base'];
+      $dn = "cn=System Administrator,".$people_ou.$cv['base'];
     }else{
-      $dn = "uid=".$uid.",".$cv['peopleou'].",".$cv['base'];
+      $dn = "uid=".$uid.",".$people_ou.$cv['base'];
     }
 
-    $methods = @passwordMethod::get_available_methods_if_not_loaded();
+    $methods = @passwordMethod::get_available_methods();
     $p_m = $methods[$cv['encryption']];
     $p_c = new $p_m(array());
     $hash = $p_c->generate_hash($pw2);
@@ -1007,7 +1063,7 @@ class Step_Migrate extends setup_step
         $dn = base64_decode($b_dn);
         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
         if(!$perform){
-          $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
+          $this->outside_winstations[$b_dn]['ldif'] = _("Winstation will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
 
 
           /* Check if there are references to this object */
@@ -1055,8 +1111,8 @@ class Step_Migrate extends setup_step
         $dn = base64_decode($b_dn);
         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
         if(!$perform){
-          $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
 
+          $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
 
           /* Check if there are references to this object */
           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
@@ -1096,14 +1152,14 @@ class Step_Migrate extends setup_step
       print_red(_("Couldn't move users to specified department."));
       return(false);
     }
+      
     foreach($this->outside_users as $b_dn => $data){
       $this->outside_users[$b_dn]['ldif'] ="";
       if($data['selected']){
         $dn = base64_decode($b_dn);
         $d_dn = preg_replace("/,.*$/",",".base64_decode($destination_dep),$dn);
         if(!$perform){
-          $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".$dn."<br>"._("to").":<br>\t".$d_dn;
+          $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));
 
           /* Check if there are references to this object */
           $ldap->search("(&(member=".$dn.")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))",array('dn'));
@@ -1160,10 +1216,17 @@ class Step_Migrate extends setup_step
     }
     
     if($this->outside_winstations_dialog){
+
+      /* Fix displayed dn syntax */ 
+      $tmp = $this->outside_winstations;
+      foreach($tmp as $key => $data){
+        $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
+      }
+
       $smarty = get_smarty();
       $smarty->assign("ous",$this->get_all_winstation_ous());
       $smarty->assign("method","outside_winstations");
-      $smarty->assign("outside_winstations",$this->outside_winstations);
+      $smarty->assign("outside_winstations",$tmp);
       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
     }
     /*************
@@ -1199,10 +1262,17 @@ class Step_Migrate extends setup_step
     }
     
     if($this->outside_groups_dialog){
+
+      /* Fix displayed dn syntax */ 
+      $tmp = $this->outside_groups;
+      foreach($tmp as $key => $data){
+        $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
+      }
+
       $smarty = get_smarty();
       $smarty->assign("ous",$this->get_all_group_ous());
       $smarty->assign("method","outside_groups");
-      $smarty->assign("outside_groups",$this->outside_groups);
+      $smarty->assign("outside_groups",$tmp);
       $smarty->assign("group_details", $this->show_details);
       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
     }
@@ -1240,10 +1310,17 @@ class Step_Migrate extends setup_step
     }
     
     if($this->outside_users_dialog){
+
+      /* Fix displayed dn syntax */ 
+      $tmp = $this->outside_users;
+      foreach($tmp as $key => $data){
+        $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
+      }
+
       $smarty = get_smarty();
       $smarty->assign("ous",$this->get_all_people_ous());
       $smarty->assign("method","outside_users");
-      $smarty->assign("outside_users",$this->outside_users);
+      $smarty->assign("outside_users",$tmp);
       $smarty->assign("user_details", $this->show_details);
       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
     }
@@ -1357,8 +1434,15 @@ class Step_Migrate extends setup_step
 
     /* Display migration dialog */
     if($this->users_migration_dialog){
+
+      /* Fix displayed dn syntax */ 
+      $tmp = $this->users_to_migrate;
+      foreach($tmp as $key => $data){
+        $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
+      }
+
       $smarty = get_smarty();
-      $smarty->assign("users_to_migrate",$this->users_to_migrate);
+      $smarty->assign("users_to_migrate",$tmp);
       $smarty->assign("method","migrate_users");
       $smarty->assign("user_details", $this->show_details);
       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
@@ -1406,7 +1490,14 @@ class Step_Migrate extends setup_step
     /* Display migration dialog */
     if($this->dep_migration_dialog){
       $smarty = get_smarty();
-      $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
+   
+      /* Fix displayed dn syntax */ 
+      $tmp = $this->deps_to_migrate;
+      foreach($tmp as $key => $data){
+        $tmp[$key]['dn'] = @LDAP::fix($data['dn']);
+      }
+
+      $smarty->assign("deps_to_migrate",$tmp);
       $smarty->assign("method","migrate_deps");
       $smarty->assign("deps_details", $this->show_details);
       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
@@ -1593,10 +1684,11 @@ class Step_Migrate extends setup_step
   }
 
 
 function get_all_people_ous()
+ function get_all_people_ous()
   {
     /* Get collected configuration settings */
     $cv = $this->parent->captured_values;
+    $people_ou = trim($cv['peopleou']);
 
     /* Establish ldap connection */
     $ldap = new LDAP($cv['admin'],
@@ -1605,33 +1697,54 @@ class Step_Migrate extends setup_step
         FALSE,
         $cv['tls']);
 
-    $ldap->cd($cv['base']);
-    $ldap->search("(".$cv['peopleou'].")",array("dn"));
-  
-    if($ldap->count() == 0 ){
-      $add_dn = $cv['peopleou'].",".$cv['base'];
-      $naming_attr = preg_replace("/=.*$/","",$add_dn);
-      $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
-      $add = array();
-      $add['objectClass'] = array("organizationalUnit");
-      $add[$naming_attr] = $naming_value;
 
-      $ldap->cd($cv['base']);
-      $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
-      $ldap->cd($add_dn);
-      $ldap->add($add);
-    }
+    /*****************
+     * If people ou is NOT empty
+     * search for for all objects matching the given container
+     *****************/
+    if(!empty($people_ou)){
+      $ldap->search("(".$people_ou.")",array("dn"));
+
+      /* Create people ou if there is currently none */
+      if($ldap->count() == 0 ){
+        $add_dn = $cv['peopleou'].",".$cv['base'];
+        $naming_attr = preg_replace("/=.*$/","",$add_dn);
+        $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
+        $add = array();
+        $add['objectClass'] = array("organizationalUnit");
+        $add[$naming_attr] = $naming_value;
+        $ldap->cd($cv['base']);
+        $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
+        $ldap->cd($add_dn);
+        $ldap->add($add);
+      }
 
-    $ldap->search("(".$cv['peopleou'].")",array("dn"));
-    $tmp = array();
-    while($attrs= $ldap->fetch()){
-      if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
-        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+      /* Create result */
+      $ldap->search("(".$cv['peopleou'].")",array("dn"));
+      $tmp = array();
+      while($attrs= $ldap->fetch()){
+        if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
+          $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+        }
+      }
+    } else{
+
+      /************
+       * If people ou is empty
+       * Get all valid gosaDepartments
+       ************/
+      $ldap->cd($cv['base']);
+      $tmp = array();
+      $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
+      $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
+      while($attrs = $ldap->fetch()){
+        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
       }
     }
-    return($tmp); 
+    return($tmp);
   }
 
+
   function get_all_winstation_ous()
   {
     /* Get collected configuration settings */
@@ -1679,7 +1792,7 @@ class Step_Migrate extends setup_step
   }
 
 
 function get_all_group_ous()
+ function get_all_group_ous()
   {
     /* Get collected configuration settings */
     $cv = $this->parent->captured_values;
@@ -1691,31 +1804,52 @@ class Step_Migrate extends setup_step
         FALSE,
         $cv['tls']);
 
+    $group_ou = trim($cv['groupou']);
+    if(!empty($group_ou)){
+      $group_ou = trim($group_ou);
+    }
+
+    /************
+     * If group ou is NOT empty
+     * Get all valid group ous, create one if necessary
+     ************/
     $ldap->cd($cv['base']);
-    $ldap->search("(".$cv['groupou'].")",array("dn"));
-  
-    if($ldap->count() == 0 ){
-      $add_dn = $cv['groupou'].",".$cv['base'];
-      $naming_attr = preg_replace("/=.*$/","",$add_dn);
-      $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
-      $add = array();
-      $add['objectClass'] = array("organizationalUnit");
-      $add[$naming_attr] = $naming_value;
+    if(!empty($group_ou)){
+      $ldap->search("(".$group_ou.")",array("dn"));
+      if($ldap->count() == 0 ){
+        $add_dn = $group_ou.$cv['base'];
+        $naming_attr = preg_replace("/=.*$/","",$add_dn);
+        $naming_value = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$add_dn);
+        $add = array();
+        $add['objectClass'] = array("organizationalUnit");
+        $add[$naming_attr] = $naming_value;
 
+        $ldap->cd($cv['base']);
+        $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
+        $ldap->cd($add_dn);
+        $ldap->add($add);
+      }
+      $ldap->search("(".$group_ou.")",array("dn"));
+      $tmp = array();
+      while($attrs= $ldap->fetch()){
+        if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
+          $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+        }
+      }
+    }else{
+      /************
+       * If group ou is empty
+       * Get all valid gosaDepartments
+       ************/
       $ldap->cd($cv['base']);
-      $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$add_dn));
-      $ldap->cd($add_dn);
-      $ldap->add($add);
-    }
-
-    $ldap->search("(".$cv['groupou'].")",array("dn"));
-    $tmp = array();
-    while($attrs= $ldap->fetch()){
-      if(!preg_match("/ou=snapshots,/",$attrs['dn'])){
-        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
+      $tmp = array();
+      $ldap->search("(&(objectClass=gosaDepartment)(ou=*))",array("dn"));
+      $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
+      while($attrs = $ldap->fetch()){
+        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
       }
     }
-    return($tmp); 
+    return($tmp);
   }