Code

Updated FAI state detction.
[gosa.git] / gosa-plugins / fai / admin / fai / class_FAI.inc
index 812c4a26fb2ccb5b1c02b0e34ef68ce86c19ce0a..34c36746540238525e476cf333b0460a01f25543 100644 (file)
@@ -26,7 +26,14 @@ class FAI
 
     /* Collect some basic informations and initialize some variables */ 
     $base_release       = FAI::get_release_dn($Current_DN);
-    $previous_releases  = array_reverse(FAI::             get_previous_releases_of_this_release($base_release,true));
+    $previous_releases  = array_reverse(FAI::get_previous_releases_of_this_release($base_release,true));
+
+    $ldap->cat($base_release);
+    $attrs = $ldap->fetch();
+    $FAIstate = "branch";
+    if(isset($attrs['FAIstate'][0])){
+      $FAIstate = $attrs['FAIstate'][0];
+    }
 
     /* We must also include the given release dn */
     $previous_releases[] = $base_release;
@@ -59,6 +66,22 @@ class FAI
             }
           }
 
+          /* Seems to be an inherited class, apply current FAIstate to this classes 
+           */
+          if(!preg_match("/".normalizePreg($base_release)."$/i",$attr['dn'])){
+            $buffer['FAIstate'] = $FAIstate; 
+          }else{
+
+            /* Seems to be created within this release department.
+               This indicates - it can't be of state "freeze"
+             */             
+            if(isset($attr['FAIstate'])){
+              $buffer['FAIstate'] = $attr['FAIstate'][0];
+            }else{
+              $buffer['FAIstate'] = "branch"; 
+            }
+          }
+
           /* In detailed mode are some additonal informations visible */
           if($detailed){
 
@@ -158,7 +181,77 @@ class FAI
           $addObj['diff'] = FALSE;
         }
       } 
+    }else{
+
+      /* If this is the last CLASS of a specific name (e.g. DEPOTSERVER)
+          we have to remove this name from all profiles in this release.
+      */
+      $ldap = $config->get_ldap_link();
+      $ldap->cd($config->current['BASE']);
+      $obj_dn = FAI::get_parent_release_object($Current_DN,TRUE);
+
+      /* Dont't try to modify non FAIclasses  
+       */
+      if(!preg_match("/[^,]+,".normalizePreg(get_ou("faiou"))."/",$obj_dn)){
+        trigger_error("PLEASE check fai class handling in ".__LINE__." -> ".__FILE__);        
+        echo "<br>-->".$Current_DN."<br>";
+        echo "<br>-->".$obj_dn."<br>";
+      }else{
+
+        /* Get source object and check if it is a base FAIclass
+         */
+        $ldap->cat($obj_dn);
+        $attrs = $ldap->fetch();
+        $classes = array("FAIprofile","FAIscript","FAIpackageList","FAIpartitionTable","FAIHook","FAIvariable","FAItemplate");
+        if(count(array_intersect($classes,$attrs['objectClass']))){
+          $cn    = $attrs['cn'][0];
+
+          /* Check if this is the last with this name in the current release.
+              In this case we have to remove the package name 
+              from all profiles in this release.
+           */
+          $classes = FAI::get_all_objects_for_given_base(FAI::get_release_dn($Current_DN),
+              "(&(objectClass=FAIclass)(cn=".$cn."))"); 
+
+          /* Check if this is the last class with this name.
+           */
+          if(count($classes) == 1){
+
+            /* Get all FAI Profiles 
+             */
+            $profiles = FAI::get_all_objects_for_given_base(FAI::get_release_dn($Current_DN),
+                "(&(objectClass=FAIprofile)(FAIclass=*))"); 
+
+            /* Walk though all profiles and remove the source class name
+             */
+            foreach($profiles as $dn){
+              $ldap->cat($dn['dn']);
+              $attrs = $ldap->fetch();
+
+              $attrs = array('FAIclass' => $attrs['FAIclass'][0]);
+
+              /* Check if this Profile uses the source class ($cn)
+               */
+              if(preg_match("/".normalizePreg($cn)."/",$attrs['FAIclass'])){
+                $attrs['FAIclass'] = preg_replace("/[ ]*".normalizePreg($cn)."[ ]*/i"," ",$attrs['FAIclass']);
+                if(empty($attrs['FAIclass'])){
+                  $attrs['FAIclass'] = array();
+                }
+                $ldap->cd($dn['dn']);
+                $ldap->modify($attrs);
+
+                if (!$ldap->success()){
+                  msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
+                }
+              }
+            }
+          }
+        }
+      }
     }
