Code

013fd65561d0d26896308161063b33ce65c568b6
[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  */
20 require "tabs_ogroups.inc";
22 class ogroupManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Object groups";
26   var $plDescription= "This does something";
28   /* attribute list for save action */
29   var $attributes= array();
30   var $objectclasses= array();
31   var $obtypes= array();
32   var $ogroup;
34   var $CopyPasteHandler = NULL;
35   var $DivListOGroup    = NULL;
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_/");
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("/_group_edit_/",$key)){
90         $type = preg_replace("/_group_edit_.*$/","",$key);
91         $s_action="edit";
92         $s_entry  = preg_replace("/".$type."_group_edit_/i","",$key);
93         $_POST['arg'] = $type;
94       }
95     }
96     $s_entry  = preg_replace("/_.$/","",$s_entry);
98     // Edit if
99     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
100       $s_action ="edit";
101       $s_entry  = $_GET['id'];
102     }
105     /****************
106       Copy & Paste handling 
107      ****************/
109     /* Display the copy & paste dialog, if it is currently open */
110     $ret = $this->copyPasteHandling($s_action,$s_entry);
111     if($ret){
112       return($ret);
113     }
115     /****************
116       Create a new object group 
117      ****************/
119     /* New group? */
120     if ($s_action=="new"){
122       /* By default we set 'dn' to 'new', all relevant plugins will
123          react on this. */
124       $this->dn= "new";
126       /* Create new usertab object */
127       $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
128       $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
129     }
132     /****************
133       Delete object group
134      ****************/
136     if ($s_action=="del"){
138       /* Get 'dn' from posted 'uid' */
139       $this->dn= $this->ogrouplist[$s_entry]['dn'];
141       /* Load permissions for selected 'dn' and check if
142          we're allowed to remove this 'dn' */
143       $acl = $this->ui->get_permissions($this->dn,"ogroups");
144       if(preg_match("/d/",$acl)){
146         /* Check locking, save current plugin in 'back_plugin', so
147            the dialog knows where to return. */
148         if (($user= get_lock($this->dn)) != ""){
149           return(gen_locked_message ($user, $this->dn));
150         }
152         /* Lock the current entry, so nobody will edit it during deletion */
153         add_lock ($this->dn, $this->ui->dn);
154         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
155         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
156       } else {
158         /* Obviously the user isn't allowed to delete. Show message and
159            clean session. */
160         print_red (_("You are not allowed to delete this object group!"));
161       }
162     }
165     /****************
166       Delete confirmed 
167      ****************/
169     /* Confirmation for deletion has been passed. Group should be deleted. */
170     if (isset($_POST['delete_group_confirm'])){
172       /* Some nice guy may send this as POST, so we've to check
173          for the permissions again. */
174       $acl = $this->ui->get_permissions($this->dn,"ogroups");
175       if(preg_match("/d/",$acl)){
177         /* Delete request is permitted, perform LDAP action */
178         $this->ogroup= new ogrouptabs($this->config,
179             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
180         $this->ogroup->set_acl(array($this->acl));
181         $this->ogroup->delete ();
182         gosa_log ("Object group'".$this->dn."' has been removed");
183         unset ($this->ogroup);
184         $this->ogroup= NULL;
185       } else {
187         /* Normally this shouldn't be reached, send some extra
188            logs to notify the administrator */
189         print_red (_("You are not allowed to delete this object group!"));
190         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
191       }
193       /* Remove lock file after successfull deletion */
194       del_lock ($this->dn);
195       unset($_SESSION['objectinfo']);
196     }
199     /****************
200       Delete object group Canceled
201      ****************/
203     /* Delete group canceled? */
204     if (isset($_POST['delete_cancel'])){
205       del_lock ($this->dn);
206       unset($_SESSION['objectinfo']);
207     }
210     /****************
211       Edit group
212      ****************/
214     if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
216       /* Get 'dn' from posted 'uid', must be unique */
217       $this->dn= $this->ogrouplist[$s_entry]['dn'];
219       /* Check locking, save current plugin in 'back_plugin', so
220          the dialog knows where to return. */
221       if (($user= get_lock($this->dn)) != ""){
222         return(gen_locked_message ($user, $this->dn));
223       }
225       /* Lock the current entry, so everyone will get the
226          above dialog */
227       add_lock ($this->dn, $this->ui->dn);
229       /* Register grouptab to trigger edit dialog */
230       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
231       $this->ogroup->set_acl_base($this->dn);
232       $_SESSION['objectinfo']= $this->dn;
233     }
236     /****************
237       Edit finished save 
238      ****************/
240     /* Finish button has been pressed */
241     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
243       /* Check tabs, will feed message array */
244       $message= $this->ogroup->check();
246       /* Save, or display error message? */
247       if (count($message) == 0){
249         /* Save user data to ldap */
250         $this->ogroup->save();
251         gosa_log ("Object group'".$this->dn."' has been saved");
253         if (!isset($_POST['edit_apply'])){
254           /* Group has been saved successfully, remove lock from
255              LDAP. */
256           if ($this->dn != "new"){
257             del_lock ($this->dn);
258           }
260           unset ($this->ogroup);
261           $this->ogroup= NULL;
262           unset ($_SESSION['objectinfo']);
263         }
264       } else {
265         /* Ok. There seem to be errors regarding to the tab data,
266            show message and continue as usual. */
267         show_errors($message);
268       }
269     }
272     /****************
273       Cancel edit object group
274      ****************/
276     /* Cancel dialogs */
277     if (isset($_POST['edit_cancel'])){
278       del_lock ($this->ogroup->dn);
279       unset ($this->ogroup);
280       $this->ogroup= NULL;
281       unset($_SESSION['objectinfo']);
282     }
285     /****************
286       Display open dialogs 
287      ****************/
289     /* Show dialog if object is present */
290     if (isset($this->ogroup->config)){
291       $display= $this->ogroup->execute();
293       /* Don't show buttons if tab dialog requests this */
294       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
295         $display.= "<p style=\"text-align:right\">\n";
296         $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
297         $display.= "&nbsp;\n";
298         if ($this->dn != "new"){
299           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
300           $display.= "&nbsp;\n";
301         }
302         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
303         $display.= "</p>";
304       }
305       return ($display);
306     }
309     /****************
310       Display list 
311      ****************/
313     /* Check if there is a snapshot dialog open */
314     $base = $this->DivListOGroup->selectedBase;
315     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
316       return($str);
317     }
319     /* Display dialog with group list */
320     $this->DivListOGroup->parent = $this;
321     $this->DivListOGroup->execute();
323     /* Add departments if subsearch is disabled */
324     if(!$this->DivListOGroup->SubSearch){
325       $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4);
326     }
327     $this->reload ();
328     $this->DivListOGroup->setEntries($this->ogrouplist);
329     return($this->DivListOGroup->Draw());
330   }
333   /* Return departments, that will be included within snapshot detection */
334   function get_used_snapshot_bases()
335   {
336     return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
337   }
339   
341   function convert_list($input)
342   {
343     $temp= "";
345     $conv= array(   
346         "U" => array("select_user.png"        ,_("User")        , "ogroup"),
347         "G" => array("select_groups.png"      ,_("Group")       , "ogroup"),
348         "A" => array("select_application.png" ,_("Application") , "ogroup"),
349         "D" => array("select_department.png"  ,_("Department")  , "ogroup"),
350         "S" => array("select_server.png"      ,_("Server")      , "ogroup"),
351         "F" => array("select_phone.png"       ,_("Phone")       , "phonequeue"),
352         "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
353         "T" => array("select_terminal.png"    ,_("Terminal")    , "termgroup"),
354         "P" => array("select_printer.png"     ,_("Printer")     , "ogroup"));
356     /* Assemble picture */
357     $type= $input['gosaGroupObjects'][0];
358     $type= preg_replace("/[^A-Z]/i","",$type);
359     if (isset($type[0])){
360       $p1['pic']= $conv[$type[0]][0];
361       $p1['tit']= $conv[$type[0]][1];
362       $p1['alt']= $type[0];
363       $p1['lnk']= $conv[$type[0]][2];
364       $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
365     } else {
366       $p1['pic']= "empty.png";
367       $p1['tit']= "";
368       $p1['alt']= "";
369       $p1['lnk']= "";//$conv[$type[0]][2];
370       $html_object_1 = "<img ";
371     }
373     if (isset($type[1])){
374       $p2['pic']= $conv[$type[1]][0];
375       $p2['alt']= $type[1];
376       $p2['tit']= $conv[$type[1]][1];
377       $p2['lnk']= $conv[$type[1]][2];
378       $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
379     } else {
380       $p2['pic']= "empty.png";
381       $p2['alt']= "";
382       $p2['tit']= "";
383       $p2['lnk']= ""; //$conv[$type[0]][2];
384       $html_object_2 = "<img ";
385     }
386     
387     $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."' 
388       src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
389     $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."' 
390       src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
391     return ($temp);
392   }
395   function reload()
396   {
397     /* Set base for all searches && initialise some vars */
398     $this->ogrouplist= array();
399     $base     = $this->DivListOGroup->selectedBase;
400     $filter   = "(gosaGroupObjects=[])";
401     $Regex    = $this->DivListOGroup->Regex;
403     $chk = array(
404         "UserGroups"          => "(gosaGroupObjects=*U*)" ,
405         "GroupGroups"         => "(gosaGroupObjects=*G*)" ,
406         "ApplicationGroups"   => "(gosaGroupObjects=*A*)" ,
407         "DepartmentGroups"    => "(gosaGroupObjects=*D*)" ,
408         "ServerGroups"        => "(gosaGroupObjects=*S*)" ,
409         "WorkstationGroups"   => "(gosaGroupObjects=*W*)" ,
410         "TerminalGroups"      => "(gosaGroupObjects=*T*)" ,
411         "PrinterGroups"       => "(gosaGroupObjects=*P*)" ,
412         "PhoneGroups"         => "(gosaGroupObjects=*F*)" );
413     /* Create filter */ 
414     foreach($chk as $chkBox => $FilterPart){
415       if($this->DivListOGroup->$chkBox){
416         $filter .=  $FilterPart;
417       }
418     }
419     $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
421     if($this->DivListOGroup->SubSearch){
422       $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
423     }else{
424       $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
425     }
427     $this->ogrouplist= $res;
428     ksort ($this->ogrouplist);
429     reset ($this->ogrouplist);
430     $tmp=array();
431     foreach($this->ogrouplist as $tkey => $val ){
432       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
433     }
434     ksort($tmp);
435     $this->ogrouplist=array();
436     foreach($tmp as $val){
437       $this->ogrouplist[]=$val;
438     }
439     reset ($this->ogrouplist);
440   }
443   function copyPasteHandling($s_action,$s_entry)
444   {
445     if($this->CopyPasteHandler){
447       /* Paste copied/cutted object in here
448        */
449       if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
450         $this->CopyPasteHandler->save_object();
451         $this->CopyPasteHandler->SetVar("base", $this->DivListOGroup->selectedBase);
452         return($this->CopyPasteHandler->execute());
453       }
455       /* Copy current object to CopyHandler
456        */
457       if($s_action == "copy"){
458     
460         $this->CopyPasteHandler->Clear();
461         $dn       =   $this->ogrouplist[$s_entry]['dn'];
463         /* Check acls */
464         $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
465         if(preg_match("/(c.*w|w.*c)/",$acl_all)){
466           $obj      =   new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
467           $objNew   =   new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], "new");
469           $types_of_tabs = $obj->by_object['ogroup']->gosaGroupObjects;
470           $objNew->by_object['ogroup']->gosaGroupObjects = $types_of_tabs;    
471           $objNew->reload($types_of_tabs);
473           $this->CopyPasteHandler->Copy($obj,$objNew);
474         }
475       }
477       /* Copy current object to CopyHandler
478        */
479       if($s_action == "cut"){
480         $this->CopyPasteHandler->Clear();
481         $dn       =   $this->ogrouplist[$s_entry]['dn'];
483         /* Check acls */
484         $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
485         if(preg_match("/(c.*w|w.*c)/",$acl_all)){
486           $obj      =   new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
487           $this->CopyPasteHandler->Cut($obj);
488         }
489       }
490     }
491   }
494   function save_object()
495   {
496     $this->DivListOGroup->save_object();
497   }
501 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
502 ?>