Code

Reverted 8064
[gosa.git] / plugins / admin / fai / class_faiPartitionTable.inc
index 1d78d57219a9e839694d7e617708b2dd12808847..c85cf9ab33e11f49997054cfe9e286efffb82a78 100644 (file)
@@ -2,11 +2,6 @@
 
 class faiPartitionTable extends plugin
 {
-  /* CLI vars */
-  var $cli_summary= "Manage server basic objects";
-  var $cli_description= "Some longer text\nfor help";
-  var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /* attribute list for save action */
   var $ignore_account   = TRUE;
   var $attributes       = array("cn","description");
@@ -19,11 +14,21 @@ class faiPartitionTable extends plugin
   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
 
+  var $FAIstate         = "";
+  var $base             = "";
+  var $release          = "";
+  var $copy_paste_mode  = false;
+  var $cut_paste_mode  = false;
+
+  var $CopyPasteVars  = array("disks");
+
   function faiPartitionTable ($config, $dn= NULL)
   {
     /* Load Attributes */
     plugin::plugin ($config, $dn);
 
+    $this->acl ="#all#";
+
     /* If "dn==new" we try to create a new entry
      * Else we must read all objects from ldap which belong to this entry.
      * First read disks from ldap ... and then the partition definitions for the disks.
@@ -31,6 +36,19 @@ class faiPartitionTable extends plugin
     if($dn != "new"){
       $this->dn =$dn;
 
+      /* Set acls
+       */
+      $ui   = get_userinfo();
+      $acl  = get_permissions ($this->dn, $ui->subtreeACL);
+      $acli = get_module_permission($acl, "FAIclass", $this->dn);
+      $this->acl=$acli;
+
+      /* Get FAIstate
+       */
+      if(isset($this->attrs['FAIstate'][0])){
+        $this->FAIstate = $this->attrs['FAIstate'][0];
+      }
+
       /* Read all disks from ldap taht are defined fot this partition table 
        */
       $ldap = $this->config->get_ldap_link();
@@ -69,7 +87,8 @@ class faiPartitionTable extends plugin
            */
           $partition['status']="edited";
           $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition; 
-        }  
+        } 
+        ksort($this->disks[$name]['partitions']);
       }
     }
     ksort($this->disks);
@@ -77,6 +96,9 @@ class faiPartitionTable extends plugin
 
   function execute()
   {
+    /* Call parent execute */
+    plugin::execute();
+
     /* Fill templating stuff */
     $smarty= get_smarty();
     $display= "";
@@ -91,22 +113,40 @@ class faiPartitionTable extends plugin
         $usedDiskNames[]= $key;
       }
       $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames); 
+      $this->dialog->acl = $this->acl;
+      $this->dialog->FAIstate = $this->FAIstate;
       $this->is_dialog = true;
     }
 
     /* Edit disk.
      * Open dialog which allows us to edit the selected entry 
      */    
-    $_SESSION['objectinfo'] = $this->dn;
+
+    if($this->dn != "new"){
+      $_SESSION['objectinfo']= $this->dn;
+    }
+
     if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
       $usedDiskNames =array();
+
+      $Udisk = $_POST['disks'][0];
+      
       foreach($this->disks  as $key=>$disk){
-        if($key != $_POST['disks']){
+        if($key != $Udisk){
           $usedDiskNames[]= $key;
         }
       }
-      $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]); 
-      $_SESSION['objectinfo'] = $this->disks[$_POST['disks']]['dn'];
+      $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]); 
+      $this->dialog->acl = $this->acl;
+      $this->dialog->FAIstate = $this->FAIstate;
+     
+      /* Set object info string, which will be displayed in plugin info line */ 
+      if(isset($this->disks[$Udisk]['dn'])){
+        $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
+      }else{
+        $_SESSION['objectinfo'] = "";
+      }
+    
       $this->is_dialog = true;
     }
 
@@ -117,40 +157,46 @@ class faiPartitionTable extends plugin
       $this->dialog = NULL;
       $this->is_dialog=false;
     }
