Code

Updated css und div framework.
[gosa.git] / plugins / admin / ogroups / class_ogroup.inc
index bbb1ada80b4feacde94928f4fa6b9f93ce13b901..39ae7ad1fce711f2fab35601a66a0407e06d98a8 100644 (file)
@@ -25,14 +25,14 @@ 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;
 
   /* attribute list for save action */
-  var $attributes= array("cn", "description", "gosaGroupObjects");
+  var $attributes= array("cn", "description", "gosaGroupObjects","member");
   var $objectclasses= array("top", "gosaGroupOfNames");
 
   function ogroup ($config, $dn= NULL)
@@ -40,6 +40,8 @@ class ogroup extends plugin
     plugin::plugin ($config, $dn);
     $this->orig_dn= $dn;
 
+    $this->member = array();
+
     /* Load member objects */
     if (isset($this->attrs['member'])){
       foreach ($this->attrs['member'] as $key => $value){
@@ -69,6 +71,7 @@ class ogroup extends plugin
           "W" => "workstations",
           "T" => "terminals",
           "F" => "phones",
+          "_" => "subtrees",
           "P" => "printers") as $key => $val){
 
       if (preg_match("/$key/", $this->gosaGroupObjects)){
@@ -79,16 +82,67 @@ class ogroup extends plugin
     }
     register_global("ogfilter", $ogfilter);
   
-    if(isset($_SESSION['ogroupfilter']['depselect'])){
-     $this->base = $_SESSION['ogroupfilter']['depselect'];
+    if(isset($_SESSION['CurrentMainBase'])){
+     $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();
   }
 
+  function AddDelMembership($NewMember = false){
+
+    if($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);
+    }else{
+      /* Delete objects from group */
+      if (isset($_POST['delete_membership']) && isset($_POST['members'])){
+        foreach ($_POST['members'] as $value){
+          $this->objects["$value"]= $this->memberList[$value];
+          unset ($this->memberList["$value"]);
+          unset ($this->member["$value"]);
+          uasort ($this->objects, 'sort_list');
+          reset ($this->objects);
+        }
+        $this->reload();
+      }
+
+      /* Add objects to group */
+      if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
+        foreach ($_POST['objects'] as $value){
+          $this->memberList["$value"]= $this->objects[$value];
+          $this->member["$value"]= $value;
+          unset ($this->objects[$value]);
+          uasort ($this->memberList, 'sort_list');
+          reset ($this->memberList);
+        }
+        $this->reload();
+      }
+    }
+  }
+
   function execute()
   {
+       /* Call parent execute */
+       plugin::execute();
+
+//    $this->reload();
+
     /* Do we represent a valid group? */
     if (!$this->is_account){
       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
@@ -96,16 +150,16 @@ class ogroup extends plugin
       return ($display);
     }
 
-
-
     /* Delete objects from group */
     if (isset($_POST['delete_membership']) && isset($_POST['members'])){
       foreach ($_POST['members'] as $value){
-        $this->objects["$value"]= $this->memberList[$value];
-        unset ($this->memberList["$value"]);
-        unset ($this->member["$value"]);
-        uasort ($this->objects, 'sort_list');
-        reset ($this->objects);
+        if(isset($this->memberList[$value])){
+          $this->objects["$value"]= $this->memberList[$value];
+          unset ($this->memberList["$value"]);
+          unset ($this->member["$value"]);
+          uasort ($this->objects, 'sort_list');
+          reset ($this->objects);
+        }
       }
       $this->reload();
     }
@@ -113,11 +167,13 @@ class ogroup extends plugin
     /* Add objects to group */
     if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
       foreach ($_POST['objects'] as $value){
-        $this->memberList["$value"]= $this->objects[$value];
-        $this->member["$value"]= $value;
-        unset ($this->objects[$value]);
-        uasort ($this->memberList, 'sort_list');
-        reset ($this->memberList);
+        if(isset($this->objects[$value])){
+          $this->memberList["$value"]= $this->objects[$value];
+          $this->member["$value"]= $value;
+          unset ($this->objects[$value]);
+          uasort ($this->memberList, 'sort_list');
+          reset ($this->memberList);
+        }
       }
       $this->reload();
     }
@@ -125,6 +181,41 @@ class ogroup extends plugin
     /* Load templating engine */
     $smarty= get_smarty();
 
+    $tmp = $this->plInfo();
+    foreach($tmp['plProvidedAcls'] as $name => $translation){
+      $smarty->assign($name."ACL",$this->getacl($name));
+    }
+
+    /* Base select dialog */
+    $once = true;
+    foreach($_POST as $name => $value){
+      if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_moveable()){
+        $once = false;
+        $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
+        $this->dialog->setCurrentBase($this->base);
+      }
+    }
+
+    /* Dialog handling */
+    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()){
+
+        /* 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());
+      }
+    }
+
     /* Add objects? */
     if (isset($_POST["edit_membership"])){
       $this->group_dialog= TRUE;
@@ -149,7 +240,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])) {
@@ -179,7 +270,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]);
       }
