Code

Enabled single cut and paste
[gosa.git] / plugins / admin / ogroups / class_ogroup.inc
index 3243c58552ffefc85ed3302c160eaa85e2853569..1a5db9bca3162789803ffcf7918338b605357f0f 100644 (file)
@@ -25,11 +25,12 @@ class ogroup extends plugin
   var $gosaGroupObjects= "";
   var $department= "";
   var $objects= array();
-  var $allobjects= array();
+  var $objcache= array();
   var $memberList= array();
   var $member= array();
   var $orig_dn= "";
   var $group_dialog= FALSE;
+  var $view_logged = FALSE;
 
   /* attribute list for save action */
   var $attributes= array("cn", "description", "gosaGroupObjects","member");
@@ -46,6 +47,7 @@ class ogroup extends plugin
     if (isset($this->attrs['member'])){
       foreach ($this->attrs['member'] as $key => $value){
         if ("$key" != "count"){
+          $value= @LDAP::convert($value);
           $this->member["$value"]= "$value";
         }
       }
@@ -71,6 +73,7 @@ class ogroup extends plugin
           "W" => "workstations",
           "T" => "terminals",
           "F" => "phones",
+          "_" => "subtrees",
           "P" => "printers") as $key => $val){
 
       if (preg_match("/$key/", $this->gosaGroupObjects)){
@@ -85,6 +88,13 @@ class ogroup extends plugin
      $this->base = $_SESSION['CurrentMainBase'];
     }
 
+    /* Set base */
+    if ($this->dn == "new"){
+      $this->base= $_SESSION['CurrentMainBase'];
+    } else {
+      $this->base= preg_replace("/^[^,]+,".get_groups_ou()."/","",$this->dn);
+    }
+
     /* Load member data */
     $this->reload();
   }
