Code

Fixed removal of template and profile entries.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiTemplate.inc
index 9015ba8789f9e1f853ef3ff95c591cd0a0a77501..47313ac420467f4357e809df160d771ce8f86a6e 100644 (file)
@@ -46,43 +46,37 @@ class faiTemplate extends plugin
     if($dn != "new"){
       $this->dn =$dn;
 
-      /* Get FAIstate
-       */
-      if(isset($this->attrs['FAIstate'][0])){
-        $this->FAIstate = $this->attrs['FAIstate'][0];
-      }
-
       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
        */
-      $ldap     = $this->config->get_ldap_link();
-      $ldap->cd ($this->dn);
-    
-      $attrs_to_search = $this->subAttributes;
-      $attrs_to_search[] = "FAIstate";
-      $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
-
-      $data = array();
-      while($object = $ldap->fetch()){
-        $data[] = $object;
-      }
-      foreach($data as $object){
+      $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+      foreach($res as $obj){
 
-        /* Skip objects, that are tagged as removed */
-        if(isset($object['FAIstate'][0])){
-          if(preg_match("/removed$/",$object['FAIstate'][0])){
-            continue;
-          }
-        }
+        /* Skip not relevant objects */
+        if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
 
-        /* Set status for save management */
         $objects = array();
         $objects['status']      = "FreshLoaded";
-        $objects['dn']          = $object['dn'];
+        $objects['dn']          = $obj['dn'];
         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
         $this->SubObjects[$objects['cn']] = $objects;
       }
     }
+    $this->is_new = FALSE;
+    if($this->dn == "new"){
+      $this->is_new =TRUE;
+    }
     $this->ui = get_userinfo();
+
+    // Prepare lists
+    $this->templateList = new sortableListing();
+    $this->templateList->setInstantDelete(false);
+    $this->templateList->setDeleteable(true);
+    $this->templateList->setEditable(true);
+    $this->templateList->setWidth("100%");
+    $this->templateList->setHeight("220px");
+    $this->templateList->setColspecs(array('*','18px'));
+    $this->templateList->setHeader(array(_("Name"),""));
+    $this->templateList->setDefaultSortColumn(0);
   }
 
 
@@ -107,8 +101,6 @@ class faiTemplate extends plugin
         if(in_array_ics($attrs,$this->subBinary)){
           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
         }
-
-        $var = stripslashes($var);
   
         $object[$attrs] = $var;
       }
@@ -133,80 +125,83 @@ class faiTemplate extends plugin
 
     /* New Listhandling
      */
