Code

Shared Folder- Fixed handling of costum added acls .
[gosa.git] / plugins / admin / groups / class_groupMail.inc
index 9bd73c3dffedad7be86e6ebe76c66536e78af1ff..edde1af9411fabee4e29d35906e97df24c55fa2b 100644 (file)
@@ -2,13 +2,9 @@
 
 class mailgroup extends plugin
 {
-  /* CLI vars */
-  var $cli_summary        = "Manage mail groups/shared folders";
-  var $cli_description    = "Some longer text\nfor help";
-  var $cli_parameters     = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   var $uid                        = "";       // User id 
   var $cn                         = "";       // cn
+  var $orig_cn                    = "";       // cn
 
   var $method                     = "mailMethod"; // Used Mail method 
   var $mmethod                    = "";           // Contains the gosa.conf MAILMETHOD
@@ -24,7 +20,7 @@ class mailgroup extends plugin
   var $gosaVacationMessage        = "";       // Vocation message 
 
   var $imapacl                    = array('anyone'    => 'p',     // Set acls for everyone
-      '%members%' => 'lrsp',  // %members% are all group-members
+      '%members%' => 'lrspw',  // %members% are all group-members
       ''          => 'p');    // Every user added gets this right
 
 
@@ -40,12 +36,16 @@ class mailgroup extends plugin
   var $perms                      = array();
   var $gosaMailDeliveryMode       = "[L        ]";   // 
   var $gosaMailMaxSize            = "";       // 
-
+  
+  var $remove_folder_from_imap    = true;
 
   /* Helper */
   var $indexed_acl= array();
   var $indexed_user= array();
 
+  /* Copy & paste */
+  var $CopyPasteVars          = array("quotaUsage","imapacl");
+
   /* attribute list for save action */
   var $attributes= array( "mail",   "gosaMailServer", "gosaMailQuota", "gosaMailMaxSize",
       "gosaMailAlternateAddress", "gosaMailForwardingAddress",
@@ -54,12 +54,17 @@ class mailgroup extends plugin
 
   var $objectclasses= array("gosaMailAccount");
 
+  var $kolabFolderType_SubType = "";
+  var $kolabFolderType_Type = "";
 
-  function mailgroup ($config, $dn= NULL, $ui= NULL)
+  function mailgroup ($config, $dn= NULL, $parent= NULL)
   {
     /* Initialise all available attributes ... if possible
      */
-    plugin::plugin($config, $dn);
+    plugin::plugin($config, $dn, $parent);
+
+    /* Save initial cn */
+    $this->orig_cn = $this->cn;
 
     /* Set mailMethod to the one defined in gosa.conf 
      */
@@ -87,6 +92,14 @@ class mailgroup extends plugin
       $this->uid= $this->attrs['cn'][0];
     }
 
+    /* Get folder type */
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+      if(isset($this->attrs['kolabFolderType'])){
+        $tmp = split("\.",$this->attrs['kolabFolderType'][0]);
+        $this->kolabFolderType_Type = $tmp[0];
+        $this->kolabFolderType_SubType = $tmp[1];
+      }
+    }
 
     /* If this ins't new mailgroup, read all required data from ldap
      */
@@ -170,7 +183,7 @@ class mailgroup extends plugin
           }else{
             $this->quotaUsage     = "";
             $this->gosaMailQuota  = "";
-            print_red(sprintf(_("Can't get quota for for '%s'."),$this->uid));
+//            print_red(sprintf(_("Can't get quota information for '%s'."),$this->uid));
           }
           $method->disconnect();
         }   // ENDE $method->connect($this->attrs["gosaMailServer"][0])){
@@ -192,7 +205,7 @@ class mailgroup extends plugin
 
     /* Load permissions */
     $tmp = array();
-    if(preg_match("/kolab/i",$this->mmethod)){
+    if(preg_match("/olab/i",$this->mmethod)){
       $ldap = $this->config->get_ldap_link();
 
       if (isset($this->attrs['acl'])){
@@ -223,69 +236,80 @@ class mailgroup extends plugin
           }
         }
       }
-
-      /* In this section we dectect which acl is tho most used 
-         This will be used as %members% acl  
-       */
-      $tmp2 = array(); 
-      foreach($tmp as $acl => $user){
-        $tmp2[count($tmp[$acl])]=$acl;
-      }
-      /* Most used at last 
-       */
-      ksort($tmp2);      
-  
-      /* Assign last (most used acl) to %members% acl 
-       */
-      $str = array_pop($tmp2);
-      if(!empty($str)) {
-        $this->imapacl['%members%']=$str;
-      }
-
-      /* Open ldap connection 
-       */
-      $ldap = $this->config->get_ldap_link();
-      $ldap->cd($this->config->current['BASE']);
-
-      /* Remove those users, that use %members% acl && are member of this group. */
-      foreach($this->imapacl as $mail => $permission){
-        $ldap->search("(&(objectClass=person)(mail=".$mail."))",array("uid"));
-        $atr = $ldap->fetch();
-        if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
-          if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
-            unset($this->imapacl[$mail]);
-          }
+    }else{
+      foreach($this->imapacl as $user => $permission){
+        if ($user != "anyone" && $user != "%members%"){
+          $tmp[$permission][] = $user;
+        }
+        if ($user != "anyone" && $user != "%members%"){
+          unset($this->imapacl['']);
         }
       }
-      /* Append an empty entry, for special acl handling */
-      if(count($this->imapacl)==2){
-        $this->imapacl[''] ="";
-      }
-  
-    }else{ // Not kolab 
-      /* Load permissions */ 
-      if (isset($this->attrs['acl'])){
-        for ($i= 0; $i<$this->attrs['acl']['count']; $i++){
-          list($user, $permission)= split(' ', $this->attrs['acl'][$i]);
-          $this->imapacl[$user]= $permission;
-          if ($user != "anyone" && $user != "%members%"){
-            unset($this->imapacl['']);
-          }
+    }
+
+
+    /**
+     * Detect group member with same acl and replace them with %members%
+     **/
+
+    /* In this section we dectect which acl is tho most used 
+       This will be used as %members% acl  
+     */
+    $tmp2 = array(); 
+    foreach($tmp as $acl => $user){
+      $tmp2[count($tmp[$acl])]=$acl;
+    }
+    /* Most used at last 
+     */
+    ksort($tmp2);      
+
+    /* Assign last (most used acl) to %members% acl 
+     */
+    $str = array_pop($tmp2);
+    if(!empty($str)) {
+      $this->imapacl['%members%']=$str;
+    }
+
+    /* Open ldap connection 
+     */
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+
+    /* Remove those users, that use %members% acl && are member of this group. */
+    foreach($this->imapacl as $mail => $permission){
+      $ldap->search("(&(objectClass=person)(|(mail=".$mail.")(uid=".$mail.")))",array("uid"));
+      $atr = $ldap->fetch();
+      if((isset($this->attrs['memberUid'])) && (is_array($this->attrs['memberUid']))){
+        if((isset($atr['uid'][0]))&&(in_array($atr['uid'][0],$this->attrs['memberUid']))&&($permission == $this->imapacl['%members%'])){
+          unset($this->imapacl[$mail]);
         }
       }
     }
 
+    /**
+     * ENDE: Detect group member with same acl and replace them with %members%
+     **/
+
+
+    /* Append an empty entry, for special acl handling */
+    if(count($this->imapacl)==2){
+      $this->imapacl[''] ="lrsw";
+      $this->indexed_user[] = ''; 
+    }
+
     /* Load Mailserver  
      */
     if(isset($this->attrs['gosaMailServer'][0])){
       $this->gosaMailServer =  $this->attrs['gosaMailServer'][0];
     }
     /* Fill translations */
-    $this->perms["lrs"]= _("read");
-    $this->perms["lrsp"]= _("post");
+    $this->perms["lrsw"]= _("read");
+    $this->perms["lrswp"]= _("post");
     $this->perms["p"]= _("external post");
-    $this->perms["lrsip"]= _("append");
+    $this->perms["lrswip"]= _("append");
     $this->perms["lrswipcd"]= _("write");
+    $this->perms["lrswipcda"]= _("admin");
+    $this->perms[""]= _("none");
   }
 
   function execute()
@@ -295,6 +319,7 @@ class mailgroup extends plugin
 
     /* Load templating engine */
     $smarty= get_smarty();
+    $display = "";
     if ($_SESSION['js']==FALSE){
       $smarty->assign("javascript", "false");
     } else {
@@ -303,12 +328,19 @@ class mailgroup extends plugin
 
     /* Do we need to flip is_account state? */
     if (isset($_POST['modify_state'])){
-      $this->is_account= !$this->is_account;
-    }
 
+      /* Onyl change account state if allowed */
+      if($this->is_account && $this->acl == "#all#"){
+        $this->is_account= !$this->is_account;
+      }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
+        $this->is_account= !$this->is_account;
+      }
+    }
+    
     /* Do we represent a valid account? */
     if (!$this->is_account && $this->parent == NULL){
-      $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
+
+      $display.= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
         _("This 'dn' has no valid mail extensions.")."</b>";
       return ($display);
     }
@@ -320,17 +352,33 @@ class mailgroup extends plugin
         $display= $this->show_header(_("Remove mail account"),
             _("This account has mail features enabled. You can disable them by clicking below."));
       } else {
-        $display= $this->show_header(_("Create mail account"),
+        $display.= $this->show_header(_("Create mail account"),
             _("This account has mail features disabled. You can enable them by clicking below."));
+
+        /* Show checkbox that allows us to remove imap entry too*/
+        if($this->initially_was_account){
+        
+          $c = "";
+          if($this->remove_folder_from_imap){
+            $c= " checked ";
+          }
+  
+          $display .= "<h2>Shared folder delete options</h2>
+                       <input class='center' type='checkbox' name='remove_folder_from_imap' value='1' ".$c."  
+                          title='"._("Remove shared folder from mail server database when entry gets removed in LDAP")."'>";
+          $display .= _("Remove the shared folder and all its contents after saving this account"); 
+        }
+
         return ($display);
       }
     }
 
     /* Add ACL? */
+    if (isset($_POST["add_acl"])){
+      $this->imapacl[""]= "lrsw";
+      $this->indexed_user[] = "";
+    }
     foreach ($this->indexed_user as $nr => $user){
-      if (isset($_POST["add_$nr"])){
-        $this->imapacl[""]= "l";
-      }
       if (isset($_POST["del_$nr"])){
         unset ($this->imapacl[$user]);
       }
@@ -530,17 +578,15 @@ class mailgroup extends plugin
           "value=\"$user\" $perm></td><td><select size=\"1\" name=\"perm_$nr\" $perm>";
         foreach ($this->perms as $key => $value){
           if ($acl == $key){
-            $tmp.= "<option value=$key selected>$value</option>";
+            $tmp.= "<option value=\"$key\" selected>$value</option>";
           } else {
-            $tmp.= "<option value=$key>$value</option>";
+            $tmp.= "<option value=\"$key\">$value</option>";
           }
         }
         $tmp.= "</select>&nbsp;";
         if ($nr == $count - 1){
-          $tmp.= "<input type=submit value=\""._("Add")."\" ".
-            "name=\"add_$nr\" $perm>";
         }
-        if ($count > 3){
+        if ($count > 2){
           $tmp.= "<input type=submit value=\""._("Remove")."\" ".
             "name=\"del_$nr\" $perm></td></tr>";
         }
@@ -548,6 +594,7 @@ class mailgroup extends plugin
       $this->indexed_user[$nr]= $user;
       $this->indexed_acl[$nr++]= $acl;
     }
+    $tmp.= "<tr><td></td><td></td><td><input type=submit value='"._("Add")."' name='add_acl' $perm>";
     $smarty->assign("plusattributes", $tmp);
 
     /* Show main page */
@@ -568,6 +615,29 @@ class mailgroup extends plugin
       $smarty->assign("quotadefined", "false");
     }
 
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+
+      $smarty->assign("kolab", TRUE);
+      $smarty->assign("JS",$_SESSION['js']);
+      $smarty->assign("kolabFolderType_Types",    array (   ''      => _('Unspecified'),  'mail' => _('Mails'),
+                                                            'task'  => _('Tasks') ,       'journal' => _('Journals'),
+                                                            'calendar' => _('Calendar'),       'contact' => _('Contacts'), 
+                                                            'note'  => _('Notes')));
+      if($this->kolabFolderType_Type == "mail"){
+        $smarty->assign("kolabFolderType_SubTypes", array(    
+              ''          => _('Unspecified'),  'inbox'     => _("Inbox")   , 
+              'drafts'    => _("Drafts"),       'sentitems' => _("Sent items"),
+              'junkemail' => _("Junk mail")));
+      }else{
+        $smarty->assign("kolabFolderType_SubTypes", array(  'default' => _("Default")));
+      }
+      $smarty->assign("kolabFolderType_Type",     $this->kolabFolderType_Type);
+      $smarty->assign("kolabFolderType_SubType",  $this->kolabFolderType_SubType);
+    }else{
+      $smarty->assign("kolab", FALSE);
+    }
+
+
     $display.= $smarty->fetch (get_template_path('mail.tpl', TRUE));
     return ($display);
   }
@@ -579,7 +649,7 @@ class mailgroup extends plugin
     /* Added these ObjectClass and Attributes, because they were not 
        removed correctly, only in case of kolab ... 
      */
-    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/kolab/i",$this->config->current['MAILMETHOD'])){
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
       $this->attributes[]="acl";
       $this->objectclasses[] = "kolabSharedFolder";
     }
@@ -596,18 +666,19 @@ class mailgroup extends plugin
 
     /* Connect to IMAP server for account deletion */
     if ($this->initially_was_account){
       $method= new $this->method($this->config);
-      if ($method->connect($this->gosaMailServer)){
+      $method->fixAttributesOnRemove($this);
+      if ($method->connect($this->gosaMailServer) && $this->remove_folder_from_imap){
+
         /* Remove account from IMAP server */
         $method->deleteMailbox($this->uid);
         $method->disconnect();
       }
-      $method->fixAttributesOnRemove($this);
     }
     /* Keep uid */
     unset ($this->attrs['uid']);
 
-
     $ldap->cd($this->dn);
     $ldap->modify ($this->attrs); 
     show_ldap_error($ldap->get_error(), _("Removing group mail settings failed"));
@@ -620,6 +691,24 @@ class mailgroup extends plugin
   /* Save data to object */
   function save_object()
   {
+
+    /* Add special kolab attributes */    
+    if(isset($this->config->current['MAILMETHOD'])&&preg_match("/olab/i",$this->config->current['MAILMETHOD'])){
+      if(isset($_POST['kolabFolderType_Type'])){
+        $this->kolabFolderType_Type = get_post("kolabFolderType_Type");
+        $this->kolabFolderType_SubType = get_post("kolabFolderType_SubType");
+      }
+    }
+  
+    /* Check if user wants to remove the shared folder from imap too */
+    if($this->initially_was_account && !$this->is_account){
+      if(isset($_POST['remove_folder_from_imap'])){
+        $this->remove_folder_from_imap = true;
+      }else{
+        $this->remove_folder_from_imap = false;
+      }
+    }
+
     /* Assemble mail delivery mode
        The mode field in ldap consists of values between braces, this must
        be called when 'mail' is set, because checkboxes may not be set when
@@ -676,6 +765,7 @@ I: Only insider delivery */
             $this->is_modified= TRUE;
           }
           $this->imapacl[$_POST["user_$nr"]]= $_POST["perm_$nr"];
+          $this->indexed_user[$nr] = $_POST["user_$nr"];
         }
       }
     }
@@ -698,36 +788,8 @@ I: Only insider delivery */
     $this->attrs['gosaMailForwardingAddress'] = $this->gosaMailForwardingAddress;
     $this->attrs['gosaSharedFolderTarget']    = "share+".$this->uid;
 
-    if(preg_match("/kolab/i",$this->mmethod)){
-      /* Save acl's */
-      $this->attrs['acl']= array();
-      foreach ($this->imapacl as $user => $acl){
-        if ($user == ""){
-          continue;
-        }
-        $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail"));
-        $mail = $ldap->fetch();
-        if($mail){
-          if(isset($mail['mail'][0])){
-            $this->attrs['acl'][]= $mail['mail'][0]." $acl";
-          }
-        }else{
-          $this->attrs['acl'][]= "$user $acl";
-        }
-      }
-    }else{
-      /* Save acl's */
-      $this->attrs['acl']= array();
-      foreach ($this->imapacl as $user => $acl){
-        if ($user == ""){
-          continue;
-        }
-        $this->attrs['acl'][]= "$user $acl";
-      }
-    }
-
     /* Only do IMAP actions if we are not a template */
-    if(preg_match("/kolab/i",$this->mmethod)){
+    if(preg_match("/olab/i",$this->mmethod)){
       if (empty($this->gosaMailServer)||is_array($this->gosaMailServer)){
         if(isset($this->attrs['gosaMailServer'][0])){
           $this->gosaMailServer = $this->attrs['gosaMailServer'][0];
@@ -735,7 +797,80 @@ I: Only insider delivery */
       }
     }  
 
+    /* Exchange '%member%' pseudo entry */
+    $memberacl= $this->imapacl['%members%'];
+    foreach ($this->members as $user){
+      if (!isset($this->imapacl[$user])){
+        $this->imapacl[$user]= $memberacl;
+      }
+    }
+
+    /* Prepare kolab attributes to be written  */
+    if(preg_match("/olab/i",$this->mmethod)){
+      $this->attrs['acl']= array();
+      if(!empty($this->kolabFolderType_Type)){ 
+        $this->attrs['kolabFolderType'] = $this->kolabFolderType_Type.".".$this->kolabFolderType_SubType;
+      }else{
+        $this->attrs['kolabFolderType'] = array();
+      }
+    }else{
+      $this->attrs['acl']= array();;
+      unset($this->attrs['acl']);
+    }
 
+    /* Get naming attribute for mail accounts */  
+    $tmp = new $this->method($this->config);
+    $uattrib = $tmp->uattrib;
+
+    /* Create ACL array 
+     *  In case of kolab also create attrs['acl'] to save acls in ldap.
+     */
+    foreach ($this->imapacl as $user => $acl){
+
+      if (empty($user) || $user == ""){
+        unset($this->imapacl[$user]);
+      }
+  
+      /* Skip placeholder */
+      if (empty($user) || $user == "" || preg_match("/%members%/",$user)){
+        continue;
+      }
+
+      /* Check if your is a real GOsa user  
+       * If user is a real GOsa user but do not have an email address - SKIP adding acls
+       * If user is a real GOsa user with an emal address - add acls  
+       */
+      $ldap->search("(&(objectClass=person)(|(uid=".$user.")(mail=".$user.")))",array("mail","uid"));
+      if($ldap->count()){
+        $attrs = $ldap->fetch();
+
+        /* Has the user a valid mail account? */
+        if(isset($attrs['mail'][0])){
+
+          $name = $attrs[$uattrib][0];
+
+          /* In case of kolab methods add acl attribute too */
+          if(preg_match("/olab/i",$this->mmethod)){
+            $this->attrs['acl'][]= $name." ".$acl;
+            unset($this->imapacl[$user]);
+          }
+          $this->imapacl[$name] = $acl;
+        }else{
+
+          /* User has no mail extension */
+          unset($this->imapacl[$user]);
+        }
+      }else{
+        /* Seems to be a manually added acl */
+
+        /* In case of kolab methods add acl attribute too */
+        if(preg_match("/olab/i",$this->mmethod)){
+          $this->attrs['acl'][]= $user." ".$acl;
+        }
+        $this->imapacl[$user] = $acl;
+      }
+    }
     if ((!$this->is_template)&&(!empty($this->gosaMailServer))){
       $method= new $this->method($this->config);
       $method->fixAttributesOnStore($this);
@@ -747,29 +882,6 @@ I: Only insider delivery */
       }
     }
 
-    /* Exchange '%member%' pseudo entry */
-    $memberacl= $this->imapacl['%members%'];
-
-    foreach ($this->members as $user){
-      if(preg_match("/kolab/i",$this->mmethod)){
-        $ldap->cd($this->config->current['BASE']);
-        $ldap->search("(&(objectClass=person)(|(mail=".$user.")(uid=".$user.")))",array("mail"));
-        $at = $ldap->fetch();
-        if(isset($at['mail'][0])){
-          $user = $at['mail'][0];
-        }
-      }
-      if (!isset($this->imapacl[$user])){
-        $this->imapacl[$user]= $memberacl;
-      }
-    }
-    $this->attrs['acl'] = array();
-    foreach($this->imapacl as $user => $acl){
-      if(preg_match("/%members%/",$user) || empty($user)) continue;
-      
-      $this->attrs['acl'][] = $user." ".$acl;
-    }
-
     /* Save data to LDAP */
     $ldap->cd($this->dn);
     $this->cleanup();
@@ -779,7 +891,7 @@ I: Only insider delivery */
     /* Optionally execute a command after we're done */
     if ($this->initially_was_account == $this->is_account){
       if ($this->is_modified){
-        $this->handle_post_events("mofify");
+        $this->handle_post_events("modify");
       }
     } else {
       $this->handle_post_events("add");
@@ -807,7 +919,7 @@ I: Only insider delivery */
     }
     $ldap->cd($this->config->current['BASE']);
     $ldap->search ("(&(!(objectClass=gosaUserTemplate))(objectClass=gosaMailAccount)(|(mail=".$this->mail.")(gosaMailAlternateAddress=".
-        $this->mail."))(!(uid=".$this->cn."))(!(cn=".$this->cn.")))");
+        $this->mail."))(!(uid=".$this->orig_cn."))(!(cn=".$this->orig_cn.")))");
     if ($ldap->count() != 0){
       $message[]= _("The primary address you've entered is already in use.");
     }
@@ -972,8 +1084,24 @@ I: Only insider delivery */
     }
   }
 
+  function PrepareForCopyPaste($source)
+  {
+    plugin::PrepareForCopyPaste($source);
 
-}
+    /* Reset alternate mail addresses */
+    $this->gosaMailAlternateAddress = array();
+  }
+
+  
+  /* Remove given ACL for given member (uid,mail) ..
+   */
+  function removeUserAcl($index )
+  {
+    if(isset($this->imapacl[$index])){
+      unset($this->imapacl[$index]);
+    }
+  }
 
+}
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>