-  
+
     /* Dialog saved
-     * Save given data from Dialog, if no error is occured
+     * Save given data from Dialog, if no error is occurred
      */
     if(isset($_POST['SaveDisk'])){
-      $this->dialog->save_object();
-      if(count($this->dialog->check())){
-        foreach($this->dialog->check() as $msg){
-          print_red($msg);
-        }
-      }else{
-        $disk = $this->dialog->save();
-        if(isset($disk['rename'])){
-          if($this->disks[$disk['rename']['from']]['status']=="edited"){
-            $this->disks[$disk['rename']['from']]['status']="delete";
-          }else{
-            unset($this->disks[$disk['rename']['from']]);
+
+      if($this->FAIstate != "freeze"){
+        $this->dialog->save_object();
+        if(count($this->dialog->check())){
+          foreach($this->dialog->check() as $msg){
+            print_red($msg);
           }
-  
-          foreach($disk['partitions'] as $key => $val){
-            if($disk['partitions'][$key]['status']!="delete"){
-              $disk['partitions'][$key]['status']= "new";
+        }else{
+          $disk = $this->dialog->save();
+          if(isset($disk['rename'])){
+            if($this->disks[$disk['rename']['from']]['status']=="edited"){
+              $this->disks[$disk['rename']['from']]['status']="delete";
+            }else{
+              unset($this->disks[$disk['rename']['from']]);
+            }
+
+            foreach($disk['partitions'] as $key => $val){
+              if($disk['partitions'][$key]['status']!="delete"){
+                $disk['partitions'][$key]['status']= "new";
+              }
             }
+
+            $disk['status']="new";
+            $disk['cn']= $disk['rename']['to'];
           }
 
-          $disk['status']="new";
-          $disk['cn']= $disk['rename']['to'];
+          $this->disks[$disk['cn']]=$disk; 
+          unset($this->dialog);
+          $this->dialog = NULL;
+          $this->is_dialog=false;
+          ksort($this->disks);
         }
-      
-        $this->disks[$disk['cn']]=$disk; 
-        unset($this->dialog);
+      }else{
         $this->dialog = NULL;
         $this->is_dialog=false;
-        ksort($this->disks);
       }
     }
 
@@ -158,21 +204,24 @@ class faiPartitionTable extends plugin
      * Assign delete status for all its partitions      
      */
     if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
-      $disk = $_POST['disks'];
-      if($this->disks[$disk]['status']=="edited"){
-        $this->disks[$disk."-delete"]=$this->disks[$disk];
-        unset($this->disks[$disk]);
-        $disk = $disk."-delete";        
-        $this->disks[$disk]['status']="delete";
-        foreach($this->disks[$disk]['partitions'] as $name => $value ){
-          if($value['status']=="edited"){
-            $this->disks[$disk]['partitions'][$name]['status']="delete"; 
+      if($this->FAIstate != "freeze"){
+        foreach($_POST['disks'] as $disk) {
+          if($this->disks[$disk]['status']=="edited"){
+            $this->disks[$disk."-delete"]=$this->disks[$disk];
+            unset($this->disks[$disk]);
+            $disk = $disk."-delete";        
+            $this->disks[$disk]['status']="delete";
+            foreach($this->disks[$disk]['partitions'] as $name => $value ){
+              if($value['status']=="edited"){
+                $this->disks[$disk]['partitions'][$name]['status']="delete"; 
+              }else{
+                unset($this->disks[$disk]['partitions'][$name]);
+              }
+            }
           }else{
-            unset($this->disks[$disk]['partitions'][$name]);
+            unset($this->disks[$disk]);
           }
         }
-      }else{
-        unset($this->disks[$disk]);
       }
     }
 
@@ -193,10 +242,13 @@ class faiPartitionTable extends plugin
         $smarty->assign($attrs."CHK"," disabled ");
       }
     }
-    
+   
+    foreach($this->attributes as $attr){
+      $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
+    }
+
     $disks = $this->getDisks();
     $smarty->assign("disks"   ,$disks);
-    $smarty->assign("diskKeys",array_flip($disks));
     $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
     return($display);
   }
@@ -215,9 +267,17 @@ class faiPartitionTable extends plugin
           }
         }
         if(!empty($disk['description'])){
-          $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
+          if($cnt == 1){
+            $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
+          }else{
+            $a_return[$key]=  $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
+          }
         }else{
-          $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
+          if($cnt == 1){
+            $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
+          }else{
+            $a_return[$key]=  $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
+          }
         }
       }
     }
