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 */
21 class departmentManagement extends plugin
22 {
23 /* Definitions */
24 var $plHeadline= "Departments";
25 var $plDescription= "Manage Departments";
27 /* Headpage attributes */
28 var $last_dep_sorting= "invalid";
29 var $departments= array();
30 var $deptabs= NULL;
32 /* attribute list for save action */
33 var $attributes= array();
34 var $objectclasses= array();
36 /* Vars to handle operations after saving the department
37 Recursive move && tagging */
38 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
39 var $ObjectTaggingRequested = false; // Object must be tagged, an iframe will be shown.
40 var $RecursiveRemoveRequested = false; // Is true, if this object must be moved, an iframe will be displayed in this case
42 function departmentManagement (&$config, &$ui)
43 {
44 $this->ui= &$ui;
45 $this->dn= "";
46 $this->config= &$config;
47 $this->DivListDepartment = new divListDepartment($this->config,$this);
48 }
50 function execute()
51 {
52 global $config;
54 /* Call parent execute */
55 plugin::execute();
57 /***************
58 Var init
59 ***************/
61 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/"));
63 /* Reload departments */
64 $smarty = get_smarty();
65 $display = "";
66 $s_action = ""; // Will contain an action, like del or edit
67 $s_entry = ""; // The entry name for edit delete -...
70 /***************
71 Check posts
72 ***************/
74 // Check Post action
75 foreach($_POST as $key => $val){
76 // Post for delete
77 if(preg_match("/dep_del.*/",$key)){
78 $s_action = "del";
79 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
80 $s_entry = preg_replace("/_.*$/","",$s_entry);
81 $s_entry = base64_decode($s_entry);
82 // Post for edit
83 }elseif(preg_match("/dep_edit_.*/",$key)){
84 $s_action="edit";
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 new
89 }elseif(preg_match("/^remove_multiple_departments/",$key)){
90 $s_action="del_multiple";
91 }elseif(preg_match("/dep_new.*/",$key)){
92 $s_action="new";
93 }
94 }
96 /* Create options */
97 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "dep_new"){
98 $s_action = "new";
99 }
101 /* handle remove from layers menu */
102 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
103 $s_action = "del_multiple";
104 }
107 /***************
108 Create a new department
109 ***************/
111 /* New Entry if Posted action (s_action) == new
112 */
113 if ($s_action=="new"){
114 $this->dn= "new";
115 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
116 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
117 }
120 /***************
121 Edit entry
122 ***************/
124 /* Edit Entry if Posted action (s_action) == edit
125 * The entry which will be edited is defined in $s_entry
126 */
127 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
128 $this->dn= $this->config->departments[trim($s_entry)];
130 if (($user= get_lock($this->dn)) != ""){
131 return(gen_locked_message ($user, $this->dn));
132 }
134 /* Lock the current entry, so everyone will get the above dialog */
135 add_lock ($this->dn, $this->ui->dn);
137 /* Register deptabs to trigger edit dialog */
138 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
139 $this->deptabs->set_acl_base($this->dn);
141 session::set('objectinfo',$this->dn);
142 }
145 /********************
146 Delete MULTIPLE entries requested, display confirm dialog
147 ********************/
149 if ($s_action=="del_multiple"){
150 $ids = $this->list_get_selected_items();
153 if(count($ids)){
154 foreach($ids as $id){
155 $id = base64_decode($id);
156 $dn = $this->config->departments[$id];
158 if (($user= get_lock($dn)) != ""){
159 return(gen_locked_message ($user, $dn));
160 }
161 $this->dns[$id] = $dn;
162 }
164 $dns_names = "<br><pre>";
165 foreach($this->dns as $dn){
166 add_lock ($dn, $this->ui->dn);
167 $dns_names .= $dn."\n";
168 }
169 $dns_names .="</pre>";
171 /* Lock the current entry, so nobody will edit it during deletion */
172 if (count($this->dns) == 1){
173 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
174 } else {
175 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
176 }
177 $smarty->assign("multiple", true);
178 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
179 }
180 }
183 /********************
184 Delete MULTIPLE entries confirmed
185 ********************/
187 /* Confirmation for deletion has been passed. Users should be deleted. */
188 if (isset($_POST['delete_multiple_department_confirm'])){
190 /* Remove user by user and check acls before removeing them */
191 foreach($this->dns as $key => $dn){
192 $acl = $this->ui->get_permissions($dn,"department/department");
193 if (preg_match('/d/', $acl)){
195 /* Delete request is permitted, perform LDAP action */
196 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
197 $this->deptabs->set_acl_base();
198 $this->deptabs->delete ();
199 $this->deptabs = NULL;
200 } else {
201 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
202 }
203 /* Remove lock file after successfull deletion */
204 del_lock ($dn);
205 unset($this->dns[$key]);
206 }
207 }
210 /********************
211 Delete MULTIPLE entries Canceled
212 ********************/
214 /* Remove lock */
215 if(isset($_POST['delete_multiple_department_cancel'])){
216 foreach($this->dns as $key => $dn){
217 del_lock ($dn);
218 unset($this->dns[$key]);
219 }
220 }
223 /***************
224 Delete entry
225 ***************/
227 /* Delete Entry if Posted action (s_action) == del
228 * The entry which will be deleted is defined in $s_entry
229 */
230 if ($s_action =="del"){
231 $this->dn= $this->config->departments[trim($s_entry)];
233 /* check acls */
234 $acl = $this->ui->get_permissions($this->dn,"department/department");
235 if(preg_match("/d/",$acl)){
237 /* Check locking */
238 if (($user= get_lock($this->dn)) != ""){
239 session::set('dn',$this->dn);
240 return(gen_locked_message($user, $this->dn));
241 } else {
242 add_lock ($this->dn, $this->ui->dn);
243 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
244 $smarty->assign("multiple", false);
245 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
246 return ($display);
247 }
248 }else{
249 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
250 }
251 }
254 /***************
255 Delete department confirmed
256 ***************/
258 /* If department deletion is accepted ...
259 * Finally delete department
260 */
261 if (isset($_POST['delete_department_confirm'])){
263 /* check acls */
264 $acl = $this->ui->get_permissions($this->dn,"department/department");
265 if(preg_match("/d/",$acl)){
266 $this->remove_from_parent();
267 } else {
268 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), WARNING_DIALOG);
269 }
270 }
273 /***************
274 Edit department finished
275 ***************/
277 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
278 $this->deptabs->save_object();
279 $obj = $this->deptabs->by_object['department'];
280 if($obj->orig_dn != "new"){
281 if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
282 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
283 }
284 }
285 }
287 /* Save changes */
288 if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
290 /* Check tabs, will feed message array */
291 $message= $this->deptabs->check();
293 /* Save, or display error message? */
294 if (count($message) == 0){
296 $this->deptabs->save(true);
297 global $config;
298 $config->get_departments();
299 $config->make_idepartments();
300 $this->config = $config;
302 /* This object must be tagged, so set ObjectTaggingRequested to true */
303 if($this->deptabs->by_object['department']->must_be_tagged()){
304 $this->ObjectTaggingRequested = true;
305 }
307 /* Get recursive move is required, set RecursiveRemoveRequested to true */
308 if($this->deptabs->by_object['department']->am_i_moved()){
309 $this->RecursiveRemoveRequested = true;
310 }
312 /* This var indicated that there is an object which isn't saved right now. */
313 $this->ObjectInSaveMode = true;
315 } else {
316 /* Ok. There seem to be errors regarding to the tab data,
317 show message and continue as usual. */
318 show_errors($message);
319 }
320 }
323 /***************
324 Handle Tagging (Return output for an iframe)
325 ***************/
327 /* This department must be tagged (Is called from iframe, generates output)*/
328 if(isset($_GET['TagDepartment'])){
329 $this->deptabs->by_object['department']->tag_objects();
330 exit();
331 }
334 /***************
335 Handle recursive move (Return output for an iframe)
336 ***************/
338 /* initiate recursive remove (Is called from iframe, generates output)*/
339 if(isset($_GET['PerformRecMove'])){
340 $this->deptabs->by_object['department']->recursive_move("","",true);
341 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
342 exit();
343 }
346 /***************
347 Return iframes, which call tagging / recusrsive move
348 ***************/
350 /* While one of these vars below isset, we must return an iframe,
351 to perform requested operation */
352 if($this->ObjectTaggingRequested){
353 $this->ObjectTaggingRequested = false;
354 return($this->deptabs->by_object['department']->ShowTagFrame());
355 }
356 if($this->RecursiveRemoveRequested){
357 $this->RecursiveRemoveRequested = false;
358 return($this->deptabs->by_object['department']->ShowMoveFrame());
359 }
362 /***************
363 In case of tagging/moving the object wasn't deleted, do it know
364 ***************/
366 /* If there is an unsaved object and all operations are done
367 remove locks & save object tab & unset current object */
368 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
369 $this->deptabs->save();
370 $this->config->get_departments();
371 $this->ObjectInSaveMode = false;
372 if ($this->dn != "new"){
373 del_lock ($this->dn);
374 }
375 unset ($this->deptabs);
376 $this->deptabs= NULL;
377 session::un_set('objectinfo');
378 }
381 /***************
382 Dialog canceled
383 ***************/
385 /* User canceled edit oder delete
386 * Cancel dialog
387 */
388 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
389 del_lock ($this->dn);
390 unset($this->depdabs);
391 $this->deptabs= NULL;
392 session::un_set('objectinfo');
393 }
395 /* Headpage or normal plugin screen? */
396 if ($this->deptabs !== NULL){
398 /* Show main page (tabs) */
399 $display= $this->deptabs->execute();
400 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
401 $display.= "<p style=\"text-align:right\">\n";
402 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
403 $display.= " \n";
404 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
405 $display.= "</p>";
406 }
407 return ($display);
408 }else{
409 /* Display dialog with group list */
410 $this->DivListDepartment->parent = $this;
411 $this->DivListDepartment->execute();
412 $this->reload();
413 $this->DivListDepartment->DepartmentsAdded = true;
414 $this->DivListDepartment->setEntries($this->departments);
415 return($this->DivListDepartment->Draw());
416 }
417 }
420 function reload()
421 {
422 /* Vairaible init */
423 $base = $this->DivListDepartment->selectedBase;
424 $base_back = preg_replace("/^[^,]+,/","",$base);
425 $Regex = $this->DivListDepartment->Regex;
427 // Create Array to Test if we have a valid back button
428 $config = session::get('config');
429 $tmp = $config->idepartments;
431 // In case of a valid back button create entry
432 if(isset($tmp[$base_back])){
433 $tmp2 ['dn'] = convert_department_dn($base_back);
435 // If empty always go to top
436 if(empty($tmp2['dn'])){
437 $tmp2['dn']="/";
438 }
439 $tmp2 ['description'][0] = ".. "._("Back");
440 $result[$tmp[$base_back]]=$tmp2;
441 }
443 if($this->DivListDepartment->SubSearch){
444 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
445 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
446 }else{
447 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
448 "department", $base, array("ou", "description"), GL_SIZELIMIT );
449 }
451 $this->departments= array();
453 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
454 if(preg_match("/^ou=/",$base)){
455 $this->departments [ convert_department_dn($base) ] = ".";
456 }
458 foreach ($res as $key => $value){
460 /* Don't display base as entry on subsearch */
461 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
462 continue;
463 }
465 $cdn= convert_department_dn($value['dn']);
467 /* Append to dep list */
468 if(isset($value["description"][0])){
469 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
470 }else{
471 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
472 }
473 }
474 natcasesort ($this->departments);
475 reset ($this->departments);
476 }
478 function remove_from_parent()
479 {
480 $ldap= $this->config->get_ldap_link();
481 $ldap->cd ($this->dn);
482 $ldap->recursive_remove();
484 /* Optionally execute a command after we're done */
485 $this->postremove();
487 /* Delete references to object groups */
488 $ldap->cd ($this->config->current['BASE']);
489 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
490 while ($ldap->fetch()){
491 $og= new ogroup($this->config, $ldap->getDN());
492 unset($og->member[$this->dn]);
493 $og->save ();
494 }
496 }
499 function list_get_selected_items()
500 {
501 $ids = array();
502 foreach($_POST as $name => $value){
503 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
504 $id = preg_replace("/^item_selected_/","",$name);
505 $ids[$id] = $id;
506 }
507 }
508 return($ids);
509 }
512 function remove_lock()
513 {
514 if (isset($this->dn)){
515 del_lock ($this->dn);
516 }
517 }
519 function save_object()
520 {
521 /* reload department */
522 $this->config->get_departments();
524 $this->config->make_idepartments();
525 $this->DivListDepartment->config= $this->config;
526 $this->DivListDepartment->save_object();
527 }
529 }
530 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
531 ?>