+
+
+
     $FAI_objects_to_save = session::get('FAI_objects_to_save') ;
     $FAI_objects_to_save[$Current_DN] =  $addObj;
     session::set('FAI_objects_to_save',$FAI_objects_to_save);
@@ -186,7 +279,7 @@ class FAI
         $ar1[$key1] = $val1[0];
       }
 
-      if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
+      if(isset($ar2[$key1])&&  (is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
         $val1 = $val1[0];
         $ar2[$key1] = $ar2[$key1][0];
       }
@@ -205,9 +298,12 @@ class FAI
   {
     foreach($ar1 as $ak1 => $av1){
       if(!isset($ar2[$ak1]) || (!($av1 === $ar2[$ak1]))){
-        return(true);
+        return(TRUE);    
       }elseif(is_array($av1)){
-        return(FAI::arr_diff($av1,$ar2[$ak1]));
+        $ret = (FAI::arr_diff($av1,$ar2[$ak1]));
+        if($ret) {
+          return(TRUE);
+        }
       }
     }
     return(FALSE);
@@ -219,11 +315,23 @@ class FAI
   /* check which objects must be saved, and save them */
   static function save_release_changes_now()
   {
+    global $config;
     /* Variable init*/
     $to_save = array();
+    
+    $reload_fai_classes = FALSE;
 
     /* check which objects must be saved */
+    if(!session::is_set('FAI_objects_to_save')){
+      return;
+    }
     $FAI_objects_to_save = session::get('FAI_objects_to_save');
+    if(!is_array($FAI_objects_to_save)) {
+      print_a(array(session::get('FAI_objects_to_save')));
+      trigger_error("Can't save FAI objects, no array given.");
+      return;
+    }
+  
     foreach($FAI_objects_to_save as $Current_DN => $object){
       if($object['diff']){
         $sub_name = $Current_DN;
@@ -247,6 +355,7 @@ class FAI
     }
     $to_save = $tmp;
 
+
     /* Save objects and manage the correct release behavior*/
     foreach($to_save as $save){
 
@@ -255,7 +364,6 @@ class FAI
       $objectAttrs= $save['objectAttrs'];
 
       /* Get ldap object */ 
-      global $config;
       $ldap = $config->get_ldap_link();
       $ldap->cd($config->current['BASE']);
 
@@ -298,6 +406,12 @@ class FAI
           }else{
             $objectAttrs['FAIstate'][0] = "|removed";
           }
+
+          /* Force reload of FAI classes */
+          $classes = array("FAIprofile","FAIscript","FAIpackageList","FAIpartitionTable","FAIHook","FAIvariable","FAItemplate");
+          if(count(array_intersect($classes,$objectAttrs['objectClass']))){
+            $reload_fai_classes = TRUE;
+          }
         }
       }
 
@@ -310,20 +424,33 @@ class FAI
           print_a($objectAttrs);
         }
         FAI::save_FAI_object($Current_DN,$objectAttrs);
+
+        /* Force reload of FAI classes */
+        $classes = array("FAIprofile","FAIscript","FAIpackageList","FAIpartitionTable","FAIHook","FAIvariable","FAItemplate");
+        if(count(array_intersect($classes,$objectAttrs['objectClass']))){
+          $reload_fai_classes = TRUE;
+        }
+
       }else{
 
         /* This object is inherited by some sub releases */  
 
         /* Get all releases, that inherit this object */ 
-        $r = get_following_releases_that_inherit_this_object($Current_DN);
+        $r = FAI::get_following_releases_that_inherit_this_object($Current_DN);
 
         /* Get parent object */
         $ldap->cat($parent_obj);
-        $parent_attrs = FAI::                           prepare_ldap_fetch_to_be_saved($ldap->fetch());
+        $parent_attrs = FAI::prepare_ldap_fetch_to_be_saved($ldap->fetch());
 
         /* New objects require special handling */
         if($is_new){
 
+          /* Force reload of FAI classes */
+          $classes = array("FAIprofile","FAIscript","FAIpackageList","FAIpartitionTable","FAIHook","FAIvariable","FAItemplate");
+          if(count(array_intersect($classes,$objectAttrs['objectClass']))){
+            $reload_fai_classes = TRUE;
+          }
+
           /* check if there is already an entry named like this,
              in one of our parent releases */
           if(!empty($parent_obj)){
@@ -388,7 +515,26 @@ class FAI
           }
         }
       }
