Code

Updated application locking
[gosa.git] / gosa-plugins / goto / admin / applications / class_applicationManagement.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  */
21 class applicationManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline     = "Applications";
25   var $plDescription  = "This does something";
27   /* Dialog attributes */
28   var $apptabs                  = NULL;
29   var $ui                       = NULL;
30   var $CopyPasteHandler         = NULL;
31   var $DivListApplication       = NULL;
32   var $applications             = array();
33   var $enableReleaseManagement  = false;
34   var $start_pasting_copied_objects = FALSE;
36   var $app_base     ="";
37   var $app_release  ="";
39   var $dns = array();
41   function IsReleaseManagementActivated()
42   {
43     /* Check if we should enable the release selection */
44     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
45     if(!empty($tmp)){
46       return(true);
47     }
48     return(false);
49   }
52   function applicationManagement (&$config, &$ui)
53   {
54     /* Save configuration for internal use */
55     $this->config   = &$config;
56     $this->ui       = &$ui;
58     /* Check if copy & paste is activated */
59     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
60       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
61     }
63     /* Creat dialog object */
64     $this->DivListApplication = new divListApplication($this->config,$this);
66     if($this->IsReleaseManagementActivated()){
67     /* Check if we should enable the release selection */
68       $this->enableReleaseManagement = true;
70       /* Hide SubSearch checkbox */
71       $this->DivListApplication->DisableCheckBox("SubSearch");
72     }
74     /* Set default release */
75     if(!$this->IsReleaseManagementActivated()){
76       $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
77       if(!session::is_set("app_filter")){
78         session::set("app_filter",array("app_base" => $this->app_base));
79       }
80       $app_filter     = session::get("app_filter");
81       $this->app_base = $app_filter['app_base'];
82     }else{
83       $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
84       if(!session::is_set("app_filter")){
85         session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
86       }
87       $app_filter         = session::get("app_filter");
88       $this->app_base     = $app_filter['app_base'];
89       $this->app_release  = $app_filter['app_release'];
90     }
91   }
94   function getReleases()
95   {
96     $ldap = $this->config->get_ldap_link();
97     $ret  = array();
98     $base = $this->app_base; 
100     $ret[$this->app_base] = "/";
101     $ldap->cd($base);
102     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
103     while($attrs = $ldap->fetch()){
104       $str = str_replace($base,"",$attrs['dn']);
105       $tmp = array_reverse( split("ou=",$str));
106       $str = "";
107       foreach($tmp as $val){
108         $val = trim(preg_replace("/,/","",$val));
109         if(empty($val)) break;
110         $str .= "/".$val;
111       } 
112       if(!empty($str)){
113         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
114       }
115     }
116     asort($ret);
117     return($ret);
118   }
120   function execute()
121   {
122     /* Call parent execute */
123     plugin::execute();
126     /**************** 
127       Variable init 
128      ****************/
130     /* These vars will be stored if you try to open a locked app, 
131         to be able to perform your last requests after showing a warning message */
132     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
134     $smarty       = get_smarty();             // Smarty instance
135     $s_action     = "";                       // Contains the action to proceed
136     $s_entry      = "";                       // The value for s_action
137     $base_back    = "";                       // The Link for Backbutton
138     
139     /* Test Posts */
140     foreach($_POST as $key => $val){
141       // Post for delete
142       if(preg_match("/appl_del.*/",$key)){
143         $s_action = "del";
144         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
145         // Post for edit
146       }elseif(preg_match("/appl_edit_.*/",$key)){
147         $s_action="edit";
148         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
149         // Post for new
150       }elseif(preg_match("/^copy_.*/",$key)){
151         $s_action="copy";
152         $s_entry  = preg_replace("/^copy_/i","",$key);
153       }elseif(preg_match("/^cut_.*/",$key)){
154         $s_action="cut";
155         $s_entry  = preg_replace("/^cut_/i","",$key);
156         // Post for new
157       }elseif(preg_match("/^appl_new.*/",$key)){
158         $s_action="new";
159       }elseif(preg_match("/^remove_multiple_applications/",$key)){
160         $s_action="del_multiple";
161       }elseif(preg_match("/^editPaste.*/i",$key)){
162         $s_action="editPaste";
163       }elseif(preg_match("/^multiple_copy_groups/",$key)){
164         $s_action = "copy_multiple";
165       }elseif(preg_match("/^multiple_cut_groups/",$key)){
166         $s_action = "cut_multiple";
167       }
168     }
170     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
171       $s_action ="edit";
172       $s_entry  = $_GET['id'];
173     }
175     $s_entry  = preg_replace("/_.$/","",$s_entry);
178     /* handle C&P from layers menu */
179     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
180       $s_action = "copy_multiple";
181     }
182     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
183       $s_action = "cut_multiple";
184     }
185     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
186       $s_action = "editPaste";
187     }
189     /* Create options */
190     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
191       $s_action = "new";
192     }
194     /* handle remove from layers menu */
195     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
196       $s_action = "del_multiple";
197     }
199     /**************** 
200       Copy & Paste handling  
201      ****************/
203     /* Display the copy & paste dialog, if it is currently open */
204     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
205     if($ret){
206       return($ret);
207     }
209     /**************** 
210       Create a new app 
211      ****************/
213     /* New application? */
214     if ($s_action=="new"){
216       /* By default we set 'dn' to 'new', all relevant plugins will
217          react on this. */
218       $this->dn= "new";
220       /* Create new usertab object */
221       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
222       $this->apptabs->parent = &$this;
223       $this->apptabs->set_acl_base($this->app_base);
224     }
227     /**************** 
228       Edit entry canceled 
229      ****************/
231     /* Cancel dialogs */
232     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
233       $this->remove_lock();
234       $this->apptabs= NULL;
235       session::un_set('objectinfo');
236     }
239     /**************** 
240       Edit entry finished 
241      ****************/
243     /* Finish apps edit is triggered by the tabulator dialog, so
244        the user wants to save edited data. Check and save at this
245        point. */
246     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
248       /* Check tabs, will feed message array */
249       $this->apptabs->last= $this->apptabs->current;
250       $this->apptabs->save_object();
251       $message= $this->apptabs->check();
253       /* Save, or display error message? */
254       if (count($message) == 0){
256         /* Save data data to ldap */
257         $this->apptabs->save();
259         if (!isset($_POST['edit_apply'])){
260           /* Application has been saved successfully, remove lock from
261              LDAP. */
262           if ($this->dn != "new"){
263             $this->remove_lock();
264           }
265           $this->apptabs= NULL;
266           session::un_set('objectinfo');
267         }else{
269           /* Reinitialize tab */
270           if($this->apptabs instanceof tabs){
271             $this->apptabs->re_init();
272           }
273         }
274       } else {
275         /* Ok. There seem to be errors regarding to the tab data,
276            show message and continue as usual. */
277         msg_dialog::displayChecks($message);
278       }
279     }
282     /**************** 
283       Edit entry  
284      ****************/
286     /* User wants to edit data? */
287     if (($s_action=="edit") && (!isset($this->apptabs->config))){
289       /* Get 'dn' from posted 'applist', must be unique */
290       $this->dn= $this->applications[$s_entry]['dn'];
292       /* Check locking, save current plugin in 'back_plugin', so
293          the dialog knows where to return. */
294       if (($user= get_lock($this->dn)) != ""){
295         return(gen_locked_message ($user, $this->dn));
296       }
298       /* Lock the current entry, so everyone will get the
299          above dialog */
300       add_lock ($this->dn, $this->ui->dn);
302       /* Register apptabs to trigger edit dialog */
303       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
304       $this->apptabs->parent = &$this;
305       $this->apptabs->set_acl_base($this->dn);
306       session::set('objectinfo',$this->dn);
307     }
311     /********************
312       Delete MULTIPLE entries requested, display confirm dialog
313      ********************/
315     if ($s_action=="del_multiple"){
316       $ids = $this->list_get_selected_items();
318       if(count($ids)){
320         foreach($ids as $id){
321           $dn = $this->applications[$id]['dn'];
322           $this->dns[$id] = $dn;
323         }
324         if ($user= get_multiple_locks($this->dns)){
325           return(gen_locked_message($user,$this->dns));
326         }
328         $dns_names = array();
329         foreach($this->dns as $dn){
330           add_lock ($dn, $this->ui->dn);
331           $dns_names[] =@LDAP::fix($dn);
332         }
334         /* Lock the current entry, so nobody will edit it during deletion */
335         $smarty->assign("intro",  msgPool::deleteInfo($dns_names,_("application")));
336         $smarty->assign("multiple", true);
337         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
338       }
339     }
342     /********************
343       Delete MULTIPLE entries confirmed
344      ********************/
346     /* Confirmation for deletion has been passed. Users should be deleted. */
347     if (isset($_POST['delete_multiple_application_confirm'])){
349       /* Remove user by user and check acls before removeing them */
350       foreach($this->dns as $key => $dn){
352         $ui = get_userinfo();
353         $acl = $ui->get_permissions($dn ,"application/application");
354         if (preg_match('/d/', $acl)){
356           /* Delete request is permitted, perform LDAP action */
357           $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
358           $this->apptabs->parent = &$this;
359           $this->apptabs->set_acl_base($dn);
360           $this->apptabs->delete ();
361           unset ($this->apptabs);
362           $this->apptabs= NULL;
364         } else {
365           /* Normally this shouldn't be reached, send some extra
366              logs to notify the administrator */
367           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
368           new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
369         }
370       }
372       /* Remove lock file after successfull deletion */
373       $this->remove_lock();
374       $this->dns = array();
375     }
378     /********************
379       Delete MULTIPLE entries Canceled
380      ********************/
382     /* Remove lock */
383     if(isset($_POST['delete_multiple_application_cancel'])){
385       /* Remove lock file after successfull deletion */
386       $this->remove_lock();
387       $this->dns = array();
388     }
390     /**************** 
391       Delete app 
392      ****************/
394     /* Remove user was requested */
395     if ($s_action == "del"){
397       /* Get 'dn' from posted 'uid' */
398       $this->dn= $this->applications[$s_entry]['dn'];
400       /* Load permissions for selected 'dn' and check if
401          we're allowed to remove this 'dn' */
402       $ui = get_userinfo();
403       $acl = $ui->get_permissions($this->dn ,"application/application");
405       if(preg_match("/d/",$acl)){
406         /* Check locking, save current plugin in 'back_plugin', so
407            the dialog knows where to return. */
408         if (($user= get_lock($this->dn)) != ""){
409           return (gen_locked_message ($user, $this->dn));
410         }
412         /* Lock the current entry, so nobody will edit it during deletion */
413         add_lock ($this->dn, $this->ui->dn);
414         $smarty= get_smarty();
415         $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
416         $smarty->assign("multiple", false);
417         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
418       } else {
420         /* Obviously the user isn't allowed to delete. Show message and
421            clean session. */
422         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
423       }
424     }
427     /**************** 
428       Delete app confirmed 
429      ****************/
431     /* Confirmation for deletion has been passed. Group should be deleted. */
432     if (isset($_POST['delete_app_confirm'])){
434       /* Some nice guy may send this as POST, so we've to check
435          for the permissions again. */
436       $ui = get_userinfo();
437       $acl = $ui->get_permissions($this->dn ,"application/application");
439       if(preg_match("/d/",$acl)){
441         /* Delete request is permitted, perform LDAP action */
442         $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
443         $this->apptabs->parent = &$this;
444         $this->apptabs->set_acl_base($this->dn);
445         $this->apptabs->delete ();
446         unset ($this->apptabs);
447         $this->apptabs= NULL;
449       } else {
451         /* Normally this shouldn't be reached, send some extra
452            logs to notify the administrator */
453         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
454         new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
455       }
457       /* Remove lock file after successfull deletion */
458       $this->remove_lock();
459     }
462     /**************** 
463       Delete app canceled 
464      ****************/
466     /* Delete application canceled? */
467     if (isset($_POST['delete_cancel'])){
468       $this->remove_lock();
469       session::un_set('objectinfo');
470     }
472     /* Show tab dialog if object is present */
473     if (($this->apptabs) && (isset($this->apptabs->config))){
474       $display= $this->apptabs->execute();
476       /* Don't show buttons if tab dialog requests this */
477       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
478         $display.= "<p style=\"text-align:right\">\n";
479         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
480         $display.= "&nbsp;\n";
481         if ($this->dn != "new"){
482           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
483           $display.= "&nbsp;\n";
484         }
485         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
486         $display.= "</p>";
487       }
488       return ($display);
489     }
492     /****************
493       Dialog display
494      ****************/
496     /* Check if there is a snapshot dialog open */
497     if($this->IsReleaseManagementActivated()){
498       $base = $this->app_release;  
499     }else{
500       $base = $this->app_base;  
501     }
502     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
503       return($str);
504     }
506     /* Display dialog with system list */
507     $this->DivListApplication->parent = $this;
508     $this->DivListApplication->execute();
509     if(!$this->IsReleaseManagementActivated()){
510       $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
511     } 
512     $this->reload();
513     $this->DivListApplication->setEntries($this->applications);
514     return($this->DivListApplication->Draw());
515   }
518   /* Return departments, that will be included within snapshot detection */
519   function get_used_snapshot_bases()
520   {
521     if($this->IsReleaseManagementActivated()){
522       return(array($this->app_release));  
523     }else{
524       return(array($this->app_base));  
525     }
526   }
529   function reload()
530   {
531     $this->applications= array();
533     /* Set base for all searches */
534     $Regex      = $this->DivListApplication->Regex;
535     $SubSearch  = $this->DivListApplication->SubSearch; 
536     $Flags      =  GL_NONE | GL_SIZELIMIT;
537     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
538     $tmp        = array();
540     if(!$this->IsReleaseManagementActivated()){
541       $use_base = $this->app_base;
542       if($SubSearch){
543         $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
544       }
545     }else{
546       $use_base = $this->app_release;
547       $SubSearch= FALSE;
548     }
549   
550     if($SubSearch){ 
551       $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
552     }else{
553       $res= get_list($Filter, "application",$use_base, array("cn","description","dn","objectClass"), $Flags);
554     }
555     $tmp2 = array();
556     foreach ($res as $val){
557       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
558       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
559     }
561     natcasesort($tmp2);
562     $this->applications=array();
563     foreach($tmp2 as $val){
564       $this->applications[]=$tmp[$val];
565     }
566     reset ($this->applications);
567   }
569   function remove_from_parent()
570   {
571     /* Optionally execute a command after we're done */
572     $this->postremove();
573   }
576   function copyPasteHandling_from_queue($s_action,$s_entry)
577   {
578     /* Check if Copy & Paste is disabled */
579     if(!is_object($this->CopyPasteHandler)){
580       return("");
581     }
583     /* Add a single entry to queue */
584     if($s_action == "cut" || $s_action == "copy"){
586       /* Cleanup object queue */
587       $this->CopyPasteHandler->cleanup_queue();
588       $dn = $this->applications[$s_entry]['dn'];
589       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
590     }
593     /* Add entries to queue */
594     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
596       /* Cleanup object queue */
597       $this->CopyPasteHandler->cleanup_queue();
599       /* Add new entries to CP queue */
600       foreach($this->list_get_selected_items() as $id){
601         $dn = $this->applications[$id]['dn'];
603         if($s_action == "copy_multiple"){
604           $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
605         }
606         if($s_action == "cut_multiple"){
607           $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
608         }
609       }
610     }
612     /* Start pasting entries */
613     if($s_action == "editPaste"){
614       $this->start_pasting_copied_objects = TRUE;
615     }
618     /* Return C&P dialog */
619     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
622       /* Get dialog */
623       $this->CopyPasteHandler->SetVar("base",$this->app_base);
624       $this->CopyPasteHandler->SetVar("parent",$this);
625       $data = $this->CopyPasteHandler->execute();
627       /* Return dialog data */
628       if(!empty($data)){
629         return($data);
630       }
631     }
633     /* Automatically disable status for pasting */
634     if(!$this->CopyPasteHandler->entries_queued()){
635       $this->start_pasting_copied_objects = FALSE;
636     }
637     return("");
638   }
641   function list_get_selected_items()
642   {
643     $ids = array();
644     foreach($_POST as $name => $value){
645       if(preg_match("/^item_selected_[0-9]*$/",$name)){
646         $id   = preg_replace("/^item_selected_/","",$name);
647         $ids[$id] = $id;
648       }
649     }
650     return($ids);
651   }
654   /* Save to LDAP */
655   function save()
656   {
657     /* Optionally execute a command after we're done */
658     $this->postcreate();
659   }
662   function remove_lock()
663   {
664     if (isset($this->apptabs->dn)){
665       del_lock ($this->apptabs->dn);
666     }
667     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
668       del_lock($this->dn);
669     }
670     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
671       del_lock($this->dns);
672     }
673   }
676   function save_object() 
677   {
678     $this->DivListApplication->save_object();
679     if(is_object($this->CopyPasteHandler)){
680       $this->CopyPasteHandler->save_object();
681     }
682     
683     if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
684       $sel_rel = get_post('app_release');
685       $releases = array_flip($this->getReleases());
686       if(isset($releases[$sel_rel])){
687         $this->app_release = $releases[$sel_rel];
688       }
689       $app_filter     = session::get("app_filter");
690       $app_filter['app_release'] = $this->app_release;
691       session::set("app_filter",$app_filter);
692     }elseif(!$this->IsReleaseManagementActivated()){
693       $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
694       $app_filter     = session::get("app_filter");
695       $app_filter['app_base'] = $this->app_base;
696       session::set("app_filter",$app_filter);
697     }
698   }
700   function check() {}
701   function adapt_from_template($dn, $skip= array()) {}
702   function password_change_needed() {}
704 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
705 ?>