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= "This does something";
27 /* CLI vars */
28 var $cli_summary= "Handling of LDAP subtrees";
29 var $cli_description= "Some longer text\nfor help";
30 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
32 /* Headpage attributes */
33 var $last_dep_sorting= "invalid";
34 var $departments= array();
35 var $deptabs= NULL;
37 /* attribute list for save action */
38 var $attributes= array();
39 var $objectclasses= array();
41 /* Vars to handle operations after saving the department
42 Recursive move && tagging */
43 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
44 var $ObjectTaggingRequested = false; // Object must be tagged, an iframe will be shown.
45 var $RecursiveRemoveRequested = false; // Is true, if this object must be moved, an iframe will be displayed in this case
47 function departmentManagement (&$config, &$ui)
48 {
49 $this->ui= &$ui;
50 $this->dn= "";
51 $this->config= &$config;
52 $this->DivListDepartment = new divListDepartment($this->config,$this);
53 }
55 function execute()
56 {
57 global $config;
59 /* Call parent execute */
60 plugin::execute();
62 /***************
63 Var init
64 ***************/
66 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/");
68 /* Reload departments */
69 $smarty = get_smarty();
70 $display = "";
71 $s_action = ""; // Will contain an action, like del or edit
72 $s_entry = ""; // The entry name for edit delete -...
75 /***************
76 Check posts
77 ***************/
79 // Check Post action
80 foreach($_POST as $key => $val){
81 // Post for delete
82 if(preg_match("/dep_del.*/",$key)){
83 $s_action = "del";
84 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
85 $s_entry = preg_replace("/_.*$/","",$s_entry);
86 $s_entry = base64_decode($s_entry);
87 // Post for edit
88 }elseif(preg_match("/dep_edit_.*/",$key)){
89 $s_action="edit";
90 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
91 $s_entry = preg_replace("/_.*$/","",$s_entry);
92 $s_entry = base64_decode($s_entry);
93 // Post for new
94 }elseif(preg_match("/^remove_multiple_departments/",$key)){
95 $s_action="del_multiple";
96 }elseif(preg_match("/dep_new.*/",$key)){
97 $s_action="new";
98 }
99 }
102 /***************
103 Create a new department
104 ***************/
106 /* New Entry if Posted action (s_action) == new
107 */
108 if ($s_action=="new"){
109 $this->dn= "new";
110 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
111 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
112 }
115 /***************
116 Edit entry
117 ***************/
119 /* Edit Entry if Posted action (s_action) == edit
120 * The entry which will be edited is defined in $s_entry
121 */
122 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
123 $this->dn= $this->config->departments[trim($s_entry)];
125 if (($user= get_lock($this->dn)) != ""){
126 return(gen_locked_message ($user, $this->dn));
127 }
129 /* Lock the current entry, so everyone will get the above dialog */
130 add_lock ($this->dn, $this->ui->dn);
132 /* Register deptabs to trigger edit dialog */
133 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
134 $this->deptabs->set_acl_base($this->dn);
136 $_SESSION['objectinfo']= $this->dn;
137 }
140 /********************
141 Delete MULTIPLE entries requested, display confirm dialog
142 ********************/
144 if ($s_action=="del_multiple"){
145 $ids = $this->list_get_selected_items();
148 if(count($ids)){
149 foreach($ids as $id){
150 $id = base64_decode($id);
151 $dn = $this->config->departments[$id];
153 if (($user= get_lock($dn)) != ""){
154 return(gen_locked_message ($user, $dn));
155 }
156 $this->dns[$id] = $dn;
157 }
159 $dns_names = "<br><pre>";
160 foreach($this->dns as $dn){
161 add_lock ($dn, $this->ui->dn);
162 $dns_names .= $dn."\n";
163 }
164 $dns_names .="</pre>";
166 /* Lock the current entry, so nobody will edit it during deletion */
167 if (count($this->dns) == 1){
168 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
169 } else {
170 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
171 }
172 $smarty->assign("multiple", true);
173 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
174 }
175 }
178 /********************
179 Delete MULTIPLE entries confirmed
180 ********************/
182 /* Confirmation for deletion has been passed. Users should be deleted. */
183 if (isset($_POST['delete_multiple_department_confirm'])){
185 /* Remove user by user and check acls before removeing them */
186 foreach($this->dns as $key => $dn){
187 $acl = $this->ui->get_permissions($dn,"department/department");
188 if (preg_match('/d/', $acl)){
190 /* Delete request is permitted, perform LDAP action */
191 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
192 $this->deptabs->set_acl_base();
193 $this->deptabs->delete ();
194 $this->deptabs = NULL;
195 } else {
196 print_red (_("You have no permission to remove this department."));
197 }
198 /* Remove lock file after successfull deletion */
199 del_lock ($dn);
200 unset($this->dns[$key]);
201 }
202 }
205 /********************
206 Delete MULTIPLE entries Canceled
207 ********************/
209 /* Remove lock */
210 if(isset($_POST['delete_multiple_department_cancel'])){
211 foreach($this->dns as $key => $dn){
212 del_lock ($dn);
213 unset($this->dns[$key]);
214 }
215 }
218 /***************
219 Delete entry
220 ***************/
222 /* Delete Entry if Posted action (s_action) == del
223 * The entry which will be deleted is defined in $s_entry
224 */
225 if ($s_action =="del"){
226 $this->dn= $this->config->departments[trim($s_entry)];
228 /* check acls */
229 $acl = $this->ui->get_permissions($this->dn,"department/department");
230 if(preg_match("/d/",$acl)){
232 /* Check locking */
233 if (($user= get_lock($this->dn)) != ""){
234 $_SESSION['dn']= $this->dn;
235 return(gen_locked_message($user, $this->dn));
236 } else {
237 add_lock ($this->dn, $this->ui->dn);
238 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
239 $smarty->assign("multiple", false);
240 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
241 return ($display);
242 }
243 }else{
244 print_red (_("You have no permission to remove this department."));
245 }
246 }
249 /***************
250 Delete department confirmed
251 ***************/
253 /* If department deletion is accepted ...
254 * Finally delete department
255 */
256 if (isset($_POST['delete_department_confirm'])){
258 /* check acls */
259 $acl = $this->ui->get_permissions($this->dn,"department/department");
260 if(preg_match("/d/",$acl)){
261 $this->remove_from_parent();
262 } else {
263 print_red (_("You have no permission to remove this department."));
264 }
265 }
268 /***************
269 Edit department finished
270 ***************/
272 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
273 $this->deptabs->save_object();
274 $obj = $this->deptabs->by_object['department'];
275 if($obj->orig_dn != "new"){
276 if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
277 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
278 }
279 }
280 }
282 /* Save changes */
283 if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
285 /* Check tabs, will feed message array */
286 $message= $this->deptabs->check();
288 /* Save, or display error message? */
289 if (count($message) == 0){
291 $this->deptabs->save(true);
292 global $config;
293 $config->get_departments();
294 $config->make_idepartments();
295 $this->config = $config;
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 unset ($this->deptabs);
371 $this->deptabs= NULL;
372 unset ($_SESSION['objectinfo']);
373 }
376 /***************
377 Dialog canceled
378 ***************/
380 /* User canceled edit oder delete
381 * Cancel dialog
382 */
383 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
384 del_lock ($this->dn);
385 unset($this->depdabs);
386 $this->deptabs= NULL;
387 unset ($_SESSION['objectinfo']);
388 }
390 /* Headpage or normal plugin screen? */
391 if ($this->deptabs !== NULL){
393 /* Show main page (tabs) */
394 $display= $this->deptabs->execute();
395 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
396 $display.= "<p style=\"text-align:right\">\n";
397 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
398 $display.= " \n";
399 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
400 $display.= "</p>";
401 }
402 return ($display);
403 }else{
404 /* Display dialog with group list */
405 $this->DivListDepartment->parent = $this;
406 $this->DivListDepartment->execute();
407 $this->reload();
408 $this->DivListDepartment->DepartmentsAdded = true;
409 $this->DivListDepartment->setEntries($this->departments);
410 return($this->DivListDepartment->Draw());
411 }
412 }
415 function reload()
416 {
417 /* Vairaible init */
418 $base = $this->DivListDepartment->selectedBase;
419 $base_back = preg_replace("/^[^,]+,/","",$base);
420 $Regex = $this->DivListDepartment->Regex;
422 // Create Array to Test if we have a valid back button
423 $tmp = $_SESSION['config']->idepartments;
425 // In case of a valid back button create entry
426 if(isset($tmp[$base_back])){
427 $tmp2 ['dn'] = convert_department_dn($base_back);
429 // If empty always go to top
430 if(empty($tmp2['dn'])){
431 $tmp2['dn']="/";
432 }
433 $tmp2 ['description'][0] = ".. "._("Back");
434 $result[$tmp[$base_back]]=$tmp2;
435 }
437 if($this->DivListDepartment->SubSearch){
438 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
439 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
440 }else{
441 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
442 "department", $base, array("ou", "description"), GL_SIZELIMIT );
443 }
445 $this->departments= array();
447 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
448 if(preg_match("/^ou=/",$base)){
449 $this->departments [ convert_department_dn($base) ] = ".";
450 }
452 foreach ($res as $key => $value){
454 /* Don't display base as entry on subsearch */
455 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
456 continue;
457 }
459 $cdn= convert_department_dn($value['dn']);
461 /* Append to dep list */
462 if(isset($value["description"][0])){
463 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
464 }else{
465 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
466 }
467 }
468 natcasesort ($this->departments);
469 reset ($this->departments);
470 }
472 function remove_from_parent()
473 {
474 $ldap= $this->config->get_ldap_link();
475 $ldap->cd ($this->dn);
476 $ldap->recursive_remove();
478 /* Optionally execute a command after we're done */
479 $this->postremove();
481 /* Delete references to object groups */
482 $ldap->cd ($this->config->current['BASE']);
483 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
484 while ($ldap->fetch()){
485 $og= new ogroup($this->config, $ldap->getDN());
486 unset($og->member[$this->dn]);
487 $og->save ();
488 }
490 }
493 function list_get_selected_items()
494 {
495 $ids = array();
496 foreach($_POST as $name => $value){
497 if(preg_match("/^item_selected_[a-z0-9\\/\=]*$/i",$name)){
498 $id = preg_replace("/^item_selected_/","",$name);
499 $ids[$id] = $id;
500 }
501 }
502 return($ids);
503 }
506 function remove_lock()
507 {
508 if (isset($this->dn)){
509 del_lock ($this->dn);
510 }
511 }
513 function save_object()
514 {
515 /* reload department */
516 $this->config->get_departments();
518 $this->config->make_idepartments();
519 $this->DivListDepartment->config= $this->config;
520 $this->DivListDepartment->save_object();
521 }
523 }
524 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
525 ?>