@@ -92,12 +102,15 @@ class ogroup extends plugin
   function AddDelMembership($NewMember = false){
 
     if($NewMember){
-      $this->memberList[$NewMember]= $this->allobjects[$NewMember];
+
+      /* Add member and force reload */
       $this->member[$NewMember]= $NewMember;
+      $this->reload(); 
+
+      $this->memberList[$NewMember]= $this->objcache[$NewMember];
       unset ($this->objects[$NewMember]);
       uasort ($this->memberList, 'sort_list');
       reset ($this->memberList);
-      $this->reload();
     }else{
       /* Delete objects from group */
       if (isset($_POST['delete_membership']) && isset($_POST['members'])){
@@ -127,10 +140,14 @@ class ogroup extends plugin
 
   function execute()
   {
-       /* Call parent execute */
-       plugin::execute();
+    /* Call parent execute */
+    plugin::execute();
+
+    if(!$this->view_logged){
+      $this->view_logged = TRUE;
+      new log("view","ogroups/".get_class($this),$this->dn);
+    }
 
-//    $this->reload();
 
     /* Do we represent a valid group? */
     if (!$this->is_account){
@@ -175,26 +192,30 @@ class ogroup extends plugin
       $smarty->assign($name."ACL",$this->getacl($name));
     }
 
-
     /* Base select dialog */
     $once = true;
     foreach($_POST as $name => $value){
-      if(preg_match("/^chooseBase/",$name) && $once){
+      if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
         $once = false;
-        $this->dialog = new baseSelectDialog($this->config,$this);
+        $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
         $this->dialog->setCurrentBase($this->base);
       }
     }
 
     /* Dialog handling */
-    if(is_object($this->dialog)){
+    if(is_object($this->dialog) && $this->acl_is_moveable()){
       /* Must be called before save_object */
       $this->dialog->save_object();
 
       if($this->dialog->isClosed()){
         $this->dialog = false;
       }elseif($this->dialog->isSelected()){
-        $this->base = $this->dialog->isSelected();
+
+        /* A new base was selected, check if it is a valid one */
+        $tmp = $this->get_allowed_bases();
+        if(isset($tmp[$this->dialog->isSelected()])){
+          $this->base = $this->dialog->isSelected();
+        }
         $this->dialog= false;
       }else{
         return($this->dialog->execute());
@@ -225,7 +246,7 @@ class ogroup extends plugin
       }
       if (isset($_POST['dselect'])){
         foreach( array("accounts", "groups", "applications", "departments",
-              "servers", "workstations", "terminals", "printers",
+              "servers", "workstations", "terminals", "printers","subtrees",
               "phones") as $type){
 
           if (isset($_POST[$type])) {
@@ -255,7 +276,7 @@ class ogroup extends plugin
       $smarty->assign("deplist", $this->config->idepartments);
       $smarty->assign("alphabet", generate_alphabet());
       foreach( array("dselect", "regex", "accounts", "groups", "applications",
-            "departments", "servers", "workstations", "terminals",
+            "departments", "servers", "workstations", "terminals","subtrees",
             "printers", "phones") as $type){
         $smarty->assign("$type", $ogfilter[$type]);
       }
@@ -267,8 +288,7 @@ class ogroup extends plugin
     }
 
     /* Bases / Departments */
-   
-      if (isset($_POST['base'])){
+      if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
         $this->base= $_POST['base'];
       }
 
@@ -301,7 +321,7 @@ class ogroup extends plugin
     }
 
     /* Assign variables */
-    $smarty->assign("bases", $this->config->idepartments);
+    $smarty->assign("bases", $this->get_allowed_bases());
     $smarty->assign("base_select", $this->base);
     $smarty->assign("department", $this->department);
     $smarty->assign("members", $this->convert_list($this->memberList));
@@ -323,7 +343,21 @@ class ogroup extends plugin
   {
     /* Save additional values for possible next step */
     if (isset($_POST['ogroupedit'])){
+
+      /* Create a base backup and reset the
+         base directly after calling plugin::save_object();
+         Base will be set seperatly a few lines below */
+      $base_tmp = $this->base;
       plugin::save_object();
+      $this->base = $base_tmp;
+
+      /* Save base, since this is no LDAP attribute */
+      $tmp = $this->get_allowed_bases();
+      if(isset($_POST['base'])){
+        if(isset($tmp[$_POST['base']])){
+          $this->base= $_POST['base'];
+        }
+      }
     }
   }
 
@@ -343,7 +377,6 @@ class ogroup extends plugin
     $ogfilter               = get_global("ogfilter");
     $regex                  = $ogfilter['regex'];
 
-    /* Get ldap connection */
     $ldap= $this->config->get_ldap_link();
     $ldap->cd ($ogfilter['dselect']);
 
@@ -352,113 +385,52 @@ class ogroup extends plugin
       Generate Filter 
       ###########*/
 
-    /* Assemble filter */
-    if ($ogfilter['accounts'] == "checked"){
-      $filter.= "(objectClass=gosaAccount)";
-      $objectClasses["gosaAccount"]     = get_people_ou();
-    }
-    if ($ogfilter['groups'] == "checked"){
-      $filter.= "(objectClass=posixGroup)";
-      $objectClasses["posixGroup"]      = get_groups_ou();
-    }
-    if ($ogfilter['applications'] == "checked"){
-      $filter.= "(objectClass=gosaApplication)";
-      $objectClasses["gosaApplication"] = "ou=apps,";
-    }
-    if ($ogfilter['departments'] == "checked"){
-      $filter.= "(objectClass=gosaDepartment)";
-      $objectClasses["gosaDepartment"]  = "";
-    }
-    if ($ogfilter['servers'] == "checked"){
-      $filter.= "(objectClass=goServer)";
-      $objectClasses["goServer"]        = "ou=servers,ou=systems,";
-    }
-    if ($ogfilter['workstations'] == "checked"){
-      $filter.= "(objectClass=gotoWorkstation)";
-      $objectClasses["gotoWorkstation"] = "ou=workstations,ou=systems,";
-    }
-    if ($ogfilter['terminals'] == "checked"){
-      $filter.= "(objectClass=gotoTerminal)";
-      $objectClasses["gotoTerminal"]    = "ou=terminals,ou=systems,";
-    }
-    if ($ogfilter['printers'] == "checked"){
-      $filter.= "(objectClass=gotoPrinter)";
-
-      $objectClasses["gotoPrinter"]     = "ou=printers,ou=systems,";
-    }
-    if ($ogfilter['phones'] == "checked"){
-      $filter.= "(objectClass=goFonHardware)";
-      $objectClasses["goFonHardware"]   = "ou=phones,ou=systems,";
-    }
+    $p_f= array("accounts"        => array("CLASS"=>"gosaAccount"    ,"DN"=> get_people_ou()                  ,"ACL" => "users"), 
+                "groups"          => array("CLASS"=>"posixGroup"     ,"DN"=> get_groups_ou()                  ,"ACL" => "groups"), 
+                "applications"    => array("CLASS"=>"gosaApplication","DN"=> "ou=apps,"                       ,"ACL" => "application"), 
+                "departments"     => array("CLASS"=>"gosaDepartment" ,"DN"=> ""                               ,"ACL" => "department"), 
+                "servers"         => array("CLASS"=>"goServer"       ,"DN"=> "ou=servers,ou=systems,"         ,"ACL" => "server"),
+                "workstations"    => array("CLASS"=>"gotoWorkstation","DN"=> "ou=workstations,ou=systems,"    ,"ACL" => "workstation"),
+                "terminals"       => array("CLASS"=>"gotoTerminal"   ,"DN"=> "ou=terminals,ou=systems,"       ,"ACL" => "terminal"),
+                "printers"        => array("CLASS"=>"gotoPrinter"    ,"DN"=> "ou=printers,ou=systems,"        ,"ACL" => "printer"),
+                "phones"          => array("CLASS"=>"goFonHardware"  ,"DN"=> "ou=phones,ou=systems,"          ,"ACL" => "phone"));
 
 
     /*###########
       Perform search for selected objectClasses & regex to fill list with objects   
       ###########*/
 
-    /* Perform search for selected objectClasses */
-    foreach($objectClasses as $class=> $basedn){
-      $ldap->ls("(&(objectClass=".$class.")(|(uid=$regex)(cn=$regex)(ou=$regex)))",$basedn.$ogfilter['dselect'] ,
-          array("dn", "cn", "description", "objectClass", "sn", "givenName", "uid","ou"));
-      
-      /* fetch results and append them to the list */
-      while($attrs = $ldap->fetch()){
-
-        $type= $this->getObjectType($attrs);
-        $name= $this->getObjectName($attrs);
+    $Get_list_flags = 0;
+    if($ogfilter['subtrees'] == "checked"){
+      $Get_list_flags |= GL_SUBSEARCH;
+    }    
 
-        /* Fill array */
-        if (isset($attrs["description"][0])){
-          $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
-        } elseif (isset($attrs["uid"][0])) {
-          $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
-        } else {
-          $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
-        }
-      }
-    }
-    uasort ($this->objects, 'sort_list');
-    reset ($this->objects);
+    foreach($p_f as $post_name => $data){
 
-    /*###########
-      Get a list with all possible objects, to detect objects which doesn't exists anymore ... 
-      ###########*/
+      if($ogfilter[$post_name] == "checked"){
+        $filter = "(&(objectClass=".$data['CLASS'].")(|(uid=$regex)(cn=$regex)(ou=$regex)))";
+        $res    = get_list($filter, $data['ACL']  , $data['DN'].$ogfilter['dselect'], 
+                    array("description", "objectClass", "sn", "givenName", "uid","ou","cn"));
 
-    /* Only do this, if this wasn't already done */
-    if(count($this->allobjects) == 0){
-      $ldap->cd ($this->config->current['BASE']);
-      $filter="(objectClass=gosaAccount)".        
-              "(objectClass=posixGroup)".
-              "(objectClass=gosaApplication)".
-              "(objectClass=gosaDepartment)".
-              "(objectClass=goServer)".
-              "(objectClass=gotoWorkstation)".
-              "(objectClass=gotoTerminal)".
-              "(objectClass=gotoPrinter)".
-              "(objectClass=goFonHardware)";
-      $regex= "*";
-
-      $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
-      while ($attrs= $ldap->fetch()){
+        /* fetch results and append them to the list */
+        foreach($res as $attrs){
 
-        $type= $this->getObjectType($attrs);
-        $name= $this->getObjectName($attrs);
+          $type= $this->getObjectType($attrs);
+          $name= $this->getObjectName($attrs);
 
-        if (isset($attrs["description"][0])){
-          $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
-        } elseif (isset($attrs["uid"][0])) {
-          $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
-        } else {
-          $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
-        }
-        $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
-        if(isset($attrs['uid'])){
-          $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
+          /* Fill array */
+          if (isset($attrs["description"][0])){
+            $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
+          } elseif (isset($attrs["uid"][0])) {
+            $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
+          } else {
+            $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
+          }
         }
       }
-      uasort ($this->allobjects, 'sort_list');
-      reset ($this->allobjects);
-    }      
+    }
+    uasort ($this->objects, 'sort_list');
+    reset ($this->objects);
 
     
     /*###########
@@ -470,61 +442,46 @@ class ogroup extends plugin
     /* Walk through all single member entry */
     foreach($this->member as $dn){
 
-      /* Object in object list? */
-      if (isset($this->allobjects[$dn])){
-        
-        /* Add this entry to member list, its dn is in allobjects
-            this means it still exists 
-         */
-        $this->memberList[$dn]= $this->allobjects[$dn];
-
-        /* Remove this from selectable entries */
-        if (isset ($this->objects[$dn])){
-          unset ($this->objects[$dn]);
-        }
+      /* The dn for the current member can't be resolved 
+         it seams that this entry was removed 
+       */ 
+      /* Try to resolv the entry again, if it still fails, display error msg */
+      $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
 
-      
-      } else {
+      /* It has failed, add entry with type flag I (Invalid)*/
+      if ($ldap->error != "Success"){
+        $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
 
-        /* The dn for the current member can't be resolved 
-            it seams that this entry was removed 
-         */ 
-        /* Try to resolv the entry again, if it still fails, display error msg */
-        $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
+      } else {
 
-        /* It has failed, add entry with type flag I (Invalid)*/
-        if ($ldap->error != "success"){
-          $this->memberList[$dn]= array('text' => _("Non existing dn:")." ".@LDAP::fix($dn),"type" => "I");
+        /* Append this entry to our all object list */
 
-        } else {
-          
-          /* Append this entry to our all object list */
-    
-          /* Fetch object */
-          $attrs= $ldap->fetch();
+        /* Fetch object */
+        $attrs= $ldap->fetch();
 
-          $type= $this->getObjectType($attrs);
-          $name= $this->getObjectName($attrs);
+        $type= $this->getObjectType($attrs);
+        $name= $this->getObjectName($attrs);
 
-          if (isset($attrs["description"][0])){
-            $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
-          } elseif (isset($attrs["uid"][0])) {
-            $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
-          } else {
-            $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
-          }
-          $this->allobjects[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
-          if(isset($attrs['uid'])){
-            $this->allobjects[$attrs["dn"]]['uid']          = $attrs['uid'];
-          }
+        if (isset($attrs["description"][0])){
+          $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
+        } elseif (isset($attrs["uid"][0])) {
+          $this->objcache[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
+        } else {
+          $this->objcache[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
+        }
+        $this->objcache[$attrs["dn"]]['objectClass']  = $attrs['objectClass'];
+        if(isset($attrs['uid'])){
+          $this->objcache[$attrs["dn"]]['uid']          = $attrs['uid'];
+        }
 
-          /* Fill array */
-          if (isset($attrs["description"][0])){
-            $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
-          } else {
-            $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
-          }
+        /* Fill array */
+        if (isset($attrs["description"][0])){
+          $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
+        } else {
+          $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
         }
+
+        $this->memberList[$dn]= $this->objects[$attrs["dn"]];
       }
     }
     uasort ($this->memberList, 'sort_list');
@@ -564,7 +521,7 @@ class ogroup extends plugin
 
     foreach ($input as $key => $value){
       /* Generate output */
-      $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
+      $temp.= "<option title='".addslashes( $key)."' value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
     }
 
     return ($temp);
@@ -638,13 +595,13 @@ class ogroup extends plugin
       $message[]= _("There is already an object with this cn.");
     } 
 
-    print_a($this);
-    if ($this->acl_is_createable()){
-      $message[]= _("You have no permissions to create a group on this 'Base'.");
+    /* Set new acl base */
+    if($this->dn == "new") {
+      $this->set_acl_base($this->base);
     }
 
     /* must: cn */
-    if ($this->cn == "" && $this->acl_is_writeable("cn")){
+    if ($this->cn == ""){
       $message[]= "The required field 'Name' is not set.";
     }
 
@@ -665,7 +622,7 @@ class ogroup extends plugin
     /* Move members to target array */
     $this->attrs['member'] =array();
     foreach ($this->member as $key => $desc){
-      $this->attrs['member'][]= $key;
+      $this->attrs['member'][]= @LDAP::fix($key);
     }
 
     $ldap= $this->config->get_ldap_link();
@@ -684,6 +641,7 @@ class ogroup extends plugin
         $this->attrs['member']= array();
       }
       $mode= "modify";
+
     } else {
       $mode= "add";
       $ldap->cd($this->config->current['BASE']);
@@ -695,6 +653,12 @@ class ogroup extends plugin
     $this->cleanup();
     $ldap->$mode($this->attrs);
 
+    if($mode == "add"){
+      new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+    }else{
+      new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+    }
+
     /* Trigger post signal */
     $this->handle_post_events($mode);
 
@@ -714,16 +678,52 @@ class ogroup extends plugin
     $ldap->rmdir($this->dn);
     show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
 
+    new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
+
     /* Trigger remove signal */
     $this->handle_post_events("remove");
   }
 
+  
+  function PrepareForCopyPaste($source)
+  {
+    /* Update available object types */
+    if(isset($source['gosaGroupObjects'][0])){
+      $this->gosaGroupObjects =  $source['gosaGroupObjects'][0];
+    }
+
+    /* Reload tabs */
+    $this->parent->reload($this->gosaGroupObjects );
+   
+    /* Reload plugins */ 
+    foreach($this->parent->by_object as $name => $class ){
+      if(get_class($this) != $name) {
+        $this->parent->by_object[$name]->PrepareForCopyPaste($source);
+      }
+    }
+
+    /* Load member objects */
+    if (isset($source['member'])){
+      foreach ($source['member'] as $key => $value){
+        if ("$key" != "count"){
+          $value= @LDAP::convert($value);
+          $this->member["$value"]= "$value";
+        }
+      }
+    }
+
+  }
+
+
   function getCopyDialog()
   {
-    $str  = "";
-    $str .= _("Group name");
-    $str .= "&nbsp;<input type='text' name='cn' value='".$this->cn."'>";
-    return($str);
+    $smarty = get_smarty();
+    $smarty->assign("cn",     $this->cn);
+    $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
+    $ret = array();
+    $ret['string'] = $str;
+    $ret['status'] = "";
+    return($ret);
   }
 
   function saveCopyDialog()
@@ -741,7 +741,7 @@ class ogroup extends plugin
           "plDescription" => _("Object group generic"),
           "plSelfModify"  => FALSE,
           "plDepends"     => array(),
-          "plPriority"    => 0,
+          "plPriority"    => 1,
           "plSection"     => array("administration"),
           "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
                                                       "objectClass"  => "gosaGroupOfNames")),