Code

Updated list summary, using images instead of object names
[gosa.git] / plugins / admin / fai / class_faiProfile.inc
index 5cb6600f99930499eb6e251d02bd4d4b5133b79e..e9b59675fe583568bf80df95c71d288fcec7b878 100644 (file)
@@ -2,11 +2,6 @@
 
 class faiProfile 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;
 
@@ -30,12 +25,32 @@ class faiProfile extends plugin
   var $FAIclasses       = array();  // Contains classname seperated in an array
   var $FAIAllclasses    = array();  // Contains all possible Classnames
 
+  var $FAIstate       = "";
+  var $base           = "";
+  var $release        = "";
+  var $copy_paste_mode= false;
+  var $cut_paste_mode = false;
+
+  var $CopyPasteVars  = array("FAIclass","FAIclasses","FAIAllclasses");
+
   function faiProfile($config, $dn= NULL)
   {
     /* Load Attributes */
     plugin::plugin ($config, $dn);
     $ldap=$this->config->get_ldap_link();
 
+    $this->acl = "#all#";
+
+    if($this->dn != "new"){
+      /* Set acls
+       */
+      $ui   = get_userinfo();
+      $acl  = get_permissions ($this->dn, $ui->subtreeACL);
+      $acli = get_module_permission($acl, "FAIclass", $this->dn);
+      $this->acl=$acli;
+    }
+
+    /* Parse ldap attribute to get all assigned classes */
     $tmp = split(" ",$this->FAIclass);
     $tmp2 = array();
     foreach($tmp as $class){
@@ -43,37 +58,58 @@ class faiProfile extends plugin
         $tmp2[trim($class)] = trim($class);
       }
     }
-    
+
+    if(isset($this->attrs['FAIstate'][0])){
+      $this->FAIstate = $this->attrs['FAIstate'][0];
+    }
+
+    /* Sort assigned classes */ 
     if(is_array($tmp2)){
       foreach($tmp2 as $class){
         $this->FAIclasses[$class]=$class;
       }
     }
 
-    $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTAble","FAIpackage");
-
-    /* Build filter */
-    $filter= "";
-    foreach ($categories as $cat){
-      $filter.= "(objectClass=$cat)";
+    /* Search only in fai tree */
+    $ObjectTypes = array(
+        "FAIpartitionTable"  => array("OU"=>"ou=disk,"       ),
+        "FAIpackageList"     => array("OU"=>"ou=packages,"   ),
+        "FAIscript"          => array("OU"=>"ou=scripts,"    ),
+        "FAIvariable"        => array("OU"=>"ou=variables,"  ),
+        "FAIhook"            => array("OU"=>"ou=hooks,"      ),
+#        "FAIprofile"         => array("OU"=>"ou=profiles,"  ),
+        "FAItemplate"        => array("OU"=>"ou=templates,"  ));
+
+    $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
+    if($_SESSION['faifilter']['branch']!="main"){
+      $base = $_SESSION['faifilter']['branch'];
     }
-    
-    $sort = array();
-    $base = $_SESSION['faifilter']['base'];
+
+    /* Get ldap connection */
+    $ldap= $this->config->get_ldap_link();
     $ldap->cd($base);
-    $ldap->search("(|$filter)",array("*"));
-    while($attrs = $ldap->fetch()){
-      foreach($categories as $cat){
-        if(in_array($cat,$attrs['objectClass'])){
-          $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
-          $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
-        }
+    $sort = array();
+    /* Capture objects from given base */
+    $result = array();
+    foreach($ObjectTypes as $oc => $data){
+      $ou = $data['OU'].$base;
+
+      $ldap->ls("(objectClass=".$oc.")",$ou,array("cn","objectClass","dn"));
+      while($attrs = $ldap->fetch()){
+        $this->FAIAllclasses[$attrs['cn'][0]][$oc]=$attrs;
+        $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
       }
-    } 
-    ksort($sort);
+    }
+  
+    /* Sort the sort array */
+    //ksort($sort);
+
+    /* Reorder the FAIclasses array */
     foreach($sort as $name){
       $tmp[$name] =$this->FAIAllclasses[$name];
     }
+
+    /* Assign sorted classes */
     $this->FAIAllclasses = array();
     $this->FAIAllclasses = $tmp;
 
@@ -84,7 +120,7 @@ class faiProfile extends plugin
   }
 
 
-  /* Combine new array */
+  /* Combine new array, used for up down buttons */
   function combineArrays($ar0,$ar1,$ar2)
   {
     $ret = array();
@@ -103,6 +139,7 @@ class faiProfile extends plugin
     return($ret);
   }
 
+  /* returns position in array */
   function getpos($atr,$attrs)
   {
     $i = 0;
@@ -115,7 +152,7 @@ class faiProfile extends plugin
     return(-1);
   }
 
-  /* TRansports the geiven Arraykey one position up*/
+  /* Transports the given Arraykey one position up*/
   function ArrayUp($atr,$attrs)
   {
     $ret = $attrs;
@@ -132,7 +169,7 @@ class faiProfile extends plugin
   }
 
 
-  /* TRansports the geiven Arraykey one position up*/
+  /* Transports the given Arraykey one position down*/
   function ArrayDown($atr,$attrs)
   {
     $ret = $attrs;
@@ -148,6 +185,7 @@ class faiProfile extends plugin
     return($ret);
   }
 
+  /* class one position up */
   function catUp($id)
   {
     /* Get all cats depinding on current dir */
@@ -155,6 +193,7 @@ class faiProfile extends plugin
     $this->FAIclasses =$this->ArrayUp($id,$cats);
   }
 
+  /* Class one position down */
   function catDown($id)
   {
     /* Get all cats depinding on current dir */
@@ -189,6 +228,7 @@ class faiProfile extends plugin
         $s_action  = "add";
       }
 
+      /* Check if a list element should be pushed one position up */
       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
         $sort_once = true;
         $val = preg_replace("/sortup_/","",$name);
@@ -197,6 +237,7 @@ class faiProfile extends plugin
         $this->catUp($val);
       }
       