@@ -242,6 +302,7 @@ class faiPartitionTable extends plugin
    */
   function save_object()
   {
+    if($this->FAIstate == "freeze") return;
     plugin::save_object();
     foreach($this->attributes as $attrs){
       if(isset($_POST[$attrs])){
@@ -254,8 +315,22 @@ class faiPartitionTable extends plugin
   /* Check supplied data */
   function check()
   {
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+
+    /* If this is a new script, check if a script with this name already exists */
+    if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
 
-    $message= array();
+      /* Check if current name is already used for fai scripts in selected release */
+      $dn = 'cn='.$this->cn.",ou=disk,".$this->release;
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cat($dn);
+      if($ldap->count()){
+
+        $r =convert_department_dn($this->release);;
+        $message[] = sprintf(_("Can't insert a new fai partition table named '%s' in '%s' there is already one defined with the given name."),$this->cn,$r);
+      }
+    }
     return ($message);
   }
 
@@ -271,6 +346,11 @@ class faiPartitionTable extends plugin
      * 3 : Save partitions for each disk
      */  
 
+    /* Copy & Paste : Ensure that FAIstate is copied too */
+    if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
+      $this->attrs['FAIstate'] = $this->FAIstate;
+    }
+
     $ldap = $this->config->get_ldap_link();
 
     if($this->new){
@@ -278,15 +358,17 @@ class faiPartitionTable extends plugin
       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
       $ldap->cd($this->dn);
       $ldap->add($this->attrs);
-      show_ldap_error($ldap->get_error());
     }else{
       /* Add partition table to ldap
        */
       $ldap->cd($this->dn);
-      $ldap->modify($this->attrs);
-      show_ldap_error($ldap->get_error());
+      $this->cleanup();
+      $ldap->modify ($this->attrs); 
     }
+    show_ldap_error($ldap->get_error(), _("Saving FAI partition table base failed"));
   
+    /* Do object tagging */
+    $this->handle_object_tagging();
   
     /* Sort entries, because we must delete entries with status="delete" first */
     $order = array();
@@ -308,22 +390,37 @@ class faiPartitionTable extends plugin
       $disk_attrs['description']  =  $disk['description']; 
       $disk_attrs['objectClass']  =  array("top","FAIclass","FAIpartitionDisk");
 
+      /* We currently use the copy & paste method */
+      if($this->copy_paste_mode){
+        $disk['status'] = "new";
+      }
+
       if($disk['status']=="new"){
-        $ldap->cat($disk_dn);
+        $ldap->cat($disk_dn,array("objectClass"));
         if($ldap->count()){
           $disk['status']="edited";
         }
       }
+
+      /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for disks */
+      $ldap->cat($disk_dn,array("objectClass"));
+      $attrs = $ldap->fetch();
+      if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
+        $disk_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
+      }
+
       if($disk['status'] == "delete"){
         $ldap->cd($disk_dn);
         $ldap->rmdir_recursive($disk_dn);
+        show_ldap_error($ldap->get_error(), _("Removing FAI partition table failed"));
       }elseif($disk['status']== "edited"){
         if(empty($disk_attrs['description'])){
           $disk_attrs['description']=array();
         }
         $ldap->cd($disk_dn);
-        $ldap->modify($disk_attrs);
+        $this->cleanup();
+        $ldap->modify ($disk_attrs); 
+        show_ldap_error($ldap->get_error(), _("Saving FAI partition table failed"));
       }elseif($disk['status']== "new"){
         if(empty($disk_attrs['description'])){
           unset($disk_attrs['description']);
@@ -332,65 +429,153 @@ class faiPartitionTable extends plugin
         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
         $ldap->cd($disk_dn);
         $ldap->add($disk_attrs);
+        show_ldap_error($ldap->get_error(), _("Saving FAI partition table failed"));
       }else{
         print_red("unknown status while saving disks");
       }
 
-      show_ldap_error($ldap->get_error());
-      if($disk['status']!="delete")
-      /* Add all partitions */
-      foreach($disk['partitions'] as $key => $partition){
-        $partition_attrs = array();
+      $this->handle_object_tagging($disk_dn, $this->gosaUnitTag);
 
-        foreach($partition as $key => $value){
-          if(!empty($value)){
-            $partition_attrs[$key]=$value;        
-          }else{
-            unset($partition_attrs[$key]);        
+      /* Collect all opperations. Delete first than add new entries .*/
+      $Todo['delete'] = array();
+      $Todo['rest'] = array();
+
+      /* Skip partition handling if current disk is marked as deleted */
+      if($disk['status']!="delete"){
+
+        /* Add all partitions */
+        foreach($disk['partitions'] as $part_nr => $partition){
+
+          /* Create attribute array */
+          $partition_attrs = array();
+          foreach($partition as $key => $value){
+            if(!empty($value)){
+              $partition_attrs[$key]=$value;        
+            }else{
+              unset($partition_attrs[$key]);        
+            }
           }
-        }
 
-        $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
-        $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
-        $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
-        
-        unset($partition_attrs['status']);
-        unset($partition_attrs['old_cn']);
-
-        if($partition['status']=="new"){
-          $ldap->cat($partition_dn);
-          if($ldap->count()){
-            $partition['status']="edited";
+          /* Create partition dn and add necessary attributes */
+          $partition_attrs['FAIpartitionNr']= $part_nr;  
+          $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;      
+          $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
+          $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
+
+          /* Unset status attributes */
+          unset($partition_attrs['status']);
+          unset($partition_attrs['old_cn']);
+
+          /* We currently use the copy & paste method */
+          if($this->copy_paste_mode){
+            $partition['status']="new";
           }
-        }
 
-        if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
-          $partition_attrs['FAImountPoint']="swap";
+          /* Check if this partition is realy new .. */
+          if($partition['status']=="new"){
+            $ldap->cat($partition_dn,array("objectClass"));
+            if($ldap->count()){
+              $partition['status']="edited";
+            }
+          }
+
+          /* Set mount point top swap if it is emtpy */
+          if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
+            $partition_attrs['FAImountPoint']="swap";
+          }
+
+          /* Collect all operations */
+          if(($partition['status'] == "delete")&&($disk['status']!="new")){
+
+            $partition_dn_del = "FAIpartitionNr=".$partition['FAIpartitionNr'].",".$disk_dn;      
+
+            $Todo['delete'][$partition_dn_del] = $partition_attrs;
+          }elseif($partition['status'] == "new"){
+            $Todo['rest'][$partition_dn] = $partition_attrs;
+          }elseif($partition['status'] == "edited"){
+            $Todo['rest'][$partition_dn] = $partition_attrs;
+          } 
         }
+      }
 
-        if(($partition['status'] == "delete")&&($disk['status']!="new")){
+      /* First of all, delete old entries */
+      foreach($Todo['delete'] as $partition_dn => $attrs){
           $ldap->cd($partition_dn);
-          $ldap->rmdir_recursive($partition_dn);
-        }elseif($partition['status'] == "new"){
+          $res = $ldap->rmdir_recursive($partition_dn);
+          show_ldap_error($ldap->get_error(), _("Removing FAI partition table entry failed"));
+      }
+    
+      /* Add/edit entries */
+      foreach($Todo['rest'] as $partition_dn => $partition_attrs){
+
+        /* Check if entry exists */
+        $ldap->cat($partition_dn);
+        if($ldap->count()){
+
+          /* Fix problem with missing objectClass "gosaAdministrativeUnitTag" for partitions */
+          $attrs = $ldap->fetch();
+          if(isset($attrs['objectClass']) && in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
+            $partition_attrs['objectClass'][] = "gosaAdministrativeUnitTag";
+          }
+          if(empty($partition_attrs['description'])){
+            $partition_attrs['description']=array();
+          }
+          if(empty($partition_attrs['FAIfsOptions'])){
+            $partition_attrs['FAIfsOptions']=array();
+          }
+          if(empty($partition_attrs['FAImountOptions'])){
+            $partition_attrs['FAImountOptions']=array();
+          }
+          if(empty($partition_attrs['FAIpartitionFlags'])){
+            $partition_attrs['FAIpartitionFlags']=array();
+          }
+          $ldap->cd($partition_dn);
+          $this->cleanup();
+          $ldap->modify ($partition_attrs); 
+          show_ldap_error($ldap->get_error(), _("Saving FAI partition table entry failed"));
+        }else{
+
           if(empty($partition_attrs['description'])){
             unset($partition_attrs['description']);
           }
+
           $ldap->cd($this->config->current['BASE']);
           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
           $ldap->cd($partition_dn);
-          $ldap->add($partition_attrs);
-        }elseif($partition['status'] == "edited"){
-          if(empty($partition_attrs['description'])){
-            $partition_attrs['description']=array();
+          if (isset($partition_attrs['gosaUnitTag']) && !in_array_ics("gosaAdministrativeUnitTag",$partition_attrs['objectClass'])){
+            $partition_attrs['objectClass'][]= "gosaAdministrativeUnitTag";
           }
-          $ldap->cd($partition_dn);
-          $ldap->modify($partition_attrs);
-        } 
-      show_ldap_error($ldap->get_error());
+          $ldap->add($partition_attrs);
+          show_ldap_error($ldap->get_error(), _("Saving FAI partition table entry failed"));
+        }
+        $this->handle_object_tagging($partition_dn, $this->gosaUnitTag);
       }
     }
     $this->handle_post_events("add");
   }
+
+  
+  /* return copy & paste dialog
+   */
+  function getCopyDialog()
+  {
+    /* Ask for cn */
+    $smarty = get_smarty();
+    $smarty->assign("cn" ,$this->cn);
+    $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+  /* Get posted cn */
+  function saveCopyDialog()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = $_POST['cn'];
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: