Code

52b4579330edb6012f00bdc4e71e2974b1c050d1
[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 $acl= "";
34   var $CopyPasteHandler  = NULL;
35   var $DivListGroup      = NULL;
37   var $ShowPrimaryCheckBox = false; 
39   function groupManagement ($config, $ui)
40   {
41     /* Save configuration for internal use */
42     $this->config= $config;
43     $this->ui= $ui;
45     /* Copy & Paste enabled ?*/
46     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
47       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48     }
49     
50     /* Detect if we have to display the primary group checkbox */
51     $tmp = "";
52     if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
53       $tmp = $this->config->data['MAIN']['NOPRIMARYGROUP'];
54     }
55     if(preg_match("/true/i",$tmp)|| (preg_match("/yes/",$tmp))){ 
56       $this->ShowPrimaryCheckBox = false;
57     } else {
58       $this->ShowPrimaryCheckBox = true;
59     }
61     /* Create dialog object */
62     $this->DivListGroup = new divListGroup($this->config,$this);
63     $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
64   }
66   function execute()
67   {
68         /* Call parent execute */
69         plugin::execute();
71     /* Store these posts if the current object is locked (used by somebody else)*/
72     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/");
74     /* Save data */
75     $s_action   = "";
76     $s_entry    = "";
78     /* Test Posts */
79     foreach($_POST as $key => $val){
80       // Post for delete
81       if(preg_match("/^group_del.*/",$key)){
82         $s_action = "del";
83         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
84         // Post for edit
85       }elseif(preg_match("/^group_edit_.*/",$key)){
86         $s_action="edit";
87         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
88         // Post for new
89       }elseif(preg_match("/^group_new.*/",$key)){
90         $s_action="new";
91       }elseif(preg_match("/^dep_home.*/i",$key)){
92         $s_action="home";
93       }elseif(preg_match("/^group_tplnew.*/i",$key)){
94         $s_action="new_tpl";
95       }elseif(preg_match("/^group_chgpw.*/i",$key)){
96         $s_action="change_pw";
97         $s_entry  = preg_replace("/group_chgpw_/i","",$key);
98       }elseif(preg_match("/_group_edit_/",$key)){
99         $type = preg_replace("/_group_edit_.*$/","",$key);
100         $s_action="edit";
101         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key); 
102         $_POST['arg'] = $type;
103       }elseif(preg_match("/^editPaste.*/i",$key)){
104         $s_action="editPaste";
105       }elseif(preg_match("/^copy_.*/",$key)){
106         $s_action="copy";
107         $s_entry  = preg_replace("/^copy_/i","",$key);
108       }elseif(preg_match("/^cut_.*/",$key)){
109         $s_action="cut";
110         $s_entry  = preg_replace("/^cut_/i","",$key);
111       }
112     }
113     $s_entry  = preg_replace("/_.$/","",$s_entry); 
115     /* Check for posted gets */
116     if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
117       $s_entry    = $_GET['id'];
118       $s_action = "edit";
119     }
121     $smarty= get_smarty();
123     /********************
124       Copy & Paste Handling  ...
125      ********************/
127     /* Only perform copy&paste requests if it is enabled
128      */
129     if($this->CopyPasteHandler){
130       if($str = $this->copyPasteHandling($s_action,$s_entry)){
131         return $str;
132       }
133     }
136     /********************
137       Create a new group  ...
138      ********************/
140     /* New group? */
141     if ($s_action=="new"){
143       /* By default we set 'dn' to 'new', all relevant plugins will
144          react on this. */
145       $this->dn= "new";
147       /* Create new usertab object */
148       $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
150       /* Set up the users ACL's for this 'dn' */
151       $acl= get_permissions ($this->DivListGroup->selectedBase, $this->ui->subtreeACL);
152       $this->grouptab->set_acl($acl);
153     }
156     /********************
157       Save Group Tab/Object Changes
158      ********************/
160     /* Finish group edit is triggered by the tabulator dialog, so
161        the user wants to save edited data. Check and save at this
162        point. */
163     if ((isset($_POST['edit_finish'])) && (isset($this->grouptab->config)) ){
165       /* Check tabs, will feed message array 
166          Save, or display error message? */
167       $message= $this->grouptab->check();
168       if (count($message) == 0){
170         /* Save user data to ldap */
171         $this->grouptab->save();
172         gosa_log ("Group object'".$this->dn."' has been saved");
174         /* Group has been saved successfully, remove lock from LDAP. */
175         if ($this->dn != "new"){
176           del_lock ($this->dn);
177         }
179         /* There's no page reload so we have to read new users at this point. */
180         //$this->reload ();
181         unset ($this->grouptab);
182         $this->grouptab= NULL;
183         unset ($_SESSION['objectinfo']);
185       } else {
186         /* Ok. There seem to be errors regarding to the tab data,
187            show message and continue as usual. */
188         show_errors($message);
189       }
190     }
193     /********************
194       Edit existing group 
195      ********************/
197     /* User wants to edit data? */
198     if (($s_action=="edit") && (!isset($this->grouptab-> config))){
200       /* Get 'dn' from posted 'uid', must be unique */
201       $this->dn= $this->grouplist[trim($s_entry)]['dn'];
203       /* Check locking & lock entry if required */
204       $user = get_lock($this->dn);
205       if ($user != ""){
206         return(gen_locked_message ($user, $this->dn));
207       }
208       add_lock ($this->dn, $this->ui->dn);
210       /* Set up the users ACL's for this 'dn' */
211       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
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($acl);
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= get_permissions ($this->dn, $this->ui->subtreeACL);
233       $this->acl= get_module_permission($acl, "group", $this->dn);
234       if (chkacl($this->acl, "delete") == ""){
236         /* Check locking, save current plugin in 'back_plugin', so
237            the dialog knows where to return. */
238         if (($user= get_lock($this->dn)) != ""){
239           return(gen_locked_message ($user, $this->dn));
240         }
242         /* Lock the current entry, so nobody will edit it during deletion */
243         add_lock ($this->dn, $this->ui->dn);
244         $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
245         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
247       } else {
248         
249         /* Obviously the user isn't allowed to delete. Show message and clean session. */
250         print_red (_("You are not allowed to delete this group!"));
251       }
252     }
255     /********************
256       Delete group confirmed  
257      ********************/
259     /* Confirmation for deletion has been passed. Group should be deleted. */
260     if (isset($_POST['delete_group_confirm'])){
262       /* Some nice guy may send this as POST, so we've to check
263          for the permissions again. */
264       if (chkacl($this->acl, "delete") == ""){
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(array($this->acl));
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       del_lock ($this->grouptab->dn);
309       unset ($this->grouptab);
310       $this->grouptab= NULL;
311       unset($_SESSION['objectinfo']);
312     }
315     /********************
316       If there is currently a dialog open, display it
317      ********************/
319     /* Show tab dialog if object is present */
320     if (isset($this->grouptab->config)){
321       $display= $this->grouptab->execute();
323       /* Don't show buttons if tab dialog requests this */
324       if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
325         $display.= "<p style=\"text-align:right\">\n";
326         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
327         $display.= "&nbsp;\n";
328         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
329         $display.= "</p>";
330       }
331       return ($display);
332     }
334     /* Display dialog with group list */
335     $this->DivListGroup->execute();
337     /* Add departments if subsearch is disabled */
338     if(!$this->DivListGroup->SubSearch){
339       $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
340     }
341     $this->reload ();
342     $this->DivListGroup->setEntries($this->grouplist);
343     return($this->DivListGroup->Draw());
344   }
346   function reload($CreatePosixsList=false)
347   {
348     $this->grouplist        = array();
349     $primaries              = array();
350     $functional             = array();
351     $error= $error2         = "";
352     $filter                 = "(objectclass=posixGroup)";
354     $base                   = $this->DivListGroup->selectedBase;
355     $Regex                  = $this->DivListGroup->Regex;
356     $UserRegex              = $this->DivListGroup->UserRegex;
357     $SubSearch              = $this->DivListGroup->SubSearch;
358     $ShowPrimaryGroups      = $this->DivListGroup->ShowPrimaryGroups;
359     $ShowSambaGroups        = $this->DivListGroup->ShowSambaGroups;
360     $ShowApplicationGroups  = $this->DivListGroup->ShowApplicationGroups;
361     $ShowMailGroups         = $this->DivListGroup->ShowMailGroups;
362     $ShowFunctionalGroups   = $this->DivListGroup->ShowFunctionalGroups;
364     /* Prepare ldap class */
365     $ldap= $this->config->get_ldap_link();
366     $ldap->cd($base);
367     $ldap->set_size_limit($_SESSION['size_limit']);
370     /********************
371       Create filter depending on selected checkboxes 
372      ********************/
374     /* Add application groups */
375     if ($ShowApplicationGroups){
376       $filter.= "(objectClass=gosaApplicationGroup)";
377     }
378     
379     /* Add Mail Groups */
380     if ($ShowMailGroups){
381       $filter.= "(objectClass=gosaMailAccount)";
382     }
384     /*
385        #FIXME Why is "sfilter" or "filter" set ?
386         Please verify && add comment 
387      */
388     $sfilter= "";
389     if ($this->config->current['SAMBAVERSION'] == 3){
390       if (!$ShowPrimaryGroups){
391         $sfilter= "(objectClass=sambaGroupMapping)";
392       } elseif ($ShowSambaGroups){
393         $filter.= "(objectClass=sambaGroupMapping)";
394       }
395     }
397     /* Prepare filter for given Regex && UserRegex */
398     if ($filter != ""){
399       $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
400       if ($UserRegex != ""){
401         $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
402       }
403     }
406     /********************
407       Collect some groupids to be able to skip primary & functional groups 
408      ********************/
410     /* Collect primary groupIDs to show primary groups 
411        if this option is enabled in gosa conf && the checkbox is checked */  
412     if ($this->ShowPrimaryCheckBox){
413       $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", $this->ui->subtreeACL,
414           $base,array("gidNumber", "cn"),  GL_SUBSEARCH);
415       foreach ($res as $attrs){
416         $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
417       }
418     }
420     /* Collect all GroupIDs from those groups which are functional.
421        Only perfrom this search if  ShowFunctionalGroups  is unchecked, else leave arre empty  */ 
422     $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
423     $res = get_list($ff,$this->ui->subtreeACL,$base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
424     foreach($res as $attrs){
425       if (!isset($primaries[$attrs['gidNumber'][0]])){
426         $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
427       }
428     }
429    
431     /********************
432       Search for the prepared filter 
433      ********************/
434    
435     /* If subsearch is activated search for subobjects too */
436     $attrs = array("cn", "description", "gidNumber", "objectClass","FAIrelease");
438     if ($SubSearch){
439       $res= get_list($filter, $this->ui->subtreeACL, $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
440     } else {
441       $res= get_list($filter, $this->ui->subtreeACL, get_groups_ou().$base, $attrs, GL_SIZELIMIT);
442     }
444     /* Sort values into grouplist*/
445     $tmp = $tmp2 = array();
446     foreach ($res as $value){
447       /* Skip functional groups if checkbox isn't checked */
448       if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
449         continue;
450       }
451       
452       /* If gidNumber is in $primaries skip this entry */
453       if (($ShowPrimaryGroups)   ||   (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
454         $tmp2[$value['cn'][0]] = $value;
455         $tmp [$value['cn'][0]] = $value['cn'][0];
456       }
457     }
458     natcasesort($tmp);
459     foreach($tmp as $name){
460       $this->grouplist[] = $tmp2[$name]; 
461     }
462     reset ($this->grouplist);
463   }
467   /* Perform copy & paste requests
468      If copy&paste is in progress this returns a dialog to fix required attributes 
469    */ 
470   function copyPasteHandling($s_action,$s_entry)
471   {
472     /* Paste copied/cutted object in here
473      */
474     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
475       $this->CopyPasteHandler->save_object();
476       $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
477       if($str = $this->CopyPasteHandler->execute()){
478         return( $str);
479       };
480       /* Ensure that the new object is shown in the list now */
481       $this->relaod();
482     }
485     /* Copy current object to CopyHandler
486      */
487     if($s_action == "copy"){
488       $this->CopyPasteHandler->Clear();
490       $dn     = $this->grouplist[trim($s_entry)]['dn'];
491       $acl    = get_permissions ($dn, $this->ui->subtreeACL);
493       $obj    = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
494       $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
495       $obj->set_acl($acl);
496       $objNew->set_acl($acl);
497       $this->CopyPasteHandler->Copy($obj,$objNew);
498     }
501     /* Copy current object to CopyHandler
502      */
503     if($s_action == "cut"){
504       $this->CopyPasteHandler->Clear();
505       $dn = $this->grouplist[trim($s_entry)]['dn'];
506       $acl= get_permissions ($dn, $this->ui->subtreeACL);
508       $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
509       $obj->set_acl($acl);
511       $this->CopyPasteHandler->Cut($obj);
512     }
513   }
515   /* Save data to object */
516   function save_object()
517   {
518     $this->DivListGroup->save_object();
519   }
522   function remove_lock()
523   {
524     if (isset($this->grouptab->dn)){
525       del_lock ($this->grouptab->dn);
526     }
527   }
530   function remove_from_parent()
531   {
532     /* Optionally execute a command after we're done */
533     $this->postremove();
534   }
537   /* Save to LDAP */
538   function save()
539   {
540     /* Optionally execute a command after we're done */
541     $this->postcreate();
542   }
544   /* Unused functions  */
545   function check()  { }
546   function adapt_from_template($dn) { }
547   function password_change_needed()  { }
548   function show_header($button_text, $text, $disabled= FALSE)  { }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>