+      /* Check if a list element should be pushed one position down */
       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
         $sort_once = true;
         $val = preg_replace("/sortdown_/","",$name);
@@ -230,7 +271,7 @@ class faiProfile extends plugin
         $this->is_dialog=false;
         unset($this->dialog);
         $this->dialog=NULL;
-        ksort($this->FAIclasses);
+        //ksort($this->FAIclasses);
       }
     }
 
@@ -249,33 +290,40 @@ class faiProfile extends plugin
     $divlist  =new divSelectBox("Profile");
     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
 
+    /* item images */
     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
-    $objTypes['FAIpackages']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
+    $objTypes['FAIpackageList']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
 
+    /* Delete button */
     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
-
+    
+    /* Up down buttons */
     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
 
+    /* Append fai classes to divlist */
     foreach($this->FAIclasses as $usedClass){
       $str = "&nbsp;";
 
-
       if(isset($this->FAIAllclasses[$usedClass])){
         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
           $str.= $objTypes[$class]; 
         }
       }
-
-          
   
       $field1 = array("string"=> $usedClass,"attach"=>"");
       $field2 = array("string"=> $str,"attach"=>"");
-      $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).preg_replace("/%s/",base64_encode($usedClass),$linkupdown),"attach"=>"style='border-right:none;'");
+      if($this->FAIstate != "freeze"){
+        $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
+            preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
+            "attach"=>"style='border-right:none;'");
+      }else{
+        $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
+      }
       $divlist->AddEntry(array($field1,$field2,$field3));
     }
 
@@ -293,6 +341,13 @@ class faiProfile extends plugin
       }
     }
 
+    foreach($this->attributes as $attr){
+      if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
+        $smarty->assign($attr."ACL"," disabled ");
+      }else{
+        $smarty->assign($attr."ACL","  ");
+      }
+    }
 
     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
     return($display);
@@ -303,6 +358,7 @@ class faiProfile extends plugin
     $ldap = $this->config->get_ldap_link();
     $ldap->cd ($this->dn);
     $ldap->rmdir_recursive($this->dn);
+    show_ldap_error($ldap->get_error(), _("Removing FAI profile failed"));
     $this->handle_post_events("remove");    
   }
 
@@ -322,24 +378,48 @@ class faiProfile extends plugin
   /* Check supplied data */
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
 
-    if(count($this->FAIclasses) == 0){
-      $message[]=_("Please assign at least one class to this  profile.");
-    }
+    if($this->copy_paste_mode){
 
-    if(empty($this->cn)){
-      $message[]=_("Please enter a valid name.");
-    }
+      /* 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) ){
 
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd($_SESSION['faifilter']['base']);
-    $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
+        /* Check if current name is already used for fai scripts in selected release */
+        $dn = 'cn='.$this->cn.",ou=profiles,".$this->release;
+        $ldap = $this->config->get_ldap_link();
+        $ldap->cat($dn);
+        if($ldap->count()){
 
-    if($ldap->count()){
-      $message[]=_("There is already a profile with this class name defined.");
-    }
+          $r =convert_department_dn($this->release);;
+          $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
+        }
+      }
+    }else{
+      if(count($this->FAIclasses) == 0){
+        $message[]=_("Please assign at least one class to this  profile.");
+      }
+
+      if(empty($this->cn)){
+        $message[]=_("Please enter a valid name.");
+      }
 
+      $ldap = $this->config->get_ldap_link();
+      $release = "ou=profiles,ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
+      if($_SESSION['faifilter']['branch']!="main"){
+        $release = "ou=profiles,".$_SESSION['faifilter']['branch'];
+      }
+      $ldap->cd($release);
+      if ($this->old_cn == ""){
+        $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
+      } else {
+        $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
+      }
+      if($ldap->count()){
+        $message[]=_("There is already a profile with this class name defined.");
+      }
+    }
     return ($message);
   }
 
@@ -358,11 +438,17 @@ class faiProfile extends plugin
 
     $this->attrs['FAIclass']=trim($this->FAIclass);
 
-    $ldap->cat($this->dn);
+    /* Copy & Paste : Ensure that FAIstate is copied too */
+    if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
+      $this->attrs['FAIstate'] = $this->FAIstate;
+    }
+
+    $ldap->cat($this->dn,array("objectClass"));
     if($ldap->count()!=0){
       /* Write FAIscript to ldap*/
       $ldap->cd($this->dn);
-      $ldap->modify($this->attrs);
+      $this->cleanup();
+      $ldap->modify ($this->attrs); 
     }else{
       /* Write FAIscript to ldap*/
       $ldap->cd($this->config->current['BASE']);
@@ -370,8 +456,35 @@ class faiProfile extends plugin
       $ldap->cd($this->dn);
       $ldap->add($this->attrs);
     }
+    show_ldap_error($ldap->get_error(), _("Saving FAI profile failed"));
+
+    /* Do object tagging */
+    $this->handle_object_tagging();
     show_ldap_error($ldap->get_error());
   }
+
+  
+  /* 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: