Code

Fixed base selection.
[gosa.git] / plugins / admin / groups / class_groupManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabs_group.inc";
22 class groupManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Groups";
26   var $plDescription= "This does something";
28   /* Dialog attributes */
29   var $grouptab           = NULL;
30   var $grouplist          = array();
31   var $ui                 = NULL;
32   var $CopyPasteHandler   = NULL;
33   var $DivListGroup       = NULL;
34   var $ShowPrimaryCheckBox= false; 
36   function groupManagement ($config, $ui)
37   {
38     /* Save configuration for internal use */
39     $this->config = $config;
40     $this->ui     = $ui;
42     /* Copy & Paste enabled ?*/
43     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
44       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
45     }
46     
47     /* Detect if we have to display the primary group checkbox */
48     $tmp = "";
49     if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
50       $tmp = $this->config->data['MAIN']['NOPRIMARYGROUP'];
51     }
52     if(preg_match("/true/i",$tmp)|| (preg_match("/yes/",$tmp))){ 
53       $this->ShowPrimaryCheckBox = false;
54     } else {
55       $this->ShowPrimaryCheckBox = true;
56     }
58     /* Create dialog object */
59     $this->DivListGroup = new divListGroup($this->config,$this);
60     $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
61   }
64   function execute()
65   {
66         /* Call parent execute */
67         plugin::execute();
69     /* Store these posts if the current object is locked (used by somebody else)*/
70     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/");
72     /* Save data */
73     $s_action   = "";
74     $s_entry    = "";
76     /* Test Posts */
77     foreach($_POST as $key => $val){
78       // Post for delete
79       if(preg_match("/^group_del.*/",$key)){
80         $s_action = "del";
81         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
82         // Post for edit
83       }elseif(preg_match("/^group_edit_.*/",$key)){
84         $s_action="edit";
85         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
86         // Post for new
87       }elseif(preg_match("/^group_new.*/",$key)){
88         $s_action="new";
89       }elseif(preg_match("/^dep_home.*/i",$key)){
90         $s_action="home";
91       }elseif(preg_match("/^group_tplnew.*/i",$key)){
92         $s_action="new_tpl";
93       }elseif(preg_match("/^group_chgpw.*/i",$key)){
94         $s_action="change_pw";
95         $s_entry  = preg_replace("/group_chgpw_/i","",$key);
96       }elseif(preg_match("/_group_edit_/",$key)){
97         $type = preg_replace("/_group_edit_.*$/","",$key);
98         $s_action="edit";
99         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key); 
100         $_POST['arg'] = $type;
101       }elseif(preg_match("/^editPaste.*/i",$key)){
102         $s_action="editPaste";
103       }elseif(preg_match("/^copy_.*/",$key)){
104         $s_action="copy";
105         $s_entry  = preg_replace("/^copy_/i","",$key);
106       }elseif(preg_match("/^cut_.*/",$key)){
107         $s_action="cut";
108         $s_entry  = preg_replace("/^cut_/i","",$key);
109       }
110     }
111     $s_entry  = preg_replace("/_.$/","",$s_entry); 
113     /* Check for posted gets */
114     if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
115       $s_entry    = $_GET['id'];
116       $s_action = "edit";
117     }
119     $smarty= get_smarty();
121     /********************
122       Copy & Paste Handling  ...
123      ********************/
125     /* Only perform copy&paste requests if it is enabled
126      */
127     if($this->CopyPasteHandler){
128       if($str = $this->copyPasteHandling($s_action,$s_entry)){
129         return $str;
130       }
131     }
134     /********************
135       Create a new group  ...
136      ********************/
138     /* New group? */
139     if ($s_action=="new"){
141       /* Check create permissions */
142       $acl = $this->ui->get_permissions($this->DivListGroup->selectedBase,"groups/group");
143       if(preg_match("/c/",$acl)){
145         /* By default we set 'dn' to 'new', all relevant plugins will
146            react on this. */
147         $this->dn= "new";
149         /* Create new usertab object */
150         $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
152         /* Set up the users ACL's for this 'dn' */
153         $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
154       }
155     }
158     /********************
159       Save Group Tab/Object Changes
160      ********************/
162     /* Finish group edit is triggered by the tabulator dialog, so
163        the user wants to save edited data. Check and save at this
164        point. */
165     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
167       /* Check tabs, will feed message array 
168          Save, or display error message? */
169       $message= $this->grouptab->check();
170       if (count($message) == 0){
172         /* Save user data to ldap */
173         $this->grouptab->save();
174         gosa_log ("Group object '".$this->dn."' has been saved");
176         if (!isset($_POST['edit_apply'])){
177           /* Group has been saved successfully, remove lock from LDAP. */
178           if ($this->dn != "new"){
179             del_lock ($this->dn);
180           }
182           /* There's no page reload so we have to read new users at this point. */
183           //$this->reload ();
184           unset ($this->grouptab);
185           $this->grouptab= NULL;
186           unset ($_SESSION['objectinfo']);
187         }
188       } else {
189         /* Ok. There seem to be errors regarding to the tab data,
190            show message and continue as usual. */
191         show_errors($message);
192       }
193     }
196     /********************
197       Edit existing group 
198      ********************/
200     /* User wants to edit data? */
201     if (($s_action=="edit") && (!isset($this->grouptab-> config))){
203       /* Get 'dn' from posted 'uid', must be unique */
204       $this->dn= $this->grouplist[trim($s_entry)]['dn'];
206       /* Check locking & lock entry if required */
207       $user = get_lock($this->dn);
208       if ($user != ""){
209         return(gen_locked_message ($user, $this->dn));
210       }
211       add_lock ($this->dn, $this->ui->dn);
213       /* Register grouptab to trigger edit dialog */
214       $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
215       $this->grouptab->set_acl_base($this->dn);
216       $_SESSION['objectinfo']= $this->dn;
217     }
220     /********************
221       Delete group 
222      ********************/
224     /* Remove group was requested */
225     if ($s_action=="del"){
227       /* Get 'dn' from posted 'uid' */
228       $this->dn= $this->grouplist[trim($s_entry)]['dn'];
230       /* Load permissions for selected 'dn' and check if
231          we're allowed to remove this 'dn' */
232       $acl = $this->ui->get_permissions($this->dn,"groups/group");
233       if(preg_match("/d/",$acl)){
235         /* Check locking, save current plugin in 'back_plugin', so
236            the dialog knows where to return. */
237         if (($user= get_lock($this->dn)) != ""){
238           return(gen_locked_message ($user, $this->dn));
239         }
241         /* Lock the current entry, so nobody will edit it during deletion */
242         add_lock ($this->dn, $this->ui->dn);
243         $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
244         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
246       } else {
247         
248         /* Obviously the user isn't allowed to delete. Show message and clean session. */
249         print_red (_("You are not allowed to delete this group!"));
250       }
251     }
254     /********************
255       Delete group confirmed  
256      ********************/
258     /* Confirmation for deletion has been passed. Group should be deleted. */
259     if (isset($_POST['delete_group_confirm'])){
261       /* Some nice guy may send this as POST, so we've to check
262          for the permissions again. */
263       $acl = $this->ui->get_permissions($this->dn,"groups/group");
264       if(preg_match("/d/",$acl)){
266         /* Delete request is permitted, perform LDAP action */
267         $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
268         $this->grouptab->set_acl_base($this->dn);
269         $this->grouptab->delete ();
270         gosa_log ("Group object'".$this->dn."' has been removed");
271         unset ($this->grouptab);
272         $this->grouptab= NULL;
274         /* Group list has changed, reload it. */
275         //$this->reload ();
277       } else {
279         /* Normally this shouldn't be reached, send some extra
280            logs to notify the administrator */
281         print_red (_("You are not allowed to delete this group!"));
282         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
283       }
285       /* Remove lock file after successfull deletion */
286       del_lock ($this->dn);
287       unset($_SESSION['objectinfo']);
288     }
291     /********************
292       Delete group canceled  
293      ********************/
295     /* Delete group canceled? */
296     if (isset($_POST['delete_cancel'])){
297       del_lock ($this->dn);
298       unset($_SESSION['objectinfo']);
299     }
300     
302     /********************
303       A dialog was canceled  
304      ********************/
306     /* Cancel dialogs */
307     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
308       if(isset($this->grouptab->dn)){
309         del_lock ($this->grouptab->dn);
310       }
311       unset ($this->grouptab);
312       $this->grouptab= NULL;
313       unset($_SESSION['objectinfo']);
314     }
317     /********************
318       If there is currently a dialog open, display it
319      ********************/
321     /* Show tab dialog if object is present */
322     if (isset($this->grouptab->config)){
323       $display= $this->grouptab->execute();
325       /* Don't show buttons if tab dialog requests this */
326       if(isset($this->grouptab->by_object)){
327         if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
328           $display.= "<p style=\"text-align:right\">\n";
329           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
330           $display.= "&nbsp;\n";
331           if ($this->dn != "new"){
332             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
333             $display.= "&nbsp;\n";
334           }
335           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
336           $display.= "</p>";
337         }
338       }
339       return ($display);
340     }
343     /* Check if there is a snapshot dialog open */
344     $base = $this->DivListGroup->selectedBase;
345     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
346       return($str);
347     }
348    
349     /* Display dialog with group list */
350     $this->DivListGroup->parent = $this;
351     $this->DivListGroup->execute();
353     /* Add departments if subsearch is disabled */
354     if(!$this->DivListGroup->SubSearch){
355       $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
356     }
357     $this->reload ();
358     $this->DivListGroup->setEntries($this->grouplist);
359     return($this->DivListGroup->Draw());
360   }
363   /* Return departments, that will be included within snapshot detection */
364   function get_used_snapshot_bases()
365   {
366     return(array(get_groups_ou().$this->DivListGroup->selectedBase));
367   }
371   function reload($CreatePosixsList=false)
372   {
373     $this->grouplist        = array();
374     $primaries              = array();
375     $functional             = array();
376     $error= $error2         = "";
377     $filter                 = "(objectclass=posixGroup)";
379     $base                   = $this->DivListGroup->selectedBase;
380     $Regex                  = $this->DivListGroup->Regex;
381     $UserRegex              = $this->DivListGroup->UserRegex;
382     $SubSearch              = $this->DivListGroup->SubSearch;
383     $ShowPrimaryGroups      = $this->DivListGroup->ShowPrimaryGroups;
384     $ShowSambaGroups        = $this->DivListGroup->ShowSambaGroups;
385     $ShowApplicationGroups  = $this->DivListGroup->ShowApplicationGroups;
386     $ShowMailGroups         = $this->DivListGroup->ShowMailGroups;
387     $ShowFunctionalGroups   = $this->DivListGroup->ShowFunctionalGroups;
389     /* Prepare ldap class */
390     $ldap= $this->config->get_ldap_link();
391     $ldap->cd($base);
392     $ldap->set_size_limit($_SESSION['size_limit']);
395     /********************
396       Create filter depending on selected checkboxes 
397      ********************/
399     /* Add application groups */
400     if ($ShowApplicationGroups){
401       $filter.= "(objectClass=gosaApplicationGroup)";
402     }
403     
404     /* Add Mail Groups */
405     if ($ShowMailGroups){
406       $filter.= "(objectClass=gosaMailAccount)";
407     }
409     $sfilter= "";
410     if ($this->config->current['SAMBAVERSION'] == 3){
411       if (!$ShowPrimaryGroups){
412         $sfilter= "(objectClass=sambaGroupMapping)";
413       } elseif ($ShowSambaGroups){
414         $filter.= "(objectClass=sambaGroupMapping)";
415       }
416     }
418     /* Prepare filter for given Regex && UserRegex */
419     if ($filter != ""){
420       $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
421       if ($UserRegex != ""){
422         $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
423       }
424     }
427     /********************
428       Collect some groupids to be able to skip primary & functional groups 
429      ********************/
431     /* Collect primary groupIDs to show primary groups 
432        if this option is enabled in gosa conf && the checkbox is checked */  
433     if ($this->ShowPrimaryCheckBox){
434       $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
435           "groups", $base,array("gidNumber", "cn"),  GL_SUBSEARCH);
436       foreach ($res as $attrs){
437         $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
438       }
439     }
441     /* Collect all GroupIDs from those groups which are functional.
442        Only perfrom this search if  ShowFunctionalGroups  is unchecked, else leave arre empty  */ 
443     $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
444     $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
445     foreach($res as $attrs){
446       if (!isset($primaries[$attrs['gidNumber'][0]])){
447         $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
448       }
449     }
450    
452     /********************
453       Search for the prepared filter 
454      ********************/
455    
456     /* If subsearch is activated search for subobjects too */
457     $attrs = array("cn", "description", "gidNumber", "objectClass","FAIrelease");
459     if ($SubSearch){
460       $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
461     } else {
462       $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
463     }
465     /* Sort values into grouplist*/
466     $tmp = $tmp2 = array();
467     foreach ($res as $value){
468       /* Skip functional groups if checkbox isn't checked */
469       if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
470         continue;
471       }
472       
473       /* If gidNumber is in $primaries skip this entry */
474       if (($ShowPrimaryGroups)   ||   (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
475         $tmp2[$value['cn'][0]] = $value;
476         $tmp [$value['cn'][0]] = $value['cn'][0];
477       }
478     }
479     natcasesort($tmp);
480     foreach($tmp as $name){
481       $this->grouplist[] = $tmp2[$name]; 
482     }
483     reset ($this->grouplist);
484   }
488   /* Perform copy & paste requests
489      If copy&paste is in progress this returns a dialog to fix required attributes 
490    */ 
491   function copyPasteHandling($s_action,$s_entry)
492   {
493     /* Paste copied/cutted object in here
494      */
495     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
496       $this->CopyPasteHandler->save_object();
497       $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
498       if($str = $this->CopyPasteHandler->execute()){
499         return( $str);
500       };
501       /* Ensure that the new object is shown in the list now */
502       $this->reload();
503     }
506     /* Copy current object to CopyHandler
507      */
508     if($s_action == "copy"){
510       $dn  = $this->grouplist[trim($s_entry)]['dn'];
511       $acl_all  = $ui->has_complete_category_acls($this->selectedBase,"groups") ;
512       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
514         $this->CopyPasteHandler->Clear();
515         $obj    = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
516         $obj->set_acl_base($dn);
517         $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
518         $objNew->set_acl_base($dn);
520         $this->CopyPasteHandler->Copy($obj,$objNew);
521       }else{
522         print_red("You are not allowed to copy this entry.");
523       }
524     }
527     /* Copy current object to CopyHandler
528      */
529     if($s_action == "cut"){
530       
531       $dn = $this->grouplist[trim($s_entry)]['dn'];
532       $acl_all  = $ui->has_complete_category_acls($this->selectedBase,"groups") ;
533       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
535         $this->CopyPasteHandler->Clear();
536         $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
537         $obj->set_acl_base($dn);
538         $this->CopyPasteHandler->Cut($obj);
539       }else{
540         print_red("You are not allowed to cut this entry.");
541       }
542     }
543   }
545   /* Save data to object */
546   function save_object()
547   {
548     $this->DivListGroup->save_object();
549   }
552   function remove_lock()
553   {
554     if (isset($this->grouptab->dn)){
555       del_lock ($this->grouptab->dn);
556     }
557   }
560   function remove_from_parent()
561   {
562     /* Optionally execute a command after we're done */
563     $this->postremove();
564   }
567   /* Save to LDAP */
568   function save()
569   {
570     /* Optionally execute a command after we're done */
571     $this->postcreate();
572   }
574   /* Unused functions  */
575   function check()  { }
576   function adapt_from_template($dn) { }
577   function password_change_needed()  { }
579 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
580 ?>