Code

Added unified messages
[gosa.git] / gosa-core / 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= "Manage object groups";
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::set('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     }
112     /* handle C&P from layers menu */
113     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
114       $s_action = "copy_multiple";
115     }
116     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
117       $s_action = "cut_multiple";
118     }
119     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
120       $s_action = "editPaste";
121     }
123     /* Create options */
124     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "group_new"){
125       $s_action = "new";
126     }
128     /* handle remove from layers menu */
129     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
130       $s_action = "del_multiple";
131     }
133     /* Hanlde notification event requests */
134     if(isset($_POST['menu_action']) && preg_match("/^trigger_event_DaemonEvent_notify/",$_POST['menu_action'])){
135       $s_action = "notify";
136     }
138     /********************
139       Create notification event
140      ********************/
142     if($s_action == "notify"){
144       $ids = $this->list_get_selected_items();
145       $uids = array();
146       foreach($ids as $id){
147         $uids[] = $this->ogrouplist[$id]['cn'][0];
148       }
149       if(count($uids)){
150         $this->ogroup= new DaemonEvent_notify($this->config);
151         $this->ogroup->add_targets($uids);
152       }
154     }
156     /* Abort event dialog */
157     if(isset($_POST['abort_event_dialog'])){
158       $this->ogroup = FALSE;
159     }
161     /* Save event */
162     if(isset($_POST['save_event_dialog'])){
163       $o_queue = new gosaSupportDaemon();
164       $o_queue->append($this->ogroup);
165       if($o_queue->is_error()){
166         msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
167               $o_queue->get_error()),ERROR_DIALOG);
168       }else{
169         $this->ogroup = FALSE;
170       }
171     }
173     /* Display event */
174     if($this->ogroup instanceof DaemonEvent){
175       $this->ogroup->save_object();
176       return($this->ogroup->execute());
177     }
180     /****************
181       Copy & Paste handling 
182      ****************/
184     /* Display the copy & paste dialog, if it is currently open */
185     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
186     if($ret){
187       return($ret);
188     }
191     /***************
192       Create a new object group 
193      ****************/
195     /* New group? */
196     if ($s_action=="new"){
198       /* By default we set 'dn' to 'new', all relevant plugins will
199          react on this. */
200       $this->dn= "new";
202       /* Create new ogroup- object */
203       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
204       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
205     }
208     /********************
209       Delete MULTIPLE entries requested, display confirm dialog
210      ********************/
211     if ($s_action=="del_multiple"){
212       $ids = $this->list_get_selected_items();
213       if(count($ids)){
214         foreach($ids as $id){
215           $dn = $this->ogrouplist[$id]['dn'];
216           if (($user= get_lock($dn)) != ""){
217             return(gen_locked_message ($user, $dn));
218           }
219           $this->dns[$id] = $dn;
220         }
221         $dns_names = "<br><pre>";
222         foreach($this->dns as $dn){
223           add_lock ($dn, $this->ui->dn);
224           $dns_names .= $dn."\n";
225         }
226         $dns_names .="</pre>";
227         /* Lock the current entry, so nobody will edit it during deletion */
228         if (count($this->dns) == 1){
229           $smarty->assign("info",     sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
230         } else {
231           $smarty->assign("info",     sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
232         }
233         $smarty->assign("multiple", true);
234         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
235       }
236     }
239     /********************
240       Delete MULTIPLE entries confirmed
241      ********************/
243     /* Confirmation for deletion has been passed. Users should be deleted. */
244     if (isset($_POST['delete_multiple_ogroup_confirm'])){
246       /* Remove user by user and check acls before removeing them */
247       foreach($this->dns as $key => $dn){
249         $acl = $this->ui->get_permissions($dn, "ogroups");
250         if (preg_match('/d/', $acl)){
252           /* Delete request is permitted, perform LDAP action */
253           $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
254           $this->ogroup->delete ();
255           unset ($this->ogroup);
256           $this->ogroup= NULL;
257         } else {
259           /* Normally this shouldn't be reached, send some extra
260              logs to notify the administrator */
261           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
262           new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
263         }
264         /* Remove lock file after successfull deletion */
265         del_lock ($dn);
266         unset($this->dns[$key]);
267       }
268     }
271     /********************
272       Delete MULTIPLE entries Canceled
273      ********************/
275     /* Remove lock */
276     if(isset($_POST['delete_multiple_ogroup_cancel'])){
277       foreach($this->dns as $key => $dn){
278         del_lock ($dn);
279         unset($this->dns[$key]);
280       }
281     }
284     /****************
285       Delete object group
286      ****************/
288     if ($s_action=="del"){
290       /* Get 'dn' from posted 'uid' */
291       $this->dn= $this->ogrouplist[$s_entry]['dn'];
293       /* Load permissions for selected 'dn' and check if
294          we're allowed to remove this 'dn' */
295       $acl = $this->ui->get_permissions($this->dn,"ogroups");
296       if(preg_match("/d/",$acl)){
298         /* Check locking, save current plugin in 'back_plugin', so
299            the dialog knows where to return. */
300         if (($user= get_lock($this->dn)) != ""){
301           return(gen_locked_message ($user, $this->dn));
302         }
304         /* Lock the current entry, so nobody will edit it during deletion */
305         add_lock ($this->dn, $this->ui->dn);
306         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
307         $smarty->assign("multiple", false);
308         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
309       } else {
311         /* Obviously the user isn't allowed to delete. Show message and
312            clean session. */
313         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
314       }
315     }
318     /****************
319       Delete confirmed 
320      ****************/
322     /* Confirmation for deletion has been passed. Group should be deleted. */
323     if (isset($_POST['delete_group_confirm'])){
325       /* Some nice guy may send this as POST, so we've to check
326          for the permissions again. */
327       $acl = $this->ui->get_permissions($this->dn,"ogroups");
328       if(preg_match("/d/",$acl)){
330         /* Delete request is permitted, perform LDAP action */
331         $this->ogroup= new ogrouptabs($this->config,
332             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
333         $this->ogroup->delete ();
334         unset ($this->ogroup);
335         $this->ogroup= NULL;
336       } else {
338         /* Normally this shouldn't be reached, send some extra
339            logs to notify the administrator */
340         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
341         new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
342       }
344       /* Remove lock file after successfull deletion */
345       del_lock ($this->dn);
346       session::un_set('objectinfo');
347     }
350     /****************
351       Delete object group Canceled
352      ****************/
354     /* Delete group canceled? */
355     if (isset($_POST['delete_cancel'])){
356       del_lock ($this->dn);
357       session::un_set('objectinfo');
358     }
361     /****************
362       Edit group
363      ****************/
365     if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
367       /* Get 'dn' from posted 'uid', must be unique */
368       $this->dn= $this->ogrouplist[$s_entry]['dn'];
370       /* Check locking, save current plugin in 'back_plugin', so
371          the dialog knows where to return. */
372       if (($user= get_lock($this->dn)) != ""){
373         return(gen_locked_message ($user, $this->dn));
374       }
376       /* Lock the current entry, so everyone will get the
377          above dialog */
378       add_lock ($this->dn, $this->ui->dn);
380       /* Register grouptab to trigger edit dialog */
381       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
382       $this->ogroup->set_acl_base($this->dn);
383       session::set('objectinfo',$this->dn);
384     }
387     /****************
388       Edit finished save 
389      ****************/
391     /* Finish button has been pressed */
392     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
394       /* Check tabs, will feed message array */
395       $message= $this->ogroup->check();
397       /* Save, or display error message? */
398       if (count($message) == 0){
400         /* Save user data to ldap */
401         $this->ogroup->save();
403         if (!isset($_POST['edit_apply'])){
404           /* Group has been saved successfully, remove lock from
405              LDAP. */
406           if ($this->dn != "new"){
407             del_lock ($this->dn);
408           }
410           unset ($this->ogroup);
411           $this->ogroup= NULL;
412           session::un_set('objectinfo');
413         }
414       } else {
415         /* Ok. There seem to be errors regarding to the tab data,
416            show message and continue as usual. */
417         show_errors($message);
418       }
419     }
422     /****************
423       Cancel edit object group
424      ****************/
426     /* Cancel dialogs */
427     if ((isset($_POST['edit_cancel']))  && (isset($this->ogroup->dn))){
428       del_lock ($this->ogroup->dn);
429       unset ($this->ogroup);
430       $this->ogroup= NULL;
431       session::un_set('objectinfo');
432     }
435     /****************
436       Display open dialogs 
437      ****************/
439     /* Show dialog if object is present */
440     if (isset($this->ogroup->config)){
441       $display= $this->ogroup->execute();
443       /* Don't show buttons if tab dialog requests this */
444       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
445         $display.= "<p style=\"text-align:right\">\n";
446         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
447         $display.= "&nbsp;\n";
448         if ($this->dn != "new"){
449           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
450           $display.= "&nbsp;\n";
451         }
452         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
453         $display.= "</p>";
454       }
455       return ($display);
456     }
459     /****************
460       Display list 
461      ****************/
463     /* Check if there is a snapshot dialog open */
464     $base = $this->DivListOGroup->selectedBase;
465     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
466       return($str);
467     }
469     /* Display dialog with group list */
470     $this->DivListOGroup->parent = $this;
471     $this->DivListOGroup->execute();
473     /* Add departments if subsearch is disabled */
474     if(!$this->DivListOGroup->SubSearch){
475       $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
476     }
477     $this->reload ();
478     $this->DivListOGroup->setEntries($this->ogrouplist);
479     return($this->DivListOGroup->Draw());
480   }
483   /* Return departments, that will be included within snapshot detection */
484   function get_used_snapshot_bases()
485   {
486     return(array(get_ou('ogroupou').$this->DivListOGroup->selectedBase));
487   }
489   
491   function convert_list($input)
492   {
493     $temp= "";
495     $conv= array(   
496         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
497         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
498         "A" => array("select_application.png" ,_("Application") , "ogroup"),
499         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
500         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
501         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
502         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
503         "O" => array("select_winstation.png" ,_("Windows Install") , "ogroup"),
504         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
505         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
507     /* Assemble picture */
508     $type= $input['gosaGroupObjects'][0];
509     $type= preg_replace("/[^A-Z]/i","",$type);
510     if (isset($type[0])){
511       $p1['pic']= $conv[$type[0]][0];
512       $p1['tit']= $conv[$type[0]][1];
513       $p1['alt']= $type[0];
514       $p1['lnk']= $conv[$type[0]][2];
515       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
516     } else {
517       $p1['pic']= "empty.png";
518       $p1['tit']= "";
519       $p1['alt']= "";
520       $p1['lnk']= "";//$conv[$type[0]][2];
521       $html_object_1 = "<img ";
522     }
524     if (isset($type[1])){
525       $p2['pic']= $conv[$type[1]][0];
526       $p2['alt']= $type[1];
527       $p2['tit']= $conv[$type[1]][1];
528       $p2['lnk']= $conv[$type[1]][2];
529       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
530     } else {
531       $p2['pic']= "empty.png";
532       $p2['alt']= "";
533       $p2['tit']= "";
534       $p2['lnk']= ""; //$conv[$type[0]][2];
535       $html_object_2 = "<img ";
536     }
537     
538     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
539       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
540     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
541       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
542     return ($temp);
543   }
546   function reload()
547   {
548     /* Set base for all searches && initialise some vars */
549     $this->ogrouplist= array();
550     $base     = $this->DivListOGroup->selectedBase;
551     $filter   = "(gosaGroupObjects=[])";
552     $Regex    = $this->DivListOGroup->Regex;
554     $chk = array(
555         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
556         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
557         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
558         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
559         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
560         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
561         "WindowsGroups"       => "(gosaGroupObjects=*O*)" ,
562         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
563         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
564         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
565     /* Create filter */ 
566     foreach($chk as $chkBox => $FilterPart){
567       if($this->DivListOGroup->$chkBox){
568         $filter .=  $FilterPart;
569       }
570     }
571     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
573     if($this->DivListOGroup->SubSearch){
574       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
575     }else{
576       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
577     }
579     $this->ogrouplist= $res;
580     ksort ($this->ogrouplist);
581     reset ($this->ogrouplist);
582     $tmp=array();
583     $tmp2 = array();
584     foreach($this->ogrouplist as $tkey => $val ){
585       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
586       $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
587     }
588     natcasesort($tmp2);
589     $this->ogrouplist=array();
590     foreach($tmp2 as $val){
591       $this->ogrouplist[]=$tmp[$val];
592     }
593     reset ($this->ogrouplist);
594   }
597   function list_get_selected_items()
598   {
599     $ids = array();
600     foreach($_POST as $name => $value){
601       if(preg_match("/^item_selected_[0-9]*$/",$name)){
602         $id   = preg_replace("/^item_selected_/","",$name);
603         $ids[$id] = $id;
604       }
605     }
606     return($ids);
607   }
610   function copyPasteHandling_from_queue($s_action,$s_entry)
611   {
612     /* Check if Copy & Paste is disabled */
613     if(!is_object($this->CopyPasteHandler)){
614       return("");
615     }
617     /* Add a single entry to queue */
618     if($s_action == "cut" || $s_action == "copy"){
620       /* Cleanup object queue */
621       $this->CopyPasteHandler->cleanup_queue();
622       $dn = $this->ogrouplist[$s_entry]['dn'];
623       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
624     }
627     /* Add entries to queue */
628     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
630       /* Cleanup object queue */
631       $this->CopyPasteHandler->cleanup_queue();
633       /* Add new entries to CP queue */
634       foreach($this->list_get_selected_items() as $id){
635         $dn = $this->ogrouplist[$id]['dn'];
637         if($s_action == "copy_multiple"){
638           $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
639         }
640         if($s_action == "cut_multiple"){
641           $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
642         }
643       }
644     }
646     /* Start pasting entries */
647     if($s_action == "editPaste"){
648       $this->start_pasting_copied_objects = TRUE;
649     }
651     /* Return C&P dialog */
652     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
654       /* Get dialog */
655       $data = $this->CopyPasteHandler->execute();
656       $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
658       /* Return dialog data */
659       if(!empty($data)){
660         return($data);
661       }
662     }
663     /* Automatically disable status for pasting */
664     if(!$this->CopyPasteHandler->entries_queued()){
665       $this->start_pasting_copied_objects = FALSE;
666     }
667     return("");
668   }
671   function save_object()
672   {
673     $this->DivListOGroup->save_object();
674   }
678 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
679 ?>