@@ -191,8 +282,7 @@ class ogroup extends plugin
     }
 
     /* Bases / Departments */
-   
-      if (isset($_POST['base'])){
+      if ((isset($_POST['base'])) && ($this->acl_is_moveable())){
         $this->base= $_POST['base'];
       }
 
@@ -225,7 +315,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));
@@ -236,12 +326,6 @@ class ogroup extends plugin
     /* Fields */
     foreach ($this->attributes as $val){
       $smarty->assign("$val", $this->$val);
-      $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
-    }
-
-    /* Assign ACL's */
-    foreach (array("base", "members") as $val){
-      $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
     }
 
     return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
@@ -253,12 +337,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;
 
-      if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
-        $this->base= $_POST["base"];
+      /* 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'];
+        }
       }
-
     }
   }
 
@@ -266,120 +359,123 @@ class ogroup extends plugin
   /* (Re-)Load objects */
   function reload()
   {
-    /* Generate object list */
-    $this->objects= array();
-    $this->allobjects= array();
-    $ldap= $this->config->get_ldap_link();
-
-    /* Assemble filter */
-    $ogfilter= get_global("ogfilter");
+    /*###########
+      Variable initialisation 
+      ###########*/
+
+    $this->objects                = array();
+    $this->ui                     = get_userinfo();
+    $filter                       = "";
+    $objectClasses                = array();
+    
+    $ogfilter               = get_global("ogfilter");
+    $regex                  = $ogfilter['regex'];
 
+    $ldap= $this->config->get_ldap_link();
     $ldap->cd ($ogfilter['dselect']);
 
-    $filter= "";
-    if ($ogfilter['accounts'] == "checked"){
-      $filter.= "(objectClass=gosaAccount)";
-    }
-    if ($ogfilter['groups'] == "checked"){
-      $filter.= "(objectClass=posixGroup)";
-    }
-    if ($ogfilter['applications'] == "checked"){
-      $filter.= "(objectClass=gosaApplication)";
-    }
-    if ($ogfilter['departments'] == "checked"){
-      $filter.= "(objectClass=gosaDepartment)";
-    }
-    if ($ogfilter['servers'] == "checked"){
-      $filter.= "(objectClass=goServer)";
-    }
-    if ($ogfilter['workstations'] == "checked"){
-      $filter.= "(objectClass=gotoWorkstation)";
-    }
-    if ($ogfilter['terminals'] == "checked"){
-      $filter.= "(objectClass=gotoTerminal)";
-    }
-    if ($ogfilter['printers'] == "checked"){
-      $filter.= "(objectClass=gotoPrinter)";
-    }
-    if ($ogfilter['phones'] == "checked"){
-      $filter.= "(objectClass=goFonHardware)";
-    }
-    $regex= $ogfilter['regex'];
 
-    $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
-    while ($attrs= $ldap->fetch()){
-
-      /* Get type */
-      $type= $this->getObjectType($attrs);
-      $name= $this->getObjectName($attrs);
-
-      /* 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);
+    /*###########
+      Generate Filter 
+      ###########*/
 
-    $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= "*";
+    $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"));
 
-    $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
-    while ($attrs= $ldap->fetch()){
 
-      /* Get type */
-      $type= $this->getObjectType($attrs);
-      $name= $this->getObjectName($attrs);
+    /*###########
+      Perform search for selected objectClasses & regex to fill list with objects   
+      ###########*/
 
-      /* Fill array */
-      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");
-      }
-    }
-    uasort ($this->allobjects, 'sort_list');
-    reset ($this->allobjects);
+    $Get_list_flags = 0;
+    if($ogfilter['subtrees'] == "checked"){
+      $Get_list_flags |= GL_SUBSEARCH;
+    }    
 
-    /* Build member list */
-    $this->memberList= array();
-    foreach($this->member as $dn){
+    foreach($p_f as $post_name => $data){
 
-      /* Object in object list? */
-      if (isset($this->allobjects[$dn])){
-        $this->memberList[$dn]= $this->allobjects[$dn];
-        if (isset ($this->objects[$dn])){
-          unset ($this->objects[$dn]);
-        }
+      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"));
 
-      } else {
+        /* fetch results and append them to the list */
+        foreach($res as $attrs){
 
-        /* No, try to ge informations from LDAP */
-        $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
-        if ($ldap->error != "success"){
-          $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
-              "type" => "I");
-        } else {
-          $ldap->cat($dn);
-          $attrs= $ldap->fetch();
           $type= $this->getObjectType($attrs);
           $name= $this->getObjectName($attrs);
 
           /* 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);
+
+    
+    /*###########
+      Build member list and try to detect obsolete entries 
+      ###########*/
+
+    $this->memberList = array();
+  
+    /* Walk through all single member entry */
+    foreach($this->member as $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"));
+
+      /* 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");
+
+      } else {
+
+        /* Append this entry to our all object list */
 
+        /* Fetch object */
+        $attrs= $ldap->fetch();
+
+        $type= $this->getObjectType($attrs);
+        $name= $this->getObjectName($attrs);
+
+        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");
         }
+
+        $this->memberList[$dn]= $this->objects[$attrs["dn"]];
       }
     }
     uasort ($this->memberList, 'sort_list');
@@ -419,7 +515,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);
@@ -452,6 +548,7 @@ class ogroup extends plugin
   function getObjectName($attrs)
   {
     /* Person? */
+    $name =""; 
     if (in_array('gosaAccount', $attrs['objectClass'])){
       if(isset($attrs['sn']) && isset($attrs['givenName'])){
         $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
@@ -472,7 +569,8 @@ class ogroup extends plugin
 
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
 
     /* Permissions for that base? */
     if ($this->base != ""){
@@ -481,21 +579,24 @@ class ogroup extends plugin
       $new_dn= $this->dn;
     }
 
-    $ui= get_userinfo();
-    $acl= get_permissions ($new_dn, $ui->subtreeACL);
-    $acl= get_module_permission($acl, "group", $new_dn);
-    if (chkacl($acl, "create") != ""){
-      $message[]= _("You have no permissions to create a group on this 'Base'.");
+
+    $ldap = $this->config->get_ldap_link();
+    if($this->dn != $new_dn){
+      $ldap->cat ($new_dn, array('dn'));
     }
+    
+    if($ldap->count() !=0){
+      $message[]= _("There is already an object with this cn.");
+    } 
 
-    /* must: cn */
-    if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
-      $message[]= "The required field 'Name' is not set.";
+    /* Set new acl base */
+    if($this->dn == "new") {
+      $this->set_acl_base($this->base);
     }
 
-    /* Need at least one member */
-    if (count($this->member) == 0){
-      $message[]= _("Object groups need at least one member!");
+    /* must: cn */
+    if ($this->cn == ""){
+      $message[]= "The required field 'Name' is not set.";
     }
 
     /* To many different object types? */
@@ -513,8 +614,9 @@ class ogroup extends plugin
     plugin::save();
 
     /* 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();
@@ -526,7 +628,7 @@ class ogroup extends plugin
 
     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
        new entries. So do a check first... */
-    $ldap->cat ($this->dn);
+    $ldap->cat ($this->dn, array('dn'));
     if ($ldap->fetch()){
       /* Modify needs array() to remove values :-( */
       if (!count ($this->member)){
@@ -541,13 +643,14 @@ class ogroup extends plugin
 
     /* Write back to ldap */
     $ldap->cd($this->dn);
+    $this->cleanup();
     $ldap->$mode($this->attrs);
 
     /* Trigger post signal */
     $this->handle_post_events($mode);
 
     $ret= 0;
-    if (show_ldap_error($ldap->get_error())){
+    if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of object group/generic with dn '%s' failed."),$this->dn))){
       $ret= 1;
     }
 
@@ -560,12 +663,50 @@ class ogroup extends plugin
 
     $ldap= $this->config->get_ldap_link();
     $ldap->rmdir($this->dn);
-    show_ldap_error($ldap->get_error());
+    show_ldap_error($ldap->get_error(), sprintf(_("Removing of object group/generic with dn '%s' failed."),$this->dn));
 
     /* Trigger remove signal */
     $this->handle_post_events("remove");
   }
 
+
+  function getCopyDialog()
+  {
+    $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()
+  {
+    if(isset($_POST['cn'])){
+      $this->cn = $_POST['cn'];
+    }
+  }
+
+
+  function plInfo()
+  {
+    return (array(
+          "plShortName"   => _("Generic"),
+          "plDescription" => _("Object group generic"),
+          "plSelfModify"  => FALSE,
+          "plDepends"     => array(),
+          "plPriority"    => 1,
+          "plSection"     => array("administration"),
+          "plCategory"    => array("ogroups" => array("description"  => _("Object groups"),
+                                                      "objectClass"  => "gosaGroupOfNames")),
+          "plProvidedAcls"=> array(
+            "cn"                => _("Name"),
+            "base"              => _("Base"),
+            "description"       => _("Description"),
+            "member"            => _("Member"))
+          ));
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: