Code

Added snapshot to groups && fixed restore dialog
[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($_POST['edit_apply'])) && (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         if (!isset($_POST['edit_apply'])){
175           /* Group has been saved successfully, remove lock from LDAP. */
176           if ($this->dn != "new"){
177             del_lock ($this->dn);
178           }
180           /* There's no page reload so we have to read new users at this point. */
181           //$this->reload ();
182           unset ($this->grouptab);
183           $this->grouptab= NULL;
184           unset ($_SESSION['objectinfo']);
185         }
186       } else {
187         /* Ok. There seem to be errors regarding to the tab data,
188            show message and continue as usual. */
189         show_errors($message);
190       }
191     }
194     /********************
195       Edit existing group 
196      ********************/
198     /* User wants to edit data? */
199     if (($s_action=="edit") && (!isset($this->grouptab-> config))){
201       /* Get 'dn' from posted 'uid', must be unique */
202       $this->dn= $this->grouplist[trim($s_entry)]['dn'];
204       /* Check locking & lock entry if required */
205       $user = get_lock($this->dn);
206       if ($user != ""){
207         return(gen_locked_message ($user, $this->dn));
208       }
209       add_lock ($this->dn, $this->ui->dn);
211       /* Set up the users ACL's for this 'dn' */
212       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
214       /* Register grouptab to trigger edit dialog */
215       $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
216       $this->grouptab->set_acl($acl);
217       $_SESSION['objectinfo']= $this->dn;
218     }
221     /********************
222       Delete group 
223      ********************/
225     /* Remove group was requested */
226     if ($s_action=="del"){
228       /* Get 'dn' from posted 'uid' */
229       $this->dn= $this->grouplist[trim($s_entry)]['dn'];
231       /* Load permissions for selected 'dn' and check if
232          we're allowed to remove this 'dn' */
233       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
234       $this->acl= get_module_permission($acl, "group", $this->dn);
235       if (chkacl($this->acl, "delete") == ""){
237         /* Check locking, save current plugin in 'back_plugin', so
238            the dialog knows where to return. */
239         if (($user= get_lock($this->dn)) != ""){
240           return(gen_locked_message ($user, $this->dn));
241         }
243         /* Lock the current entry, so nobody will edit it during deletion */
244         add_lock ($this->dn, $this->ui->dn);
245         $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
246         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
248       } else {
249         
250         /* Obviously the user isn't allowed to delete. Show message and clean session. */
251         print_red (_("You are not allowed to delete this group!"));
252       }
253     }
256     /********************
257       Delete group confirmed  
258      ********************/
260     /* Confirmation for deletion has been passed. Group should be deleted. */
261     if (isset($_POST['delete_group_confirm'])){
263       /* Some nice guy may send this as POST, so we've to check
264          for the permissions again. */
265       if (chkacl($this->acl, "delete") == ""){
267         /* Delete request is permitted, perform LDAP action */
268         $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
269         $this->grouptab->set_acl(array($this->acl));
270         $this->grouptab->delete ();
271         gosa_log ("Group object'".$this->dn."' has been removed");
272         unset ($this->grouptab);
273         $this->grouptab= NULL;
275         /* Group list has changed, reload it. */
276         //$this->reload ();
278       } else {
280         /* Normally this shouldn't be reached, send some extra
281            logs to notify the administrator */
282         print_red (_("You are not allowed to delete this group!"));
283         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
284       }
286       /* Remove lock file after successfull deletion */
287       del_lock ($this->dn);
288       unset($_SESSION['objectinfo']);
289     }
292     /********************
293       Delete group canceled  
294      ********************/
296     /* Delete group canceled? */
297     if (isset($_POST['delete_cancel'])){
298       del_lock ($this->dn);
299       unset($_SESSION['objectinfo']);
300     }
301     
303     /********************
304       A dialog was canceled  
305      ********************/
307     /* Cancel dialogs */
308     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
309       if(isset($this->grouptab->dn)){
310         del_lock ($this->grouptab->dn);
311       }
312       unset ($this->grouptab);
313       $this->grouptab= NULL;
314       unset($_SESSION['objectinfo']);
315     }
318     /********************
319       If there is currently a dialog open, display it
320      ********************/
322     /* Show tab dialog if object is present */
323     if (isset($this->grouptab->config)){
324       $display= $this->grouptab->execute();
326       /* Don't show buttons if tab dialog requests this */
327       if(isset($this->grouptab->by_object)){
328         if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
329           $display.= "<p style=\"text-align:right\">\n";
330           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
331           $display.= "&nbsp;\n";
332           if ($this->dn != "new"){
333             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
334             $display.= "&nbsp;\n";
335           }
336           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
337           $display.= "</p>";
338         }
339       }
340       return ($display);
341     }
343     /* Display dialog with group list */
344     $this->DivListGroup->parent = $this;
345     $this->DivListGroup->execute();
347     /* Add departments if subsearch is disabled */
348     if(!$this->DivListGroup->SubSearch){
349       $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
350     }
351     $this->reload ();
352     $this->DivListGroup->setEntries($this->grouplist);
353     return($this->DivListGroup->Draw());
354   }
356   function reload($CreatePosixsList=false)
357   {
358     $this->grouplist        = array();
359     $primaries              = array();
360     $functional             = array();
361     $error= $error2         = "";
362     $filter                 = "(objectclass=posixGroup)";
364     $base                   = $this->DivListGroup->selectedBase;
365     $Regex                  = $this->DivListGroup->Regex;
366     $UserRegex              = $this->DivListGroup->UserRegex;
367     $SubSearch              = $this->DivListGroup->SubSearch;
368     $ShowPrimaryGroups      = $this->DivListGroup->ShowPrimaryGroups;
369     $ShowSambaGroups        = $this->DivListGroup->ShowSambaGroups;
370     $ShowApplicationGroups  = $this->DivListGroup->ShowApplicationGroups;
371     $ShowMailGroups         = $this->DivListGroup->ShowMailGroups;
372     $ShowFunctionalGroups   = $this->DivListGroup->ShowFunctionalGroups;
374     /* Prepare ldap class */
375     $ldap= $this->config->get_ldap_link();
376     $ldap->cd($base);
377     $ldap->set_size_limit($_SESSION['size_limit']);
380     /********************
381       Create filter depending on selected checkboxes 
382      ********************/
384     /* Add application groups */
385     if ($ShowApplicationGroups){
386       $filter.= "(objectClass=gosaApplicationGroup)";
387     }
388     
389     /* Add Mail Groups */
390     if ($ShowMailGroups){
391       $filter.= "(objectClass=gosaMailAccount)";
392     }
394     $sfilter= "";
395     if ($this->config->current['SAMBAVERSION'] == 3){
396       if (!$ShowPrimaryGroups){
397         $sfilter= "(objectClass=sambaGroupMapping)";
398       } elseif ($ShowSambaGroups){
399         $filter.= "(objectClass=sambaGroupMapping)";
400       }
401     }
403     /* Prepare filter for given Regex && UserRegex */
404     if ($filter != ""){
405       $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
406       if ($UserRegex != ""){
407         $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
408       }
409     }
412     /********************
413       Collect some groupids to be able to skip primary & functional groups 
414      ********************/
416     /* Collect primary groupIDs to show primary groups 
417        if this option is enabled in gosa conf && the checkbox is checked */  
418     if ($this->ShowPrimaryCheckBox){
419       $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", $this->ui->subtreeACL,
420           $base,array("gidNumber", "cn"),  GL_SUBSEARCH);
421       foreach ($res as $attrs){
422         $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
423       }
424     }
426     /* Collect all GroupIDs from those groups which are functional.
427        Only perfrom this search if  ShowFunctionalGroups  is unchecked, else leave arre empty  */ 
428     $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
429     $res = get_list($ff,$this->ui->subtreeACL,$base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
430     foreach($res as $attrs){
431       if (!isset($primaries[$attrs['gidNumber'][0]])){
432         $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
433       }
434     }
435    
437     /********************
438       Search for the prepared filter 
439      ********************/
440    
441     /* If subsearch is activated search for subobjects too */
442     $attrs = array("cn", "description", "gidNumber", "objectClass","FAIrelease");
444     if ($SubSearch){
445       $res= get_list($filter, $this->ui->subtreeACL, $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
446     } else {
447       $res= get_list($filter, $this->ui->subtreeACL, get_groups_ou().$base, $attrs, GL_SIZELIMIT);
448     }
450     /* Sort values into grouplist*/
451     $tmp = $tmp2 = array();
452     foreach ($res as $value){
453       /* Skip functional groups if checkbox isn't checked */
454       if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
455         continue;
456       }
457       
458       /* If gidNumber is in $primaries skip this entry */
459       if (($ShowPrimaryGroups)   ||   (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
460         $tmp2[$value['cn'][0]] = $value;
461         $tmp [$value['cn'][0]] = $value['cn'][0];
462       }
463     }
464     natcasesort($tmp);
465     foreach($tmp as $name){
466       $this->grouplist[] = $tmp2[$name]; 
467     }
468     reset ($this->grouplist);
469   }
473   /* Perform copy & paste requests
474      If copy&paste is in progress this returns a dialog to fix required attributes 
475    */ 
476   function copyPasteHandling($s_action,$s_entry)
477   {
478     /* Paste copied/cutted object in here
479      */
480     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
481       $this->CopyPasteHandler->save_object();
482       $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
483       if($str = $this->CopyPasteHandler->execute()){
484         return( $str);
485       };
486       /* Ensure that the new object is shown in the list now */
487       $this->reload();
488     }
491     /* Copy current object to CopyHandler
492      */
493     if($s_action == "copy"){
494       $this->CopyPasteHandler->Clear();
496       $dn     = $this->grouplist[trim($s_entry)]['dn'];
497       $acl    = get_permissions ($dn, $this->ui->subtreeACL);
499       $obj    = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
500       $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
501       $obj->set_acl($acl);
502       $objNew->set_acl($acl);
503       $this->CopyPasteHandler->Copy($obj,$objNew);
504     }
507     /* Copy current object to CopyHandler
508      */
509     if($s_action == "cut"){
510       $this->CopyPasteHandler->Clear();
511       $dn = $this->grouplist[trim($s_entry)]['dn'];
512       $acl= get_permissions ($dn, $this->ui->subtreeACL);
514       $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
515       $obj->set_acl($acl);
517       $this->CopyPasteHandler->Cut($obj);
518     }
519   }
521   /* Save data to object */
522   function save_object()
523   {
524     $this->DivListGroup->save_object();
525         $once = true;
526     foreach($_POST as $name => $value){
528       /* Create a new snapshot, display a dialog */
529       if(preg_match("/^CreateSnapShot_/",$name) && $once){
530         $once = false;
531         $entry = preg_replace("/^CreateSnapShot_/","",$name);
532         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
533         $this->grouptab = new SnapShotDialog($this->config,$entry,$this);
534       }
536       /* Restore a snapshot, display a dialog with all snapshots of the current object */
537       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
538         $once = false;
539         $entry = preg_replace("/^RestoreSnapShot_/","",$name);
540         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
541         $this->grouptab = new SnapShotDialog($this->config,$entry,$this);
542         $this->grouptab->Restore = true;
543       }
545       /* Restore one of the already deleted objects */
546       if(preg_match("/^RestoreDeletedSnapShot_/",$name) && $once){
547         $once = false;
548         $entry = get_groups_ou().$this->DivListGroup->selectedBase;
549         $this->grouptab = new SnapShotDialog($this->config,$entry,$this);
550         $this->grouptab->Restore      = true;
551         $this->grouptab->DeletedOnes  = true;
552       }
553     }
555     /* Create a new snapshot requested, check
556        the given attributes and create the snapshot*/
557     if(isset($_POST['CreateSnapshot'])){
558       $this->grouptab->save_object();
559       $msgs = $this->grouptab->check();
560       if(count($msgs)){
561         foreach($msgs as $msg){
562           print_red($msg);
563         }
564       }else{
565         $this->dn =  $this->grouptab->dn;
566         $this->create_snapshot("snapshot",$this->grouptab->CurrentDescription);
567         $this->grouptab = NULL;
568       }
569     }
571     /* Restore is requested, restore the object with the posted dn .*/
572     if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
573       $entry =trim($_POST['SnapShot']);
574       if(!empty($entry)){
575         $entry = base64_decode($entry);
576         $this->restore_snapshot($entry);
577         $this->grouptab = NULL;
578       }
579     }
580   }
583   function remove_lock()
584   {
585     if (isset($this->grouptab->dn)){
586       del_lock ($this->grouptab->dn);
587     }
588   }
591   function remove_from_parent()
592   {
593     /* Optionally execute a command after we're done */
594     $this->postremove();
595   }
598   /* Save to LDAP */
599   function save()
600   {
601     /* Optionally execute a command after we're done */
602     $this->postcreate();
603   }
605   /* Unused functions  */
606   function check()  { }
607   function adapt_from_template($dn) { }
608   function password_change_needed()  { }
609   function show_header($button_text, $text, $disabled= FALSE)  { }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>