-    $once = true;
-    foreach($_POST as $name => $value){
-      if(preg_match("/^editscript_/",$name)&&($once)){
-        $once = false;
-        $entry = preg_replace("/^editscript_/","",$name);
-        $entry = base64_decode(preg_replace("/_.*/","",$entry));
-        $obj  = $this->SubObjects[$entry];
-
+    $this->templateList->save_object();
+    $action = $this->templateList->getAction();
+    if($action['action'] == 'delete'){
+        $id = $this->templateList->getKey($action['targets'][0]);
+        $obj  = $this->SubObjects[$id];
         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
-        if(preg_match("/r/",$acl)){
-          if($obj['status'] == "FreshLoaded"){
-            $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
-          }
-          $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
-          $this->dialog->set_acl_base($this->acl_base);
-          $this->dialog->set_acl_category("fai");
-
-          session::set('objectinfo',$obj['dn']);
-          $this->dialog->parent = &$this;
-          $this->is_dialog=true;
+        if(preg_match("/d/",$acl)){
+            $status = $this->SubObjects[$id]['status'];
+            if($status == "edited" || $status == "FreshLoaded"){
+                $this->SubObjects[$id]['status']= "delete";
+            }else{
+                unset($this->SubObjects[$id]);
+            }
         }
-      }
-      if(preg_match("/^deletescript_/",$name)&&($once)){
-        $once = false;
-        $entry = preg_replace("/^deletescript_/","",$name);
-        $entry = base64_decode(preg_replace("/_.*/","",$entry));
-        $obj  = $this->SubObjects[$entry];
-
+    }
+    if($action['action'] == 'edit'){
+        $id = $this->templateList->getKey($action['targets'][0]);
+        $obj  = $this->SubObjects[$id];
         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
-        if(preg_match("/d/",$acl)){
-          $status = $this->SubObjects[$entry]['status'];
-          if($status == "edited" || $status == "FreshLoaded"){
-            $this->SubObjects[$entry]['status']= "delete";
-          }else{
-            unset($this->SubObjects[$entry]);
-          }
+        if(preg_match("/r/",$acl)){
+            if($obj['status'] == "FreshLoaded"){
+                $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
+            }
+            $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
+            $this->dialog->set_acl_base($this->acl_base);
+            $this->dialog->FAIstate = $this->FAIstate;
+            $this->dialog->set_acl_category("fai");
+
+            set_object_info($obj['dn']);
+            $this->dialog->parent = &$this;
+            $this->is_dialog=true;
         }
-      }
     }
 
-    /* File download requested */
-    if(isset($_GET['getFAItemplate'])){
-      if(isset($this->SubObjects[$_GET['getFAItemplate']])){
-        $obj = $this->SubObjects[$_GET['getFAItemplate']];
+    $once = true;
+    foreach($_POST as $name => $value){
+      if(preg_match("/^download_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
+        $once = false;
+        $entry = postDecode(preg_replace("/^download_/","",$name));
+        $obj  = $this->SubObjects[$entry];
         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
-        send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
-      }
+        if(!isset($obj['cn'])){
+          trigger_error("Something wen't wrong here!");
+        }else{
+          send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
+        }
+      } 
     }
 
     /* Edit entries via GET */
     if(isset($_GET['act']) && isset($_GET['id'])){
       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
         $obj = $this->SubObjects[$_GET['id']];
-          if($obj['status'] == "FreshLoaded"){
+        if($obj['status'] == "FreshLoaded"){
           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
         }
         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
+        $this->dialog->FAIstate = $this->FAIstate;
         $this->dialog->acl = $this->acl;
-        session::set('objectinfo',$obj['dn']);
+        $this->dialog->set_acl_category("fai");
+        set_object_info($obj['dn']);
         $this->dialog->parent = &$this;
         $this->is_dialog=true;
       }
     }
 
     /* Add new sub object */
-    if(isset($_POST['AddSubObject'])){
-      $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
+    if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
+      $acl_dn = $this->acl_base_for_current_object($this->dn);
       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
 
       if(preg_match("/c/",$acl)){
         $this->dialog= new $this->subClassName($this->config,"new");
         $this->dialog->set_acl_base($this->acl_base);
+        $this->dialog->FAIstate = $this->FAIstate;
         $this->dialog->set_acl_category("fai");
         $this->dialog->parent = &$this;
         $this->is_dialog=true;
@@ -214,7 +209,7 @@ class faiTemplate extends plugin
     }
 
     if($this->dn != "new"){
-      session::set('objectinfo',$this->dn);
+      set_object_info($this->dn);
     }
 
     /* Save Dialog */
@@ -278,67 +273,43 @@ class faiTemplate extends plugin
       return($display);
     }
 
-     /* Divlist Containing FAItemplates */
-    $divlist = new divSelectBox("FAItemplates");
-    $divlist->setHeight(400);
+     /* list Containing FAItemplates */
+    $dn  = $this->acl_base_for_current_object($this->dn);
+    $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
+    $this->templateList->setAcl($acl,preg_match('/^freeze/', $this->FAIstate));
 
     $tmp = $this->getList(true);
-  
-    /* Create div list with all sub entries listed */
+    $data = $lData = array();
     foreach($this->SubObjects as $key => $name){
 
       /* Skip removed entries */ 
       if($name['status'] == "delete") continue;
 
-      /* Get permissions */
-      $dn  = $this->acl_base_for_current_object($name['dn']);
-      $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
       $act = "";
 
-      /* Hide delete icon if this object is freezed */
-      if(preg_match("/freeze/",$this->FAIstate)){
-        $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
-      }else{
-        $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
-        if(preg_match("/d/",$acl)){
-          $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
-        }
-      }
-
       /* Check acls for download icon */
+      $dn  = $this->acl_base_for_current_object($name['dn']);
       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
-      if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
+      if(($this->SubObjects[$key]['status'] == "new") || 
+              ($this->SubObjects[$key]['dn'] == "new") || 
+              !preg_match("/r/",$s_acl)){
         $down = "";
       }else{
-        $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
-          <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
-          </a>";
-      }
-
-      /* Check if we are allowed to view this object */
-      $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
-      if(preg_match("/r/",$s_acl)){
-
-        $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$tmp[$key]."</a>";
-        $divlist->AddEntry(array( array("string"=> $edit_link), 
-              array("string"=>$down , "attach" => "style='width:20px;'"),
-              array("string"=>str_replace("%s",base64_encode($key),$act),
-                "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
+        $down = image('images/save.png','download_'.postEncode($key), _("Download"));
       }
+      $data[$key] = $key;
+      $lData[$key] = array('data' => array($name['cn'], $down));
     }
-    $smarty->assign("Entry_divlist",$divlist->DrawList());
-    /* Divlist creation complete
-     */
+
+    $this->templateList->setListData($data,$lData);
+    $this->templateList->update();
+    $smarty->assign("Entry_listing",$this->templateList->render());
 
      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
      * If we post the escaped strings they will be escaped again
      */
     foreach($this->attributes as $attrs){
-      if(get_magic_quotes_gpc()){
-        $smarty->assign($attrs,stripslashes($this->$attrs));
-      }else{
-        $smarty->assign($attrs,($this->$attrs));
-      }
+      $smarty->assign($attrs,set_post($this->$attrs));
     }
 
     $dn = $this->acl_base_for_current_object($this->dn);
@@ -347,7 +318,7 @@ class faiTemplate extends plugin
             !preg_match("/freeze/",$this->FAIstate));
 
     foreach($this->attributes as $attr){
-      $smarty->assign($attr."ACL",$this->getacl($attr));
+      $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
     }
 
     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
@@ -357,9 +328,9 @@ class faiTemplate extends plugin
 
   function acl_base_for_current_object($dn)
   {
-    if($dn == "new"){
+    if($dn == "new" || $dn == ""){
       if($this->dn == "new"){
-        $dn = session::get('CurrentMainBase');
+        $dn = $dn= $this->parent->parent->acl_base;
       }else{
         $dn = $this->dn;
       }
@@ -392,26 +363,20 @@ class faiTemplate extends plugin
    */
   function remove_from_parent()
   {
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd ($this->dn);
-
-    $faifilter = session::get('faifilter');
-    $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
-    if($faifilter['branch'] == "main"){
-      $use_dn = $this->dn;
-    }
-
-    FAI::prepare_to_save_FAI_object($use_dn,array(),true);
-    new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
+    if($this->acl_is_removeable()){
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cd ($this->dn);
+      $release = $this->parent->parent->fai_release;
+      $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn, '/'))."/i", $release, $this->dn);
+      new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
+      FAI::prepare_to_save_FAI_object($use_dn,array(),true);
 
-    foreach($this->SubObjects as $name => $obj){
-      $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
-      if($faifilter['branch'] == "main"){
-        $use_dn = $obj['dn'];
+      foreach($this->SubObjects as $name => $obj){
+        $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
+        FAI::prepare_to_save_FAI_object($use_dn,array(),true);
       }
-      FAI::prepare_to_save_FAI_object($use_dn,array(),true);
+      $this->handle_post_events("remove");
     }
-    $this->handle_post_events("remove");    
   }
 
 
@@ -431,6 +396,17 @@ class faiTemplate extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    /* Ensure that we do not overwrite an allready existing entry 
+     */
+    if($this->is_new){
+      $release = $this->parent->parent->fai_release;
+      $new_dn= 'cn='.$this->cn.",".get_ou("faiTemplate", "faiTemplateRDN").get_ou("faiManagement", "faiBaseRDN").$release;
+      $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
+      if(isset($res[$this->cn])){
+        $message[] = msgPool::duplicated(_("Name"));
+      }
+    }
+
     return ($message);
   }
 
@@ -443,9 +419,6 @@ class faiTemplate extends plugin
     $ldap = $this->config->get_ldap_link();
 
     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
-    if (!$ldap->success()){
-      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
-    }
 
     if($this->initially_was_account){
       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
@@ -459,18 +432,19 @@ class faiTemplate extends plugin
      * After deletion, we perform add and modify 
      */
     $Objects = array();
-    foreach($this->SubObjects as $name => $obj){
+    $tmp = $this->SubObjects;
+    foreach($tmp as $name => $obj){
       if($obj['status'] == "FreshLoaded"){
-        unset($this->SubObjects[$name]);
+        unset($tmp[$name]);
       }
     }
 
-    foreach($this->SubObjects as $name => $obj){
+    foreach($tmp as $name => $obj){
       if($obj['status'] == "delete"){
         $Objects[$name] = $obj; 
       }
     }
-    foreach($this->SubObjects as $name => $obj){
+    foreach($tmp as $name => $obj){
       if($obj['status'] != "delete"){
         $Objects[$name] = $obj; 
       }
@@ -484,7 +458,7 @@ class faiTemplate extends plugin
       $tmp = array();
       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
       foreach($attributes as $attrs){
-        if(empty($obj[$attrs])){
+        if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
           $obj[$attrs] = array();
         }
         $tmp[$attrs] =($obj[$attrs]);
@@ -514,7 +488,6 @@ class faiTemplate extends plugin
         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
         $this->handle_post_events("add");
       }
-
     }
   }
 
@@ -525,29 +498,17 @@ class faiTemplate extends plugin
 
     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
      */
-    $ldap     = $this->config->get_ldap_link();
-    $ldap->cd ($source['dn']);
-
-    $attrs_to_search = $this->subAttributes;
-    $attrs_to_search[] = "FAIstate";
-    $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
-
-    while($object = $ldap->fetch()){
+    $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
+    foreach($res as $obj){
 
-      /* Skip objects, that are tagged as removed */
-      if(isset($object['FAIstate'][0])){
-        if(preg_match("/removed$/",$object['FAIstate'][0])){
-          continue;
-        }
-      }
+      /* Skip not relevant objects */
+      if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
 
-      /* Set status for save management */
       $objects = array();
       $objects['status']      = "edited";
-      $objects['dn']          = $object['dn'];
+      $objects['dn']          = $obj['dn'];
       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
-
       $this->SubObjects[$objects['cn']] = $objects;
     }
   }
@@ -564,11 +525,51 @@ class faiTemplate extends plugin
           "plPriority"    => 24,
           "plSection"     => array("administration"),
           "plCategory"    => array("fai"),
+          "plProperties" =>
+          array(
+              array(
+                  "name"          => "faiTemplateRDN",
+                  "type"          => "rdn",
+                  "default"       => "ou=templates,",
+                  "description"   => _("RDN for FAI template storage."),
+                  "check"         => "gosaProperty::isRdn",
+                  "migrate"       => "migrate_faiTemplateRDN",
+                  "group"         => "plugin",
+                  "mandatory"     => TRUE)),
+
           "plProvidedAcls" => array(
-            "cn"                => _("Name")." ("._("Readonly").")",
+              "cn"                => _("Name")." ("._("Read-only").")",
             "description"       => _("Description"))
           ));
   }
+
+
+  /*! \brief  Used for copy & paste.
+    Returns a HTML input mask, which allows to change the cn of this entry.
+    @param  Array   Array containing current status && a HTML template.
+   */
+  function getCopyDialog()
+  {
+    $vars = array("cn");
+    $smarty = get_smarty();
+    $smarty->assign("cn", set_post($this->cn));
+    $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
+  }
+
+
+  /*! \brief  Used for copy & paste.
+    Some entries must be renamed to avaoid duplicate entries.
+   */
+  function saveCopyDialog()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = get_post('cn');
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: