Code

msgPool
[gosa.git] / gosa-core / plugins / admin / ogroups / class_ogroupManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class ogroupManagement extends plugin
24 {
25   /* Definitions */
26   var $plHeadline= "Object groups";
27   var $plDescription= "Manage object groups";
29   /* attribute list for save action */
30   var $attributes= array();
31   var $objectclasses= array();
32   var $obtypes= array();
33   var $ogroup;
35   var $CopyPasteHandler = NULL;
36   var $DivListOGroup    = NULL;
37   var $start_pasting_copied_objects = FALSE;
39   function ogroupManagement (&$config, $dn= NULL)
40   {
41     /* Include config object */
42     $this->config= &$config;
43     $this->ui= get_userinfo();
45     /* Copy & Paste enabled ?
46      */
47     if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
48       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
49     }
51     /* Div lsit management */
52     $this->DivListOGroup = new divListOGroup($this->config,$this);
53   }
55   function execute()
56   {
57     /* Call parent execute */
58     plugin::execute();
60     /****************
61       Variable intialisation && Check posts for commands  
62      ****************/
64     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/"));
66     $smarty     = get_smarty();
67     $s_action   = "";
68     $s_entry    = "";
70     /* Test Posts */
71     foreach($_POST as $key => $val){
72       // Post for delete
73       if(preg_match("/^group_del.*/",$key)){
74         $s_action = "del";
75         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
76         // Post for edit
77       }elseif(preg_match("/^group_edit_.*/",$key)){
78         $s_action="edit";
79         $s_entry  = preg_replace("/^group_".$s_action."_/i","",$key);
80         // Post for new
81       }elseif(preg_match("/^group_new.*/",$key)){
82         $s_action="new";
83       }elseif(preg_match("/^editPaste.*/i",$key)){
84         $s_action="editPaste";
85       }elseif(preg_match("/^copy_.*/",$key)){
86         $s_action="copy";
87         $s_entry  = preg_replace("/^copy_/i","",$key);
88       }elseif(preg_match("/^cut_.*/",$key)){
89         $s_action="cut";
90         $s_entry  = preg_replace("/^cut_/i","",$key);
91       }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
92         $s_action="del_multiple";
93       }elseif(preg_match("/^multiple_cut_ogroups/",$key)){
94         $s_action = "cut_multiple";
95       }elseif(preg_match("/^multiple_copy_ogroups/",$key)){
96         $s_action = "copy_multiple";
97       }elseif(preg_match("/_group_edit_/",$key)){
98         $type = preg_replace("/_group_edit_.*$/","",$key);
99         $s_action="edit";
100         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key);
101         $_POST['arg'] = $type;
102       }
103     }
104     $s_entry  = preg_replace("/_.$/","",$s_entry);
106     // Edit if
107     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
108       $s_action ="edit";
109       $s_entry  = $_GET['id'];
110     }
114     /* handle C&P from layers menu */
115     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
116       $s_action = "copy_multiple";
117     }
118     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
119       $s_action = "cut_multiple";
120     }
121     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
122       $s_action = "editPaste";
123     }
125     /* Create options */
126     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "group_new"){
127       $s_action = "new";
128     }
130     /* handle remove from layers menu */
131     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
132       $s_action = "del_multiple";
133     }
135     /* Hanlde notification event requests */
136     if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
137       $s_action = $_POST['menu_action'];
138     }
141     /********************
142       Create notification event
143      ********************/
145     if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
146       $ids = $this->list_get_selected_items();
147       $uids = array();
148       foreach($ids as $id){
149         $uids[] = $this->ogrouplist[$id]['cn'][0];
150       }
151       if(count($uids)){
152         $events = DaemonEvent::get_event_types(USER_EVENT);
153         $event = preg_replace("/^event_/","",$s_action);
154         if(isset($events['BY_CLASS'][$event])){
155           $type = $events['BY_CLASS'][$event];
156           $this->ogroup = new $type['CLASS_NAME']($this->config);
157           $this->ogroup->add_targets($uids);
158           $this->ogroup->set_type(TRIGGERED_EVENT);
159         }
160       }
161     }
163     /* Abort event dialog */
164     if(isset($_POST['abort_event_dialog'])){
165       $this->ogroup = FALSE;
166     }
168     /* Save event */
169     if(isset($_POST['save_event_dialog'])){
170       $o_queue = new gosaSupportDaemon();
171       $o_queue->append($this->ogroup);
172       if($o_queue->is_error()){
173         msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
174               $o_queue->get_error()),ERROR_DIALOG);
175       }else{
176         $this->ogroup = FALSE;
177       }
178     }
180     /* Display event */
181     if($this->ogroup instanceof DaemonEvent){
182       $this->ogroup->save_object();
183       return($this->ogroup->execute());
184     }
187     /****************
188       Copy & Paste handling 
189      ****************/
191     /* Display the copy & paste dialog, if it is currently open */
192     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
193     if($ret){
194       return($ret);
195     }
198     /***************
199       Create a new object group 
200      ****************/
202     /* New group? */
203     if ($s_action=="new"){
205       /* By default we set 'dn' to 'new', all relevant plugins will
206          react on this. */
207       $this->dn= "new";
209       /* Create new ogroup- object */
210       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
211       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
212     }
215     /********************
216       Delete MULTIPLE entries requested, display confirm dialog
217      ********************/
218     if ($s_action=="del_multiple"){
219       $ids = $this->list_get_selected_items();
220       if(count($ids)){
221         foreach($ids as $id){
222           $dn = $this->ogrouplist[$id]['dn'];
223           if (($user= get_lock($dn)) != ""){
224             return(gen_locked_message ($user, $dn));
225           }
226           $this->dns[$id] = $dn;
227         }
228         $dns_names = array();
229         foreach($this->dns as $dn){
230           add_lock ($dn, $this->ui->dn);
231           $dns_names[] = @LDAP::fix($dn);
232         }
234         /* Lock the current entry, so nobody will edit it during deletion */
235         $smarty->assign("info", msgPool::deleteInfo($dns_names,_("object group")));
236         $smarty->assign("multiple", true);
237         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
238       }
239     }
242     /********************
243       Delete MULTIPLE entries confirmed
244      ********************/
246     /* Confirmation for deletion has been passed. Users should be deleted. */
247     if (isset($_POST['delete_multiple_ogroup_confirm'])){
249       /* Remove user by user and check acls before removeing them */
250       foreach($this->dns as $key => $dn){
252         $acl = $this->ui->get_permissions($dn, "ogroups");
253         if (preg_match('/d/', $acl)){
255           /* Delete request is permitted, perform LDAP action */
256           $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
257           $this->ogroup->delete ();
258           unset ($this->ogroup);
259           $this->ogroup= NULL;
260         } else {
262           /* Normally this shouldn't be reached, send some extra
263              logs to notify the administrator */
264           msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
265           new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
266         }
267         /* Remove lock file after successfull deletion */
268         del_lock ($dn);
269         unset($this->dns[$key]);
270       }
271     }
274     /********************
275       Delete MULTIPLE entries Canceled
276      ********************/
278     /* Remove lock */
279     if(isset($_POST['delete_multiple_ogroup_cancel'])){
280       foreach($this->dns as $key => $dn){
281         del_lock ($dn);
282         unset($this->dns[$key]);
283       }
284     }
287     /****************
288       Delete object group
289      ****************/
291     if ($s_action=="del"){
293       /* Get 'dn' from posted 'uid' */
294       $this->dn= $this->ogrouplist[$s_entry]['dn'];
296       /* Load permissions for selected 'dn' and check if
297          we're allowed to remove this 'dn' */
298       $acl = $this->ui->get_permissions($this->dn,"ogroups");
299       if(preg_match("/d/",$acl)){
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 nobody will edit it during deletion */
308         add_lock ($this->dn, $this->ui->dn);
309         $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("object group")));
310         $smarty->assign("multiple", false);
311         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
312       } else {
314         /* Obviously the user isn't allowed to delete. Show message and
315            clean session. */
316         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
317       }
318     }
321     /****************
322       Delete confirmed 
323      ****************/
325     /* Confirmation for deletion has been passed. Group should be deleted. */
326     if (isset($_POST['delete_group_confirm'])){
328       /* Some nice guy may send this as POST, so we've to check
329          for the permissions again. */
330       $acl = $this->ui->get_permissions($this->dn,"ogroups");
331       if(preg_match("/d/",$acl)){
333         /* Delete request is permitted, perform LDAP action */
334         $this->ogroup= new ogrouptabs($this->config,
335             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
336         $this->ogroup->delete ();
337         unset ($this->ogroup);
338         $this->ogroup= NULL;
339       } else {
341         /* Normally this shouldn't be reached, send some extra
342            logs to notify the administrator */
343         msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), INFO_DIALOG);
344         new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
345       }
347       /* Remove lock file after successfull deletion */
348       del_lock ($this->dn);
349       session::un_set('objectinfo');
350     }
353     /****************
354       Delete object group Canceled
355      ****************/
357     /* Delete group canceled? */
358     if (isset($_POST['delete_cancel'])){
359       del_lock ($this->dn);
360       session::un_set('objectinfo');
361     }
364     /****************
365       Edit group
366      ****************/
368     if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
370       /* Get 'dn' from posted 'uid', must be unique */
371       $this->dn= $this->ogrouplist[$s_entry]['dn'];
373       /* Check locking, save current plugin in 'back_plugin', so
374          the dialog knows where to return. */
375       if (($user= get_lock($this->dn)) != ""){
376         return(gen_locked_message ($user, $this->dn));
377       }
379       /* Lock the current entry, so everyone will get the
380          above dialog */
381       add_lock ($this->dn, $this->ui->dn);
383       /* Register grouptab to trigger edit dialog */
384       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
385       $this->ogroup->set_acl_base($this->dn);
386       session::set('objectinfo',$this->dn);
387     }
390     /****************
391       Edit finished save 
392      ****************/
394     /* Finish button has been pressed */
395     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
397       /* Check tabs, will feed message array */
398       $message= $this->ogroup->check();
400       /* Save, or display error message? */
401       if (count($message) == 0){
403         /* Save user data to ldap */
404         $this->ogroup->save();
406         if (!isset($_POST['edit_apply'])){
407           /* Group has been saved successfully, remove lock from
408              LDAP. */
409           if ($this->dn != "new"){
410             del_lock ($this->dn);
411           }
413           unset ($this->ogroup);
414           $this->ogroup= NULL;
415           session::un_set('objectinfo');
416         }
417       } else {
418         /* Ok. There seem to be errors regarding to the tab data,
419            show message and continue as usual. */
420         msg_dialog::displayChecks($message);
421       }
422     }
425     /****************
426       Cancel edit object group
427      ****************/
429     /* Cancel dialogs */
430     if ((isset($_POST['edit_cancel']))  && (isset($this->ogroup->dn))){
431       del_lock ($this->ogroup->dn);
432       unset ($this->ogroup);
433       $this->ogroup= NULL;
434       session::un_set('objectinfo');
435     }
438     /****************
439       Display open dialogs 
440      ****************/
442     /* Show dialog if object is present */
443     if (isset($this->ogroup->config)){
444       $display= $this->ogroup->execute();
446       /* Don't show buttons if tab dialog requests this */
447       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
448         $display.= "<p style=\"text-align:right\">\n";
449         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
450         $display.= "&nbsp;\n";
451         if ($this->dn != "new"){
452           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
453           $display.= "&nbsp;\n";
454         }
455         $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
456         $display.= "</p>";
457       }
458       return ($display);
459     }
462     /****************
463       Display list 
464      ****************/
466     /* Check if there is a snapshot dialog open */
467     $base = $this->DivListOGroup->selectedBase;
468     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
469       return($str);
470     }
472     /* Display dialog with group list */
473     $this->DivListOGroup->parent = $this;
474     $this->DivListOGroup->execute();
476     /* Add departments if subsearch is disabled */
477     if(!$this->DivListOGroup->SubSearch){
478       $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
479     }
480     $this->reload ();
481     $this->DivListOGroup->setEntries($this->ogrouplist);
482     return($this->DivListOGroup->Draw());
483   }
486   /* Return departments, that will be included within snapshot detection */
487   function get_used_snapshot_bases()
488   {
489     return(array(get_ou('ogroupou').$this->DivListOGroup->selectedBase));
490   }
492   
494   function convert_list($input)
495   {
496     $temp= "";
498     $conv= array(   
499         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
500         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
501         "A" => array("select_application.png" ,_("Application") , "ogroup"),
502         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
503         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
504         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
505         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
506         "O" => array("select_winstation.png" ,_("Windows Install") , "ogroup"),
507         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
508         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
510     /* Assemble picture */
511     $type= $input['gosaGroupObjects'][0];
512     $type= preg_replace("/[^A-Z]/i","",$type);
513     if (isset($type[0])){
514       $p1['pic']= $conv[$type[0]][0];
515       $p1['tit']= $conv[$type[0]][1];
516       $p1['alt']= $type[0];
517       $p1['lnk']= $conv[$type[0]][2];
518       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
519     } else {
520       $p1['pic']= "empty.png";
521       $p1['tit']= "";
522       $p1['alt']= "";
523       $p1['lnk']= "";//$conv[$type[0]][2];
524       $html_object_1 = "<img ";
525     }
527     if (isset($type[1])){
528       $p2['pic']= $conv[$type[1]][0];
529       $p2['alt']= $type[1];
530       $p2['tit']= $conv[$type[1]][1];
531       $p2['lnk']= $conv[$type[1]][2];
532       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
533     } else {
534       $p2['pic']= "empty.png";
535       $p2['alt']= "";
536       $p2['tit']= "";
537       $p2['lnk']= ""; //$conv[$type[0]][2];
538       $html_object_2 = "<img ";
539     }
540     
541     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
542       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
543     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
544       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
545     return ($temp);
546   }
549   function reload()
550   {
551     /* Set base for all searches && initialise some vars */
552     $this->ogrouplist= array();
553     $base     = $this->DivListOGroup->selectedBase;
554     $filter   = "(gosaGroupObjects=[])";
555     $Regex    = $this->DivListOGroup->Regex;
557     $chk = array(
558         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
559         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
560         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
561         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
562         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
563         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
564         "WindowsGroups"       => "(gosaGroupObjects=*O*)" ,
565         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
566         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
567         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
568     /* Create filter */ 
569     foreach($chk as $chkBox => $FilterPart){
570       if($this->DivListOGroup->$chkBox){
571         $filter .=  $FilterPart;
572       }
573     }
574     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
576     if($this->DivListOGroup->SubSearch){
577       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
578     }else{
579       $res= get_sub_list($filter, "ogroups",get_ou('ogroupou'), get_ou('ogroupou').$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
580     }
582     $this->ogrouplist= $res;
583     ksort ($this->ogrouplist);
584     reset ($this->ogrouplist);
585     $tmp=array();
586     $tmp2 = array();
587     foreach($this->ogrouplist as $tkey => $val ){
588       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
589       $tmp2[strtolower($val['cn'][0]).$val['cn'][0]] = strtolower($val['cn'][0]).$val['cn'][0];
590     }
591     natcasesort($tmp2);
592     $this->ogrouplist=array();
593     foreach($tmp2 as $val){
594       $this->ogrouplist[]=$tmp[$val];
595     }
596     reset ($this->ogrouplist);
597   }
600   function list_get_selected_items()
601   {
602     $ids = array();
603     foreach($_POST as $name => $value){
604       if(preg_match("/^item_selected_[0-9]*$/",$name)){
605         $id   = preg_replace("/^item_selected_/","",$name);
606         $ids[$id] = $id;
607       }
608     }
609     return($ids);
610   }
613   function copyPasteHandling_from_queue($s_action,$s_entry)
614   {
615     /* Check if Copy & Paste is disabled */
616     if(!is_object($this->CopyPasteHandler)){
617       return("");
618     }
620     /* Add a single entry to queue */
621     if($s_action == "cut" || $s_action == "copy"){
623       /* Cleanup object queue */
624       $this->CopyPasteHandler->cleanup_queue();
625       $dn = $this->ogrouplist[$s_entry]['dn'];
626       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
627     }
630     /* Add entries to queue */
631     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
633       /* Cleanup object queue */
634       $this->CopyPasteHandler->cleanup_queue();
636       /* Add new entries to CP queue */
637       foreach($this->list_get_selected_items() as $id){
638         $dn = $this->ogrouplist[$id]['dn'];
640         if($s_action == "copy_multiple"){
641           $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
642         }
643         if($s_action == "cut_multiple"){
644           $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
645         }
646       }
647     }
649     /* Start pasting entries */
650     if($s_action == "editPaste"){
651       $this->start_pasting_copied_objects = TRUE;
652     }
654     /* Return C&P dialog */
655     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
657       /* Get dialog */
658       $data = $this->CopyPasteHandler->execute();
659       $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
661       /* Return dialog data */
662       if(!empty($data)){
663         return($data);
664       }
665     }
666     /* Automatically disable status for pasting */
667     if(!$this->CopyPasteHandler->entries_queued()){
668       $this->start_pasting_copied_objects = FALSE;
669     }
670     return("");
671   }
674   function save_object()
675   {
676     $this->DivListOGroup->save_object();
677   }
681 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
682 ?>