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;
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];
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 }
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 gosa_log ("Department object'".$this->dn."' has been removed");
197 } else {
198 print_red (_("You have no permission to remove this department."));
199 }
200 /* Remove lock file after successfull deletion */
201 del_lock ($dn);
202 unset($this->dns[$key]);
203 }
204 }
207 /********************
208 Delete MULTIPLE entries Canceled
209 ********************/
211 /* Remove lock */
212 if(isset($_POST['delete_multiple_department_cancel'])){
213 foreach($this->dns as $key => $dn){
214 del_lock ($dn);
215 unset($this->dns[$key]);
216 }
217 }
220 /***************
221 Delete entry
222 ***************/
224 /* Delete Entry if Posted action (s_action) == del
225 * The entry which will be deleted is defined in $s_entry
226 */
227 if ($s_action =="del"){
228 $this->dn= $this->config->departments[trim($s_entry)];
230 /* check acls */
231 $acl = $this->ui->get_permissions($this->dn,"department/department");
232 if(preg_match("/d/",$acl)){
234 /* Check locking */
235 if (($user= get_lock($this->dn)) != ""){
236 $_SESSION['dn']= $this->dn;
237 return(gen_locked_message($user, $this->dn));
238 } else {
239 add_lock ($this->dn, $this->ui->dn);
240 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
241 $smarty->assign("multiple", false);
242 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
243 return ($display);
244 }
245 }else{
246 print_red (_("You have no permission to remove this department."));
247 }
248 }
251 /***************
252 Delete department confirmed
253 ***************/
255 /* If department deletion is accepted ...
256 * Finally delete department
257 */
258 if (isset($_POST['delete_department_confirm'])){
260 /* check acls */
261 $acl = $this->ui->get_permissions($this->dn,"department/department");
262 if(preg_match("/d/",$acl)){
263 $this->remove_from_parent();
264 gosa_log ("Department object'".$this->dn."' has been removed");
265 } else {
266 print_red (_("You have no permission to remove this department."));
267 }
268 }
271 /***************
272 Edit department finished
273 ***************/
275 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
276 $this->deptabs->save_object();
277 $obj = $this->deptabs->by_object['department'];
278 if($obj->orig_dn != "new"){
279 if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
280 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
281 }
282 }
283 }
285 /* Save changes */
286 if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
288 /* Check tabs, will feed message array */
289 $message= $this->deptabs->check();
291 /* Save, or display error message? */
292 if (count($message) == 0){
294 $this->deptabs->save(true);
295 $this->config->get_departments();
297 /* This object must be tagged, so set ObjectTaggingRequested to true */
298 if($this->deptabs->by_object['department']->must_be_tagged()){
299 $this->ObjectTaggingRequested = true;
300 }
302 /* Get recursive move is required, set RecursiveRemoveRequested to true */
303 if($this->deptabs->by_object['department']->am_i_moved()){
304 $this->RecursiveRemoveRequested = true;
305 }
307 /* This var indicated that there is an object which isn't saved right now. */
308 $this->ObjectInSaveMode = true;
310 } else {
311 /* Ok. There seem to be errors regarding to the tab data,
312 show message and continue as usual. */
313 show_errors($message);
314 }
315 }
318 /***************
319 Handle Tagging (Return output for an iframe)
320 ***************/
322 /* This department must be tagged (Is called from iframe, generates output)*/
323 if(isset($_GET['TagDepartment'])){
324 $this->deptabs->by_object['department']->tag_objects();
325 exit();
326 }
329 /***************
330 Handle recursive move (Return output for an iframe)
331 ***************/
333 /* initiate recursive remove (Is called from iframe, generates output)*/
334 if(isset($_GET['PerformRecMove'])){
335 $this->deptabs->by_object['department']->recursive_move("","",true);
336 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
337 exit();
338 }
341 /***************
342 Return iframes, which call tagging / recusrsive move
343 ***************/
345 /* While one of these vars below isset, we must return an iframe,
346 to perform requested operation */
347 if($this->ObjectTaggingRequested){
348 $this->ObjectTaggingRequested = false;
349 return($this->deptabs->by_object['department']->ShowTagFrame());
350 }
351 if($this->RecursiveRemoveRequested){
352 $this->RecursiveRemoveRequested = false;
353 return($this->deptabs->by_object['department']->ShowMoveFrame());
354 }
357 /***************
358 In case of tagging/moving the object wasn't deleted, do it know
359 ***************/
361 /* If there is an unsaved object and all operations are done
362 remove locks & save object tab & unset current object */
363 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
364 $this->deptabs->save();
365 $this->config->get_departments();
366 $this->ObjectInSaveMode = false;
367 if ($this->dn != "new"){
368 del_lock ($this->dn);
369 }
370 gosa_log ("Department object'".$this->dn."' has been saved");
371 unset ($this->deptabs);
372 $this->deptabs= NULL;
373 unset ($_SESSION['objectinfo']);
374 }
377 /***************
378 Dialog canceled
379 ***************/
381 /* User canceled edit oder delete
382 * Cancel dialog
383 */
384 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
385 del_lock ($this->dn);
386 unset($this->depdabs);
387 $this->deptabs= NULL;
388 unset ($_SESSION['objectinfo']);
389 }
391 /* Headpage or normal plugin screen? */
392 if ($this->deptabs != NULL){
394 /* Show main page (tabs) */
395 $display= $this->deptabs->execute();
396 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
397 $display.= "<p style=\"text-align:right\">\n";
398 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
399 $display.= " \n";
400 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
401 $display.= "</p>";
402 }
403 return ($display);
404 }else{
405 /* Display dialog with group list */
406 $this->DivListDepartment->parent = $this;
407 $this->DivListDepartment->execute();
408 $this->reload();
409 $this->DivListDepartment->DepartmentsAdded = true;
410 $this->DivListDepartment->setEntries($this->departments);
411 return($this->DivListDepartment->Draw());
412 }
413 }
416 function reload()
417 {
418 /* Vairaible init */
419 $base = $this->DivListDepartment->selectedBase;
420 $base_back = preg_replace("/^[^,]+,/","",$base);
421 $Regex = $this->DivListDepartment->Regex;
423 // Create Array to Test if we have a valid back button
424 $tmp = $_SESSION['config']->idepartments;
426 // In case of a valid back button create entry
427 if(isset($tmp[$base_back])){
428 $tmp2 ['dn'] = convert_department_dn($base_back);
430 // If empty always go to top
431 if(empty($tmp2['dn'])){
432 $tmp2['dn']="/";
433 }
434 $tmp2 ['description'][0] = _("..");
435 $result[$tmp[$base_back]]=$tmp2;
436 }
438 if($this->DivListDepartment->SubSearch){
439 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
440 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
441 }else{
442 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
443 "department", $base, array("ou", "description"), GL_SIZELIMIT );
444 }
446 $this->departments= array();
448 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
449 if(preg_match("/^ou=/",$base)){
450 $this->departments [ convert_department_dn($base) ] = ".";
451 }
453 foreach ($res as $key => $value){
455 /* Don't display base as entry on subsearch */
456 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
457 continue;
458 }
460 $cdn= convert_department_dn($value['dn']);
462 /* Append to dep list */
463 if(isset($value["description"][0])){
464 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
465 }else{
466 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
467 }
468 }
469 natcasesort ($this->departments);
470 reset ($this->departments);
471 }
473 function remove_from_parent()
474 {
475 $ldap= $this->config->get_ldap_link();
476 $ldap->cd ($this->dn);
477 $ldap->recursive_remove();
479 /* Optionally execute a command after we're done */
480 $this->postremove();
482 /* Delete references to object groups */
483 $ldap->cd ($this->config->current['BASE']);
484 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
485 while ($ldap->fetch()){
486 $og= new ogroup($this->config, $ldap->getDN());
487 unset($og->member[$this->dn]);
488 $og->save ();
489 }
491 }
494 function list_get_selected_items()
495 {
496 $ids = array();
497 foreach($_POST as $name => $value){
498 if(preg_match("/^item_selected_[a-z0-9\=]*$/i",$name)){
499 $id = preg_replace("/^item_selected_/","",$name);
500 $ids[$id] = $id;
501 }
502 }
503 return($ids);
504 }
507 function remove_lock()
508 {
509 if (isset($this->dn)){
510 del_lock ($this->dn);
511 }
512 }
514 function save_object()
515 {
516 /* reload department */
517 $this->config->get_departments();
519 $this->config->make_idepartments();
520 $this->DivListDepartment->config= $this->config;
521 $this->DivListDepartment->save_object();
522 }
524 }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>