Code

Added envrironment to ogroups
[gosa.git] / plugins / admin / departments / class_departmentManagement.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_department.inc";
22 class departmentManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Departments";
26   var $plDescription= "This does something";
28   /* CLI vars */
29   var $cli_summary= "Handling of LDAP subtrees";
30   var $cli_description= "Some longer text\nfor help";
31   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33   /* Headpage attributes */
34   var $last_dep_sorting= "invalid";
35   var $departments= array();
36   var $deptabs= NULL;
38   /* attribute list for save action */
39   var $attributes= array();
40   var $objectclasses= array();
42   /* Vars to handle operations after saving the department 
43      Recursive move && tagging   */
44   var $ObjectInSaveMode         = false;    // Is true, if current object wasn't saved right now
45   var $ObjectTaggingRequested   = false;    // Object must be tagged, an iframe will be shown. 
46   var $RecursiveRemoveRequested = false;    // Is true, if this object must be moved, an iframe will be displayed in this case
48   function departmentManagement ($config, $ui)
49   {
50     $this->ui= $ui;
51     $this->dn= "";
52     $this->config= $config;
53     $this->DivListDepartment = new divListDepartment($this->config,$this);
54   }
56   function execute()
57   {
58     global $config;
59   
60     /* Call parent execute */
61     plugin::execute();
63     /***************
64       Var init 
65      ***************/
67     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/");
69     /* Reload departments */
70     $smarty                                             = get_smarty();
71     $display                                    = "";
72     $s_action                                   = "";  // Will contain an action, like del or edit
73     $s_entry                                    = "";  // The entry name for edit delete -...
76     /***************
77       Check posts  
78      ***************/
80     // Check Post action
81     foreach($_POST as $key => $val){
82       // Post for delete
83       if(preg_match("/dep_del.*/",$key)){
84         $s_action = "del";
85         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
86         $s_entry  = preg_replace("/_.*$/","",$s_entry);
87         $s_entry  = base64_decode($s_entry);
88         // Post for edit
89       }elseif(preg_match("/dep_edit_.*/",$key)){
90         $s_action="edit";
91         $s_entry  = preg_replace("/dep_".$s_action."_/i","",$key);
92         $s_entry  = preg_replace("/_.*$/","",$s_entry);
93         $s_entry  = base64_decode($s_entry);
94         // Post for new
95       }elseif(preg_match("/^remove_multiple_departments/",$key)){
96         $s_action="del_multiple";
97       }elseif(preg_match("/dep_new.*/",$key)){
98         $s_action="new";
99       }
100     }
103     /***************
104       Create a new department
105      ***************/
107     /* New Entry if Posted action (s_action) == new
108      */
109     if ($s_action=="new"){
110       $this->dn= "new";
111       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
112       $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
113     }
116     /***************
117       Edit entry
118      ***************/
120     /* Edit Entry if Posted action (s_action) == edit 
121      * The entry which will be edited is defined in $s_entry
122      */
123     if (( $s_action=="edit") && (!isset($this->deptabs->config))){
124       $this->dn= $this->config->departments[trim($s_entry)];
126       if (($user= get_lock($this->dn)) != ""){
127         return(gen_locked_message ($user, $this->dn));
128       }
130       /* Lock the current entry, so everyone will get the  above dialog */
131       add_lock ($this->dn, $this->ui->dn);
133       /* Register deptabs to trigger edit dialog */
134       $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
135       $this->deptabs->set_acl_base($this->dn);
137       $_SESSION['objectinfo']= $this->dn;
138     }
141     /********************
142       Delete MULTIPLE entries requested, display confirm dialog
143      ********************/
145     if ($s_action=="del_multiple"){
146       $ids = $this->list_get_selected_items();
149       if(count($ids)){
150         foreach($ids as $id){
151           $id = base64_decode($id);
152           $dn = $this->config->departments[$id];
153   
154           if (($user= get_lock($dn)) != ""){
155             return(gen_locked_message ($user, $dn));
156           }
157           $this->dns[$id] = $dn;
158         }
160         $dns_names = "<br><pre>";
161         foreach($this->dns as $dn){
162           add_lock ($dn, $this->ui->dn);
163           $dns_names .= $dn."\n";
164         }
165         $dns_names .="</pre>";
167         /* Lock the current entry, so nobody will edit it during deletion */
168         if (count($this->dns) == 1){
169           $smarty->assign("info",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
170         } else {
171           $smarty->assign("info",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
172         }
173         $smarty->assign("multiple", true);
174         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
175       }
176     }
177    
179     /********************
180       Delete MULTIPLE entries confirmed
181      ********************/
183     /* Confirmation for deletion has been passed. Users should be deleted. */
184     if (isset($_POST['delete_multiple_department_confirm'])){
186       /* Remove user by user and check acls before removeing them */
187       foreach($this->dns as $key => $dn){
188         $acl = $this->ui->get_permissions($dn,"department/department");
189         if (preg_match('/d/', $acl)){
191           /* Delete request is permitted, perform LDAP action */
192           $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
193           $this->deptabs->set_acl_base();
194           $this->deptabs->delete ();
195           $this->deptabs = NULL;
196         } else {
197           print_red (_("You have no permission to remove this department."));
198         }
199         /* Remove lock file after successfull deletion */
200         del_lock ($dn);
201         unset($this->dns[$key]);
202       }
203     }
205  
206     /********************
207       Delete MULTIPLE entries Canceled
208      ********************/
210     /* Remove lock */
211     if(isset($_POST['delete_multiple_department_cancel'])){
212       foreach($this->dns as $key => $dn){
213         del_lock ($dn);
214         unset($this->dns[$key]);
215       }
216     }
219     /***************
220       Delete entry 
221      ***************/
223     /* Delete Entry if Posted action (s_action) == del 
224      * The entry which will be deleted is defined in $s_entry
225      */
226     if ($s_action =="del"){
227       $this->dn= $this->config->departments[trim($s_entry)];
229       /* check acls */
230       $acl = $this->ui->get_permissions($this->dn,"department/department");
231       if(preg_match("/d/",$acl)){
233         /* Check locking */
234         if (($user= get_lock($this->dn)) != ""){
235           $_SESSION['dn']= $this->dn;
236           return(gen_locked_message($user, $this->dn));
237         } else {
238           add_lock ($this->dn, $this->ui->dn);
239           $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
240           $smarty->assign("multiple", false);
241           $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
242           return ($display);
243         }
244       }else{
245         print_red (_("You have no permission to remove this department."));
246       }
247     }
250     /***************
251       Delete department confirmed  
252      ***************/
254     /* If department deletion is accepted ...
255      * Finally delete department 
256      */
257     if (isset($_POST['delete_department_confirm'])){
259       /* check acls */
260       $acl = $this->ui->get_permissions($this->dn,"department/department");
261       if(preg_match("/d/",$acl)){
262         $this->remove_from_parent();
263       } else {
264         print_red (_("You have no permission to remove this department."));
265       }
266     }
269     /***************
270       Edit department finished 
271      ***************/
273     if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
274       $this->deptabs->save_object();
275       $obj = $this->deptabs->by_object['department'];
276       if($obj->orig_dn != "new"){
277         if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
278           return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
279         }
280       }
281     }
283     /* Save changes */
284     if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
286       /* Check tabs, will feed message array */
287       $message= $this->deptabs->check();
289       /* Save, or display error message? */
290       if (count($message) == 0){
292         $this->deptabs->save(true);
293         $this->config->get_departments();
295         /* This object must be tagged, so set ObjectTaggingRequested to true */
296         if($this->deptabs->by_object['department']->must_be_tagged()){
297           $this->ObjectTaggingRequested   = true; 
298         }               
300         /* Get recursive move is required, set RecursiveRemoveRequested to true */
301         if($this->deptabs->by_object['department']->am_i_moved()){      
302           $this->RecursiveRemoveRequested = true;
303         }               
304       
305         /* This var indicated that there is an object which isn't saved right now. */
306         $this->ObjectInSaveMode = true;
308       } else {
309         /* Ok. There seem to be errors regarding to the tab data,
310            show message and continue as usual. */
311         show_errors($message);
312       }
313     }
316     /***************
317       Handle Tagging (Return output for an iframe)
318      ***************/
320     /* This department must be tagged (Is called from iframe, generates output)*/
321     if(isset($_GET['TagDepartment'])){
322       $this->deptabs->by_object['department']->tag_objects();
323       exit();  
324     }
327     /***************
328       Handle recursive move (Return output for an iframe)
329      ***************/
331     /* initiate recursive remove  (Is called from iframe, generates output)*/
332     if(isset($_GET['PerformRecMove'])){
333       $this->deptabs->by_object['department']->recursive_move("","",true);
334       $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;  
335       exit();
336     }
339     /***************
340       Return iframes, which call tagging / recusrsive move 
341      ***************/
343     /* While one of these vars below isset, we must return an iframe, 
344        to perform requested operation */
345     if($this->ObjectTaggingRequested){
346       $this->ObjectTaggingRequested = false;
347       return($this->deptabs->by_object['department']->ShowTagFrame());
348     }
349     if($this->RecursiveRemoveRequested){
350       $this->RecursiveRemoveRequested = false;
351       return($this->deptabs->by_object['department']->ShowMoveFrame());
352     }
355     /***************
356       In case of tagging/moving the object wasn't deleted, do it know
357      ***************/
359     /* If there is an unsaved object and all operations are done
360        remove locks & save object tab & unset current object */
361     if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
362       $this->deptabs->save();
363       $this->config->get_departments();
364       $this->ObjectInSaveMode = false;
365       if ($this->dn != "new"){
366         del_lock ($this->dn);
367       }
368       unset ($this->deptabs);
369       $this->deptabs= NULL;
370       unset ($_SESSION['objectinfo']);
371     }
374     /***************
375       Dialog canceled  
376      ***************/
378     /* User canceled edit oder delete
379      * Cancel dialog 
380      */
381     if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
382       del_lock ($this->dn);
383       unset($this->depdabs);
384       $this->deptabs= NULL;
385       unset ($_SESSION['objectinfo']);
386     }
388     /* Headpage or normal plugin screen? */
389     if ($this->deptabs != NULL){
391       /* Show main page (tabs) */
392       $display= $this->deptabs->execute();
393       if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
394         $display.= "<p style=\"text-align:right\">\n";
395         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
396         $display.= "&nbsp;\n";
397         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
398         $display.= "</p>";
399       }
400       return ($display);
401     }else{
402       /* Display dialog with group list */
403       $this->DivListDepartment->parent = $this;
404       $this->DivListDepartment->execute();
405       $this->reload();
406       $this->DivListDepartment->DepartmentsAdded = true;
407       $this->DivListDepartment->setEntries($this->departments);
408       return($this->DivListDepartment->Draw());
409     }
410   }
413   function reload()
414   {
415     /* Vairaible init */
416     $base         = $this->DivListDepartment->selectedBase;
417     $base_back    = preg_replace("/^[^,]+,/","",$base);
418     $Regex        = $this->DivListDepartment->Regex;
419   
420     // Create Array to Test if we have a valid back button
421     $tmp = $_SESSION['config']->idepartments;
423     // In case of a valid back button create entry
424     if(isset($tmp[$base_back])){
425       $tmp2    ['dn']          = convert_department_dn($base_back);
427       // If empty always go to top
428       if(empty($tmp2['dn'])){
429         $tmp2['dn']="/";
430       }
431       $tmp2    ['description'][0] = _("..");
432       $result[$tmp[$base_back]]=$tmp2;
433     }
435     if($this->DivListDepartment->SubSearch){
436       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
437           "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
438     }else{
439       $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
440           "department", $base, array("ou", "description"), GL_SIZELIMIT );
441     }
443     $this->departments= array();
445     /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
446     if(preg_match("/^ou=/",$base)){
447       $this->departments [ convert_department_dn($base) ] = ".";
448     }
450     foreach ($res as $key => $value){
452       /* Don't display base as entry on subsearch */
453       if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
454         continue;
455       }
457       $cdn= convert_department_dn($value['dn']);
459       /* Append to dep list */
460       if(isset($value["description"][0])){
461         $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
462       }else{
463         $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
464       }
465     }
466     natcasesort ($this->departments);
467     reset ($this->departments);
468   }
470   function remove_from_parent()
471   {
472     $ldap= $this->config->get_ldap_link();
473     $ldap->cd ($this->dn);
474     $ldap->recursive_remove();
476     /* Optionally execute a command after we're done */
477     $this->postremove();
479     /* Delete references to object groups */
480     $ldap->cd ($this->config->current['BASE']);
481     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
482     while ($ldap->fetch()){
483       $og= new ogroup($this->config, $ldap->getDN());
484       unset($og->member[$this->dn]);
485       $og->save ();
486     }
488   }
491   function list_get_selected_items()
492   {
493     $ids = array();
494     foreach($_POST as $name => $value){
495       if(preg_match("/^item_selected_[a-z0-9\=]*$/i",$name)){
496         $id   = preg_replace("/^item_selected_/","",$name);
497         $ids[$id] = $id;
498       }
499     }
500     return($ids);
501   }
504   function remove_lock()
505   {
506     if (isset($this->dn)){
507       del_lock ($this->dn);
508     }
509   }
511   function save_object()
512   {
513     /* reload department */
514     $this->config->get_departments();
516     $this->config->make_idepartments();
517     $this->DivListDepartment->config= $this->config;
518     $this->DivListDepartment->save_object();
519   }
522 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
523 ?>