Code

Added seperate list handler for gotomasses.
[gosa.git] / plugins / admin / ogroups / class_ogroupManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004 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  */
21 class ogroupManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline= "Object groups";
25   var $plDescription= "This does something";
27   /* attribute list for save action */
28   var $attributes= array();
29   var $objectclasses= array();
30   var $obtypes= array();
31   var $ogroup;
33   var $CopyPasteHandler = NULL;
34   var $DivListOGroup    = NULL;
35   var $start_pasting_copied_objects = FALSE;
37   function ogroupManagement (&$config, $dn= NULL)
38   {
39     /* Include config object */
40     $this->config= &$config;
41     $this->ui= get_userinfo();
43     /* Copy & Paste enabled ?
44      */
45     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
46       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47     }
49     /* Div lsit management */
50     $this->DivListOGroup = new divListOGroup($this->config,$this);
51   }
53   function execute()
54   {
55     /* Call parent execute */
56     plugin::execute();
58     /****************
59       Variable intialisation && Check posts for commands  
60      ****************/
62     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/");
64     $smarty     = get_smarty();
65     $s_action   = "";
66     $s_entry    = "";
68     /* Test Posts */
69     foreach($_POST as $key => $val){
70       // Post for delete
71       if(preg_match("/^group_del.*/",$key)){
72         $s_action = "del";
73         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
74         // Post for edit
75       }elseif(preg_match("/^group_edit_.*/",$key)){
76         $s_action="edit";
77         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
78         // Post for new
79       }elseif(preg_match("/^group_new.*/",$key)){
80         $s_action="new";
81       }elseif(preg_match("/^editPaste.*/i",$key)){
82         $s_action="editPaste";
83       }elseif(preg_match("/^copy_.*/",$key)){
84         $s_action="copy";
85         $s_entry  = preg_replace("/^copy_/i","",$key);
86       }elseif(preg_match("/^cut_.*/",$key)){
87         $s_action="cut";
88         $s_entry  = preg_replace("/^cut_/i","",$key);
89       }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
90         $s_action="del_multiple";
91       }elseif(preg_match("/^multiple_cut_ogroups/",$key)){
92         $s_action = "cut_multiple";
93       }elseif(preg_match("/^multiple_copy_ogroups/",$key)){
94         $s_action = "copy_multiple";
95       }elseif(preg_match("/_group_edit_/",$key)){
96         $type = preg_replace("/_group_edit_.*$/","",$key);
97         $s_action="edit";
98         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key);
99         $_POST['arg'] = $type;
100       }
101     }
102     $s_entry  = preg_replace("/_.$/","",$s_entry);
104     // Edit if
105     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
106       $s_action ="edit";
107       $s_entry  = $_GET['id'];
108     }
111     /****************
112       Copy & Paste handling 
113      ****************/
115     /* Display the copy & paste dialog, if it is currently open */
116     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
117     if($ret){
118       return($ret);
119     }
122     /***************
123       Create a new object group 
124      ****************/
126     /* New group? */
127     if ($s_action=="new"){
129       /* By default we set 'dn' to 'new', all relevant plugins will
130          react on this. */
131       $this->dn= "new";
133       /* Create new usertab object */
134       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
135       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
136     }
139     /********************
140       Delete MULTIPLE entries requested, display confirm dialog
141      ********************/
142     if ($s_action=="del_multiple"){
143       $ids = $this->list_get_selected_items();
144       if(count($ids)){
145         foreach($ids as $id){
146           $dn = $this->ogrouplist[$id]['dn'];
147           if (($user= get_lock($dn)) != ""){
148             return(gen_locked_message ($user, $dn));
149           }
150           $this->dns[$id] = $dn;
151         }
152         $dns_names = "<br><pre>";
153         foreach($this->dns as $dn){
154           add_lock ($dn, $this->ui->dn);
155           $dns_names .= $dn."\n";
156         }
157         $dns_names .="</pre>";
158         /* Lock the current entry, so nobody will edit it during deletion */
159         if (count($this->dns) == 1){
160           $smarty->assign("info",     sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
161         } else {
162           $smarty->assign("info",     sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
163         }
164         $smarty->assign("multiple", true);
165         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
166       }
167     }
170     /********************
171       Delete MULTIPLE entries confirmed
172      ********************/
174     /* Confirmation for deletion has been passed. Users should be deleted. */
175     if (isset($_POST['delete_multiple_ogroup_confirm'])){
177       /* Remove user by user and check acls before removeing them */
178       foreach($this->dns as $key => $dn){
180         $acl = $this->ui->get_permissions($dn, "ogroups");
181         if (preg_match('/d/', $acl)){
183           /* Delete request is permitted, perform LDAP action */
184           $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
185           $this->ogroup->delete ();
186           unset ($this->ogroup);
187           $this->ogroup= NULL;
188         } else {
190           /* Normally this shouldn't be reached, send some extra
191              logs to notify the administrator */
192           print_red (_("You are not allowed to delete this object group!"));
193           new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
194         }
195         /* Remove lock file after successfull deletion */
196         del_lock ($dn);
197         unset($this->dns[$key]);
198       }
199     }
202     /********************
203       Delete MULTIPLE entries Canceled
204      ********************/
206     /* Remove lock */
207     if(isset($_POST['delete_multiple_ogroup_cancel'])){
208       foreach($this->dns as $key => $dn){
209         del_lock ($dn);
210         unset($this->dns[$key]);
211       }
212     }
215     /****************
216       Delete object group
217      ****************/
219     if ($s_action=="del"){
221       /* Get 'dn' from posted 'uid' */
222       $this->dn= $this->ogrouplist[$s_entry]['dn'];
224       /* Load permissions for selected 'dn' and check if
225          we're allowed to remove this 'dn' */
226       $acl = $this->ui->get_permissions($this->dn,"ogroups");
227       if(preg_match("/d/",$acl)){
229         /* Check locking, save current plugin in 'back_plugin', so
230            the dialog knows where to return. */
231         if (($user= get_lock($this->dn)) != ""){
232           return(gen_locked_message ($user, $this->dn));
233         }
235         /* Lock the current entry, so nobody will edit it during deletion */
236         add_lock ($this->dn, $this->ui->dn);
237         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
238         $smarty->assign("multiple", false);
239         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
240       } else {
242         /* Obviously the user isn't allowed to delete. Show message and
243            clean session. */
244         print_red (_("You are not allowed to delete this object group!"));
245       }
246     }
249     /****************
250       Delete confirmed 
251      ****************/
253     /* Confirmation for deletion has been passed. Group should be deleted. */
254     if (isset($_POST['delete_group_confirm'])){
256       /* Some nice guy may send this as POST, so we've to check
257          for the permissions again. */
258       $acl = $this->ui->get_permissions($this->dn,"ogroups");
259       if(preg_match("/d/",$acl)){
261         /* Delete request is permitted, perform LDAP action */
262         $this->ogroup= new ogrouptabs($this->config,
263             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
264         $this->ogroup->delete ();
265         unset ($this->ogroup);
266         $this->ogroup= NULL;
267       } else {
269         /* Normally this shouldn't be reached, send some extra
270            logs to notify the administrator */
271         print_red (_("You are not allowed to delete this object group!"));
272         new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
273       }
275       /* Remove lock file after successfull deletion */
276       del_lock ($this->dn);
277       unset($_SESSION['objectinfo']);
278     }
281     /****************
282       Delete object group Canceled
283      ****************/
285     /* Delete group canceled? */
286     if (isset($_POST['delete_cancel'])){
287       del_lock ($this->dn);
288       unset($_SESSION['objectinfo']);
289     }
292     /****************
293       Edit group
294      ****************/
296     if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
298       /* Get 'dn' from posted 'uid', must be unique */
299       $this->dn= $this->ogrouplist[$s_entry]['dn'];
301       /* Check locking, save current plugin in 'back_plugin', so
302          the dialog knows where to return. */
303       if (($user= get_lock($this->dn)) != ""){
304         return(gen_locked_message ($user, $this->dn));
305       }
307       /* Lock the current entry, so everyone will get the
308          above dialog */
309       add_lock ($this->dn, $this->ui->dn);
311       /* Register grouptab to trigger edit dialog */
312       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
313       $this->ogroup->set_acl_base($this->dn);
314       $_SESSION['objectinfo']= $this->dn;
315     }
318     /****************
319       Edit finished save 
320      ****************/
322     /* Finish button has been pressed */
323     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
325       /* Check tabs, will feed message array */
326       $message= $this->ogroup->check();
328       /* Save, or display error message? */
329       if (count($message) == 0){
331         /* Save user data to ldap */
332         $this->ogroup->save();
334         if (!isset($_POST['edit_apply'])){
335           /* Group has been saved successfully, remove lock from
336              LDAP. */
337           if ($this->dn != "new"){
338             del_lock ($this->dn);
339           }
341           unset ($this->ogroup);
342           $this->ogroup= NULL;
343           unset ($_SESSION['objectinfo']);
344         }
345       } else {
346         /* Ok. There seem to be errors regarding to the tab data,
347            show message and continue as usual. */
348         show_errors($message);
349       }
350     }
353     /****************
354       Cancel edit object group
355      ****************/
357     /* Cancel dialogs */
358     if ((isset($_POST['edit_cancel']))  && (isset($this->ogroup->dn))){
359       del_lock ($this->ogroup->dn);
360       unset ($this->ogroup);
361       $this->ogroup= NULL;
362       unset($_SESSION['objectinfo']);
363     }
366     /****************
367       Display open dialogs 
368      ****************/
370     /* Show dialog if object is present */
371     if (isset($this->ogroup->config)){
372       $display= $this->ogroup->execute();
374       /* Don't show buttons if tab dialog requests this */
375       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
376         $display.= "<p style=\"text-align:right\">\n";
377         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
378         $display.= "&nbsp;\n";
379         if ($this->dn != "new"){
380           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
381           $display.= "&nbsp;\n";
382         }
383         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
384         $display.= "</p>";
385       }
386       return ($display);
387     }
390     /****************
391       Display list 
392      ****************/
394     /* Check if there is a snapshot dialog open */
395     $base = $this->DivListOGroup->selectedBase;
396     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
397       return($str);
398     }
400     /* Display dialog with group list */
401     $this->DivListOGroup->parent = $this;
402     $this->DivListOGroup->execute();
404     /* Add departments if subsearch is disabled */
405     if(!$this->DivListOGroup->SubSearch){
406       $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
407     }
408     $this->reload ();
409     $this->DivListOGroup->setEntries($this->ogrouplist);
410     return($this->DivListOGroup->Draw());
411   }
414   /* Return departments, that will be included within snapshot detection */
415   function get_used_snapshot_bases()
416   {
417     return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
418   }
420   
422   function convert_list($input)
423   {
424     $temp= "";
426     $conv= array(   
427         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
428         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
429         "A" => array("select_application.png" ,_("Application") , "ogroup"),
430         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
431         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
432         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
433         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
434         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
435         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
437     /* Assemble picture */
438     $type= $input['gosaGroupObjects'][0];
439     $type= preg_replace("/[^A-Z]/i","",$type);
440     if (isset($type[0])){
441       $p1['pic']= $conv[$type[0]][0];
442       $p1['tit']= $conv[$type[0]][1];
443       $p1['alt']= $type[0];
444       $p1['lnk']= $conv[$type[0]][2];
445       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
446     } else {
447       $p1['pic']= "empty.png";
448       $p1['tit']= "";
449       $p1['alt']= "";
450       $p1['lnk']= "";//$conv[$type[0]][2];
451       $html_object_1 = "<img ";
452     }
454     if (isset($type[1])){
455       $p2['pic']= $conv[$type[1]][0];
456       $p2['alt']= $type[1];
457       $p2['tit']= $conv[$type[1]][1];
458       $p2['lnk']= $conv[$type[1]][2];
459       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
460     } else {
461       $p2['pic']= "empty.png";
462       $p2['alt']= "";
463       $p2['tit']= "";
464       $p2['lnk']= ""; //$conv[$type[0]][2];
465       $html_object_2 = "<img ";
466     }
467     
468     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
469       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
470     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
471       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
472     return ($temp);
473   }
476   function reload()
477   {
478     /* Set base for all searches && initialise some vars */
479     $this->ogrouplist= array();
480     $base     = $this->DivListOGroup->selectedBase;
481     $filter   = "(gosaGroupObjects=[])";
482     $Regex    = $this->DivListOGroup->Regex;
484     $chk = array(
485         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
486         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
487         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
488         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
489         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
490         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
491         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
492         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
493         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
494     /* Create filter */ 
495     foreach($chk as $chkBox => $FilterPart){
496       if($this->DivListOGroup->$chkBox){
497         $filter .=  $FilterPart;
498       }
499     }
500     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
502     if($this->DivListOGroup->SubSearch){
503       $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
504     }else{
505       $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
506     }
508     $this->ogrouplist= $res;
509     ksort ($this->ogrouplist);
510     reset ($this->ogrouplist);
511     $tmp=array();
512     foreach($this->ogrouplist as $tkey => $val ){
513       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
514     }
515     ksort($tmp);
516     $this->ogrouplist=array();
517     foreach($tmp as $val){
518       $this->ogrouplist[]=$val;
519     }
520     reset ($this->ogrouplist);
521   }
524   function list_get_selected_items()
525   {
526     $ids = array();
527     foreach($_POST as $name => $value){
528       if(preg_match("/^item_selected_[0-9]*$/",$name)){
529         $id   = preg_replace("/^item_selected_/","",$name);
530         $ids[$id] = $id;
531       }
532     }
533     return($ids);
534   }
537   function copyPasteHandling_from_queue($s_action,$s_entry)
538   {
539     /* Check if Copy & Paste is disabled */
540     if(!is_object($this->CopyPasteHandler)){
541       return("");
542     }
544     /* Add a single entry to queue */
545     if($s_action == "cut" || $s_action == "copy"){
547       /* Cleanup object queue */
548       $this->CopyPasteHandler->cleanup_queue();
549       $dn = $this->ogrouplist[$s_entry]['dn'];
550       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
551     }
554     /* Add entries to queue */
555     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
557       /* Cleanup object queue */
558       $this->CopyPasteHandler->cleanup_queue();
560       /* Add new entries to CP queue */
561       foreach($this->list_get_selected_items() as $id){
562         $dn = $this->ogrouplist[$id]['dn'];
564         if($s_action == "copy_multiple"){
565           $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
566         }
567         if($s_action == "cut_multiple"){
568           $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
569         }
570       }
571     }
573     /* Start pasting entries */
574     if($s_action == "editPaste"){
575       $this->start_pasting_copied_objects = TRUE;
576     }
578     /* Return C&P dialog */
579     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
581       /* Load entry from queue and set base */
582       $this->CopyPasteHandler->load_entry_from_queue();
583       $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
585       /* Get dialog */
586       $data = $this->CopyPasteHandler->execute();
588       /* Return dialog data */
589       if(!empty($data)){
590         return($data);
591       }
592     }
593     /* Automatically disable status for pasting */
594     if(!$this->CopyPasteHandler->entries_queued()){
595       $this->start_pasting_copied_objects = FALSE;
596     }
597     return("");
598   }
601   function save_object()
602   {
603     $this->DivListOGroup->save_object();
604   }
608 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
609 ?>