-    } 
+    }
+
+    /* Reload GOsa si FAI DB/cache
+     */
+    if($reload_fai_classes){
+      if( class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
+        $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);        
+        if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
+          $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db']; 
+          $tmp = new $evt['CLASS_NAME']($config);
+          $tmp->set_type(TRIGGERED_EVENT);
+          $tmp->add_targets(array("GOsa"));
+          $o_queue = new gosaSupportDaemon();
+          if(!$o_queue->append($tmp)){
+            msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
+          }
+        }
+      }
+    }
+
     session::set('FAI_objects_to_save',array());
   }
 
@@ -491,7 +637,9 @@ class FAI
 
       $ldap->add($attrs);
     }
-    show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
+    if (!$ldap->success()){
+      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
+    }
   }
 
 
@@ -785,7 +933,7 @@ class FAI
     $n_data['ou'] = $destination_name;
     $n_data['objectClass'] = array("top","organizationalUnit","FAIbranch");
     if(!empty($type)){
-      $n_data['FAIstate'] = $type;
+      $n_data['FAIstate'] = $type."/cow";
     }
 
     foreach($groups as $dn => $att){
@@ -821,7 +969,7 @@ class FAI
     $to_copy = array();
     foreach($source_dns as $dn => $attrs){
       $ldap->cd($dn);
-      $ldap->ls("(objectClass=gotoSubmenuEntry)(objectClass=gotoMenuEntry)",$dn,array("dn"));
+      $ldap->ls("(|(objectClass=gotoSubmenuEntry)(objectClass=gotoMenuEntry))",$dn,array("dn"));
       while($attrs = $ldap->fetch()){
         $destination = preg_replace("/".normalizePreg($dn)."$/","ou=".$destination_name.",".$dn,$attrs['dn']);
         $to_copy[$attrs['dn']] = $destination;
@@ -854,7 +1002,7 @@ class FAI
    *  @param $is_first          Boolean Use to identify the first func. call when recursivly called.
    *  @param $depth             Integer Current depth of recursion.
    */
-  function copy_FAI_resource_recursive($sourcedn,$destinationdn,$destinationName,$type="branch",$is_first = true,$depth=0)
+  static function copy_FAI_resource_recursive($sourcedn,$destinationdn,$destinationName,$type="branch",$is_first = true,$depth=0)
   {
     global $config;
     error_reporting(E_ALL | E_STRICT);
@@ -977,7 +1125,7 @@ class FAI
           $ldap->add($attr);
         }
 
-        if($ldap->error != "Success"){
+        if(!$ldap->success()){
 
           /* Some error occurred */
           print "---------------------------------------------";