1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class management
24 {
25 // Public
26 public $config = null;
27 public $ui = null;
29 // The plugin description
30 public $plugname = "unconfigured";
31 public $plIcon = "unconfigured";
32 public $plDescription = "unconfigured";
33 public $plHeadline = "unconfigured";
35 // The currently used object(s) (e.g. in edit, removal)
36 public $dn = ""; // this is public due to some compatibility problems with class plugin..
37 protected $dns = array();
39 // The last used object(s).
40 protected $last_dn = "";
41 protected $last_dns = array();
43 // The common places the displayed objects are stored in. (e.g. array("ou=groups,",".."))
44 protected $storagePoints = array();
46 // The tab definitions to use for the current object.
47 protected $tabClass = ""; // e.g. usertabs
48 protected $tabType = ""; // e.g. USERTABS
49 protected $aclPlugin = ""; // e.g. generic
50 protected $aclCategory = ""; // e.g. users
51 protected $objectName = ""; // e.g. users
53 // The opened object.
54 protected $tabObject = null;
55 protected $dialogObject = null;
57 // The last opened object.
58 protected $last_tabObject = null;
59 protected $last_dialogObject = null;
61 // Whether to display the apply button or not
62 protected $displayApplyBtn = FALSE;
64 // Whether to display a header or not.
65 protected $skipHeader = false;
67 // Whether to display a footer or not.
68 protected $skipFooter = false;
70 // Copy&Paste handler
71 protected $cpHandler = null;
73 // Indicates that we want to paste objects right now.
74 protected $cpPastingStarted = FALSE;
76 // The Snapshot handler class.
77 protected $snapHandler = null;
79 // The listing handlers
80 private $headpage = null;
81 private $filter = null;
83 // A list of configured actions/events
84 protected $actions = array();
86 // Attributes managed by this plugin, can be used in post events;
87 public $attributes = array();
89 function __construct(&$config,$ui,$plugname, $headpage)
90 {
91 $this->plugname = $plugname;
92 $this->headpage = $headpage;
93 $this->ui = $ui;
94 $this->config = $config;
96 if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
97 if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
99 if(empty($this->plIcon)){
100 $this->plIcon = "plugins/".$plugname."/images/plugin.png";
101 }
103 // Register default actions
104 $this->registerAction("new", "newEntry");
105 $this->registerAction("edit", "editEntry");
106 $this->registerAction("apply", "applyChanges");
107 $this->registerAction("save", "saveChanges");
108 $this->registerAction("cancel", "cancelEdit");
109 $this->registerAction("cancelDelete", "cancelEdit");
110 $this->registerAction("remove", "removeEntryRequested");
111 $this->registerAction("removeConfirmed", "removeEntryConfirmed");
113 $this->registerAction("copy", "copyPasteHandler");
114 $this->registerAction("cut", "copyPasteHandler");
115 $this->registerAction("paste", "copyPasteHandler");
117 $this->registerAction("snapshot", "createSnapshotDialog");
118 $this->registerAction("restore", "restoreSnapshotDialog");
119 $this->registerAction("saveSnapshot","saveSnapshot");
120 $this->registerAction("restoreSnapshot","restoreSnapshot");
121 $this->registerAction("removeSnapshotConfirmed","removeSnapshotConfirmed");
122 $this->registerAction("cancelSnapshot","closeDialogs");
124 $this->registerAction("config-filter","editFilter");
125 $this->registerAction("saveFilter","saveFilter");
126 $this->registerAction("cancelFilter","cancelFilter");
128 // To temporay disable the filter caching UNcomment this line.
129 #session::global_un_set(get_class($this)."_filter");
130 }
132 /*! \brief Execute this plugin
133 * Handle actions/events, locking, snapshots, dialogs, tabs,...
134 */
135 function execute()
136 {
137 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
138 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
139 session::set('LOCK_VARS_TO_USE',$vars);
141 pathNavigator::registerPlugin($this);
143 /* Display the copy & paste dialog, if it is currently open */
144 $ret = $this->copyPasteHandler("",array());
145 if($ret){
146 return($this->getHeader().$ret);
147 }
149 // Update filter
150 if ($this->filter) {
151 $this->filter->update();
152 session::global_set(get_class($this)."_filter", $this->filter);
153 session::set('autocomplete', $this->filter);
154 }
156 // Handle actions (POSTs and GETs)
157 $str = $this->handleActions($this->detectPostActions());
158 if($str) return($this->getHeader().$str);
160 // Open single dialog objects
161 if(is_object($this->dialogObject)){
162 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
163 if(method_exists($this->dialogObject,'execute')){
164 $display = $this->dialogObject->execute();
165 $display.= $this->_getTabFooter();
166 return($this->getHeader().$display);
167 }
168 }
170 // Display tab object.
171 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
172 # $this->tabObject->save_object();
173 $display = $this->tabObject->execute();
174 $display.= $this->_getTabFooter();
175 return($this->getHeader().$display);
176 }
178 // Set current restore base for snapshot handling.
179 if(is_object($this->snapHandler)){
180 $bases = array();
181 foreach($this->storagePoints as $sp){
182 $bases[] = $sp.$this->headpage->getBase();
183 }
185 // No bases specified? Try base
186 if(!count($bases)) $bases[] = $this->headpage->getBase();
188 $this->snapHandler->setSnapshotBases($bases);
189 }
191 // Display list
192 return($this->renderList());
193 }
195 function editFilter()
196 {
197 $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
198 }
200 function renderList()
201 {
202 $this->headpage->update();
203 $display = $this->headpage->render();
204 return($this->getHeader().$display);
205 }
207 function getHeadpage()
208 {
209 return($this->headpage);
210 }
212 function getFilter()
213 {
214 return($this->filter);
215 }
217 /*! \brief Generates the plugin header which is displayed whenever a tab object is
218 * opened.
219 */
220 protected function getHeader()
221 {
222 // We do not display any headers right now.
223 if(1 || $this->skipHeader) return("");
224 }
227 /*! \brief Generates the footer which is used whenever a tab object is
228 * displayed.
229 */
230 protected function _getTabFooter()
231 {
232 // Do not display tab footer for non tab objects
233 if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
234 return("");
235 }
237 // Check if there is a dialog opened - We don't need any buttons in this case.
238 if($this->tabObject->by_object[$this->tabObject->current]){
239 $current = $this->tabObject->by_object[$this->tabObject->current];
240 if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
241 return("");
242 }
243 }
245 // Skip footer if requested;
246 if($this->skipFooter) return("");
248 // In case an of locked entry, we may have opened a read-only tab.
249 $str = "";
250 if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
251 $str.= "
252 <p style=\"text-align:right\">
253 <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
254 </p>";
255 return($str);
256 }else{
258 // Display ok, (apply) and cancel buttons
259 $str.= "<p style=\"text-align:right\">\n";
260 $str.= "<button type=\"submit\" name=\"edit_finish\">".msgPool::okButton()."</button>\n";
261 $str.= " \n";
262 if($this->displayApplyBtn){
263 $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
264 $str.= " \n";
265 }
266 $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
267 $str.= "</p>";
268 }
269 return($str);
270 }
273 /*! \brief Initiates the removal for the given entries
274 * and displays a confirmation dialog.
275 *
276 * @param String 'action' The name of the action which was the used as trigger.
277 * @param Array 'target' A list of object dns, which should be affected by this method.
278 * @param Array 'all' A combination of both 'action' and 'target'.
279 */
280 protected function removeEntryRequested($action="",$target=array(),$all=array())
281 {
282 // Close dialogs and remove locks for currently handled dns
283 $this->cancelEdit();
285 $disallowed = array();
286 $this->dns = array();
288 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
290 // Check permissons for each target
291 $h = $this->getHeadpage();
292 $oTypes = array_reverse($h->objectTypes);
293 foreach($target as $dn){
294 $entry = $h->getEntry($dn);
295 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
296 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
297 if(preg_match("/d/",$acl)){
298 $this->dns[] = $dn;
299 }else{
300 $disallowed[] = $dn;
301 }
302 }
303 if(count($disallowed)){
304 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
305 }
307 // We've at least one entry to delete.
308 if(count($this->dns)){
310 // check locks
311 if ($user= get_multiple_locks($this->dns)){
312 return(gen_locked_message($user,$this->dns));
313 }
315 // Add locks
316 $dns_names = array();
317 $types = array();
319 // Build list of object -labels
320 foreach($h->objectTypes as $type){
321 $map[$type['objectClass']]= $type['label'];
322 }
324 foreach($this->dns as $dn){
325 $tmp = $h->getType($dn);
326 if(isset($map[$tmp])){
327 $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
328 }else{
329 $dns_names[] =LDAP::fix($dn);
330 }
331 }
332 add_lock ($this->dns, $this->ui->dn);
334 // Display confirmation dialog.
335 $smarty = get_smarty();
336 $smarty->assign("info", msgPool::deleteInfo($dns_names));
337 $smarty->assign("multiple", true);
338 return($smarty->fetch(get_template_path('removeEntries.tpl')));
339 }
340 }
343 /*! \brief Object removal was confirmed, now remove the requested entries.
344 *
345 * @param String 'action' The name of the action which was the used as trigger.
346 * @param Array 'target' A list of object dns, which should be affected by this method.
347 * @param Array 'all' A combination of both 'action' and 'target'.
348 */
349 function removeEntryConfirmed($action="",$target=array(),$all=array(),
350 $altTabClass="",$altTabType="", $altAclCategory="",$altAclPlugin="")
351 {
352 $tabType = $this->tabType;
353 $tabClass = $this->tabClass;
354 $aclCategory = $this->aclCategory;
355 $aclPlugin = $this->aclPlugin;
356 if(!empty($altTabClass)) $tabClass = $altTabClass;
357 if(!empty($altTabType)) $tabType = $altTabType;
358 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
359 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
361 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
363 // Check permissons for each target
364 $h = $this->getHeadpage();
365 $oTypes = array_reverse($h->objectTypes);
366 foreach($this->dns as $key => $dn){
367 $entry = $h->getEntry($dn);
368 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
369 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
371 // Check permissions, are we allowed to remove this object?
372 if(preg_match("/d/",$acl)){
374 // Delete the object
375 $this->dn = $dn;
376 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn,
377 $aclCategory, true, true);
378 $this->tabObject->set_acl_base($this->dn);
379 $this->tabObject->parent = &$this;
380 $this->tabObject->delete ();
382 // Remove the lock for the current object.
383 del_lock($this->dn);
384 } else {
385 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
386 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
387 }
388 }
390 // Cleanup
391 $this->remove_lock();
392 $this->closeDialogs();
393 }
396 /*! \brief Detects actions/events send by the ui
397 * and the corresponding targets.
398 */
399 function detectPostActions()
400 {
401 if(!is_object($this->headpage)){
402 trigger_error("No valid headpage given....!");
403 return(array());
404 }
405 $action= $this->headpage->getAction();
406 if(isset($_POST['edit_apply'])) $action['action'] = "apply";
407 if(isset($_POST['edit_finish'])) $action['action'] = "save";
408 if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
409 if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";
410 if(isset($_POST['delete_snapshot_confirm'])) $action['action'] = "removeSnapshotConfirmed";
411 if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";
412 if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";
413 if(isset($_POST['cancelFilter'])) $action['action'] = "cancelFilter";
415 // Detect Snapshot actions
416 if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";
417 if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";
418 foreach($_POST as $name => $value){
419 $once =TRUE;
420 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
421 $once = FALSE;
422 $entry = base64_decode(preg_replace("/^RestoreSnapShot_(.*)$/i","\\1",$name));
423 $action['action'] = "restoreSnapshot";
424 $action['targets'] = array($entry);
425 }
426 }
428 return($action);
429 }
432 /*! \brief Calls the registered method for a given action/event.
433 */
434 function handleActions($action)
435 {
436 // Start action
437 if(isset($this->actions[$action['action']])){
438 $func = $this->actions[$action['action']];
439 if(!isset($action['targets']))$action['targets']= array();
440 return($this->$func($action['action'],$action['targets'],$action));
441 }
442 }
445 /*! \brief Opens the snapshot creation dialog for the given target.
446 *
447 * @param String 'action' The name of the action which was the used as trigger.
448 * @param Array 'target' A list of object dns, which should be affected by this method.
449 * @param Array 'all' A combination of both 'action' and 'target'.
450 */
451 function createSnapshotDialog($action="",$target=array(),$all=array())
452 {
453 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
455 foreach($target as $entry){
456 if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
457 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
458 $this->dialogObject->aclCategories = array($this->aclCategory);
459 $this->dialogObject->parent = &$this;
461 }else{
462 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s!"), bold($entry)),
463 ERROR_DIALOG);
464 }
465 }
466 }
469 /*! \brief Creates a snapshot new entry - This method is called when the somebody
470 * clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
471 *
472 * @param String 'action' The name of the action which was the used as trigger.
473 * @param Array 'target' A list of object dns, which should be affected by this method.
474 * @param Array 'all' A combination of both 'action' and 'target'.
475 */
476 function saveSnapshot($action="",$target=array(),$all=array())
477 {
478 if(!is_object($this->dialogObject)) return;
479 $this->dialogObject->save_object();
480 $msgs = $this->dialogObject->check();
481 if(count($msgs)){
482 foreach($msgs as $msg){
483 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
484 }
485 }else{
486 $this->dn = $this->dialogObject->dn;
487 $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
488 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
489 $this->closeDialogs();
490 }
491 }
494 /*! \brief Restores a snapshot object.
495 * The dn of the snapshot entry has to be given as ['target'] parameter.
496 *
497 * @param String 'action' The name of the action which was the used as trigger.
498 * @param Array 'target' A list of object dns, which should be affected by this method.
499 * @param Array 'all' A combination of both 'action' and 'target'.
500 */
501 function restoreSnapshot($action="",$target=array(),$all=array())
502 {
503 $entry = array_pop($target);
504 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
505 $this->snapHandler->restore_snapshot($entry);
506 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
507 $this->closeDialogs();
508 }else{
509 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
510 ERROR_DIALOG);
511 }
512 }
515 /*! \brief Removes a snapshot object.
516 */
517 function removeSnapshotConfirmed($action="",$target=array(),$all=array())
518 {
519 $entry = $this->dialogObject->del_dn;
520 if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
521 $this->snapHandler->remove_snapshot($entry);
522 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot removed!");
523 }else{
524 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to remove a snapshot for %s!"), bold($entry)),
525 ERROR_DIALOG);
526 }
527 }
530 /*! \brief Displays the "Restore snapshot dialog" for a given target.
531 * If no target is specified, open the restore removed object
532 * dialog.
533 * @param String 'action' The name of the action which was the used as trigger.
534 * @param Array 'target' A list of object dns, which should be affected by this method.
535 * @param Array 'all' A combination of both 'action' and 'target'.
536 */
537 function restoreSnapshotDialog($action="",$target=array(),$all=array())
538 {
539 // Set current restore base for snapshot handling.
540 if(is_object($this->snapHandler)){
541 $bases = array();
542 foreach($this->storagePoints as $sp){
543 $bases[] = $sp.$this->headpage->getBase();
544 }
545 }
547 // No bases specified? Try base
548 if(!count($bases)) $bases[] = $this->headpage->getBase();
550 // No target, open the restore removed object dialog.
551 if(!count($target)){
552 $entry = $this->headpage->getBase();
553 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
554 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
555 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
556 $this->dialogObject->set_snapshot_bases($bases);
557 $this->dialogObject->display_all_removed_objects = true;
558 $this->dialogObject->display_restore_dialog = true;
559 $this->dialogObject->parent = &$this;
560 }else{
561 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
562 ERROR_DIALOG);
563 }
564 }else{
566 // Display the restore points for a given object.
567 $entry = array_pop($target);
568 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
569 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
570 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
571 $this->dialogObject->set_snapshot_bases($bases);
572 $this->dialogObject->display_restore_dialog = true;
573 $this->dialogObject->parent = &$this;
574 }else{
575 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
576 ERROR_DIALOG);
577 }
578 }
579 }
582 /*! \brief This method intiates the object creation.
583 *
584 * @param String 'action' The name of the action which was the used as trigger.
585 * @param Array 'target' A list of object dns, which should be affected by this method.
586 * @param Array 'all' A combination of both 'action' and 'target'.
587 */
588 function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
589 {
590 /* To handle mutliple object types overload this method.
591 * ...
592 * registerAction('newUser', 'newEntry');
593 * registerAction('newGroup','newEntry');
594 * ...
595 *
596 * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
597 * {
598 * switch($action){
599 * case 'newUser' : {
600 * mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
601 * }
602 * case 'newGroup' : {
603 * mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
604 * }
605 * }
606 * }
607 **/
608 $tabType = $this->tabType;
609 $tabClass = $this->tabClass;
610 $aclCategory = $this->aclCategory;
611 if(!empty($altTabClass)) $tabClass = $altTabClass;
612 if(!empty($altTabType)) $tabType = $altTabType;
613 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
615 // Check locking & lock entry if required
616 $this->displayApplyBtn = FALSE;
617 $this->dn = "new";
618 $this->is_new = TRUE;
619 $this->is_single_edit = FALSE;
620 $this->is_multiple_edit = FALSE;
622 set_object_info($this->dn);
624 // Open object.
625 if(empty($tabClass) || empty($tabType)){
626 // No tab type defined
627 }else{
628 if (isset($this->config->data['TABS'][$tabType])) {
629 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
630 $this->tabObject->set_acl_base($this->headpage->getBase());
631 $this->tabObject->parent = &$this;
632 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
633 } else {
634 msg_dialog::display(_("Error"), sprintf(_("No tab definition for %s found in configuration file: cannot create plugin instance!"), bold($tabType)), ERROR_DIALOG);
635 }
636 }
637 }
640 /*! \brief This method opens an existing object or a list of existing objects to be edited.
641 *
642 *
643 * @param String 'action' The name of the action which was the used as trigger.
644 * @param Array 'target' A list of object dns, which should be affected by this method.
645 * @param Array 'all' A combination of both 'action' and 'target'.
646 */
647 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
648 {
649 /* To handle mutliple object types overload this method.
650 * ...
651 * registerAction('editUser', 'editEntry');
652 * registerAction('editGroup','editEntry');
653 * ...
654 *
655 * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
656 * {
657 * switch($action){
658 * case 'editUser' : {
659 * mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
660 * }
661 * case 'editGroup' : {
662 * mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
663 * }
664 * }
665 * }
666 **/
668 // Do not create a new tabObject while there is already one opened,
669 // the user may have just pressed F5 to reload the page.
670 if(is_object($this->tabObject)){
671 return;
672 }
674 $tabType = $this->tabType;
675 $tabClass = $this->tabClass;
676 $aclCategory = $this->aclCategory;
677 if(!empty($altTabClass)) $tabClass = $altTabClass;
678 if(!empty($altTabType)) $tabType = $altTabType;
679 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
681 // Single edit - we only got one object dn.
682 if(count($target) == 1){
683 $this->displayApplyBtn = TRUE;
684 $this->is_new = FALSE;
685 $this->is_single_edit = TRUE;
686 $this->is_multiple_edit = FALSE;
688 // Get the dn of the object and creates lock
689 $this->dn = array_pop($target);
690 set_object_info($this->dn);
691 $user = get_lock($this->dn);
692 if ($user != ""){
693 return(gen_locked_message ($user, array($this->dn),TRUE));
694 }
695 add_lock ($this->dn, $this->ui->dn);
697 // Open object.
698 if(empty($tabClass) || empty($tabType)){
699 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
700 }else{
701 $tab = $tabClass;
702 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
703 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
704 $this->tabObject->set_acl_base($this->dn);
705 $this->tabObject->parent = &$this;
706 }
707 }else{
709 // We've multiple entries to edit.
710 $this->is_new = FALSE;
711 $this->is_singel_edit = FALSE;
712 $this->is_multiple_edit = TRUE;
714 // Open multiple edit handler.
715 if(empty($tabClass) || empty($tabType)){
716 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
717 }else{
718 $this->dns = $target;
719 $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
720 $this->dns,$this->headpage->getBase(),$aclCategory);
722 // Check for locked entries
723 if ($tmp->entries_locked()){
724 return($tmp->display_lock_message());
725 }
727 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
729 // Now lock entries.
730 if($tmp->multiple_available()){
731 $tmp->lock_entries($this->ui->dn);
732 $this->tabObject = $tmp;
733 set_object_info($this->tabObject->get_object_info());
734 }
735 }
736 }
737 }
740 /*! \brief Close filter dialog
741 */
742 protected function cancelFilter()
743 {
744 if($this->dialogObject instanceOf userFilter){
745 $this->remove_lock();
746 $this->closeDialogs();
747 }
748 }
751 /*! \brief Save filter modifcations.
752 */
753 protected function saveFilter()
754 {
755 if($this->dialogObject instanceOf userFilter){
756 $msgs = $this->dialogObject->check();
757 if(count($msgs)){
758 msg_dialog::displayChecks($msgs);
759 return("");
760 }else{
761 $this->dialogObject->save();
762 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
763 $this->remove_lock();
764 $this->closeDialogs();
766 // Ask filter to reload information
767 $this->filter->reloadFilters();
768 }
769 }
770 }
773 /*! \brief Save object modifications and closes dialogs (returns to object listing).
774 * - Calls '::check' to validate the given input.
775 * - Calls '::save' to save back object modifications (e.g. to ldap).
776 * - Calls '::remove_locks' to remove eventually created locks.
777 * - Calls '::closeDialogs' to return to the object listing.
778 */
779 protected function saveChanges()
780 {
781 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
782 $this->tabObject->save_object();
783 $msgs = $this->tabObject->check();
784 if(count($msgs)){
785 msg_dialog::displayChecks($msgs);
786 return("");
787 }else{
788 $this->tabObject->save();
789 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
790 $this->remove_lock();
791 $this->closeDialogs();
792 }
793 }elseif($this->dialogObject instanceOf plugin){
794 $this->dialogObject->save_object();
795 $msgs = $this->dialogObject->check();
796 if(count($msgs)){
797 msg_dialog::displayChecks($msgs);
798 return("");
799 }else{
800 $this->dialogObject->save();
801 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
802 $this->remove_lock();
803 $this->closeDialogs();
804 }
805 }
806 }
809 /*! \brief Save object modifications and keep dialogs opened.
810 * - Calls '::check' to validate the given input.
811 * - Calls '::save' to save back object modifications (e.g. to ldap).
812 */
813 protected function applyChanges()
814 {
815 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
816 $this->tabObject->save_object();
817 $msgs = $this->tabObject->check();
818 if(count($msgs)){
819 msg_dialog::displayChecks($msgs);
820 return("");
821 }else{
822 $this->tabObject->save();
823 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
824 $this->tabObject->re_init();
825 }
826 }
827 }
830 /*! \brief This method closes dialogs
831 * and cleans up the cached object info and the ui.
832 */
833 protected function closeDialogs()
834 {
835 $this->last_dn = $this->dn;
836 $this->last_dns = $this->dns;
837 $this->last_tabObject = $this->tabObject;
838 $this->last_dialogObject = $this->dialogObject;
839 $this->dn = "";
840 $this->dns = array();
841 $this->tabObject = null;
842 $this->dialogObject = null;
843 $this->skipFooter = FALSE;
844 set_object_info();
845 }
848 /*! \brief Editing an object was caneled.
849 * Close dialogs/tabs and remove locks.
850 */
851 protected function cancelEdit()
852 {
853 $this->remove_lock();
854 $this->closeDialogs();
855 }
858 /*! \brief Every click in the list user interface sends an event
859 * here can we connect those events to a method.
860 * eg. ::registerEvent('new','createUser')
861 * When the action/event new is send, the method 'createUser'
862 * will be called.
863 */
864 function registerAction($action,$target)
865 {
866 $this->actions[$action] = $target;
867 }
870 /*! \brief Removes ldap object locks created by this class.
871 * Whenever an object is edited, we create locks to avoid
872 * concurrent modifications.
873 * This locks will automatically removed here.
874 */
875 function remove_lock()
876 {
877 if(!empty($this->dn) && $this->dn != "new"){
878 del_lock($this->dn);
879 }
880 if(count($this->dns)){
881 del_lock($this->dns);
882 }
883 }
886 /*! \brief This method is used to queue and process copy&paste actions.
887 * Allows to copy, cut and paste mutliple entries at once.
888 * @param String 'action' The name of the action which was the used as trigger.
889 * @param Array 'target' A list of object dns, which should be affected by this method.
890 * @param Array 'all' A combination of both 'action' and 'target'.
891 */
892 function copyPasteHandler($action="",$target=array(),$all=array(),
893 $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
894 {
895 // Return without any actions while copy&paste handler is disabled.
896 if(!is_object($this->cpHandler)) return("");
898 $tabType = $this->tabType;
899 $tabClass = $this->tabClass;
900 $aclCategory = $this->aclCategory;
901 $aclPlugin = $this->aclPlugin;
902 if(!empty($altTabClass)) $tabClass = $altTabClass;
903 if(!empty($altTabType)) $tabType = $altTabType;
904 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
905 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
907 // Save user input
908 $this->cpHandler->save_object();
910 // Add entries to queue
911 if($action == "copy" || $action == "cut"){
912 $this->cpHandler->cleanup_queue();
913 foreach($target as $dn){
914 if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
915 $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
916 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
917 }
918 if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
919 $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
920 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
921 }
922 }
923 }
925 // Initiate pasting
926 if($action == "paste"){
927 $this->cpPastingStarted = TRUE;
928 }
930 // Display any c&p dialogs, eg. object modifications required before pasting.
931 if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
932 $this->cpHandler->SetVar("base",$this->headpage->getBase());
933 $data = $this->cpHandler->execute();
934 if(!empty($data)){
935 return($data);
936 }
937 }
939 // Automatically disable pasting process since there is no entry left to paste.
940 if(!$this->cpHandler->entries_queued()){
941 $this->cpPastingStarted = FALSE;
942 }
943 return("");
944 }
947 function setFilter($str) {
948 $this->filter = $str;
949 }
952 function postcreate() {
953 $this->_handlePostEvent('POSTCREATE');
954 }
955 function postmodify(){
956 $this->_handlePostEvent('POSTMODIFY');
957 }
958 function postremove(){
959 $this->_handlePostEvent('POSTREMOVE');
960 }
962 function _handlePostEvent($type)
963 {
965 /* Find postcreate entries for this class */
966 $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
967 if ($command != ""){
969 /* Walk through attribute list */
970 foreach ($this->attributes as $attr){
971 if (!is_array($this->$attr)){
972 $add_attrs[$attr] = $this->$attr;
973 }
974 }
975 $add_attrs['dn']=$this->dn;
977 $tmp = array();
978 foreach($add_attrs as $name => $value){
979 $tmp[$name] = strlen($name);
980 }
981 arsort($tmp);
983 /* Additional attributes */
984 foreach ($tmp as $name => $len){
985 $value = $add_attrs[$name];
986 $command= str_replace("%$name", "$value", $command);
987 }
989 if (check_command($command)){
990 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
991 $command, "Execute");
992 exec($command,$arr);
993 foreach($arr as $str){
994 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
995 $command, "Result: ".$str);
996 }
997 } else {
998 $message= msgPool::cmdnotfound($type, get_class($this));
999 msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
1000 }
1001 }
1002 }
1004 function is_modal_dialog()
1005 {
1006 return(is_object($this->tabObject) || is_object($this->dialogObject));
1007 }
1010 /*! \brief Forward command execution request
1011 * to the correct method.
1012 */
1013 function handle_post_events($mode, $addAttrs= array())
1014 {
1015 if(!in_array($mode, array('add','remove','modify'))){
1016 trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", bold($mode)));
1017 return;
1018 }
1019 switch ($mode){
1020 case "add":
1021 plugin::callHook($this,"POSTCREATE", $addAttrs);
1022 break;
1024 case "modify":
1025 plugin::callHook($this,"POSTMODIFY", $addAttrs);
1026 break;
1028 case "remove":
1029 plugin::callHook($this,"POSTREMOVE", $addAttrs);
1030 break;
1031 }
1032 }
1033 }
1035 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1036 ?>