Code

Added new application management dialog
[gosa.git] / plugins / 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  */
20 require "tabs_application.inc";
22 class applicationManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline     = "Applications";
26   var $plDescription  = "This does something";
28   /* Dialog attributes */
29   var $apptabs                  = NULL;
30   var $ui                       = NULL;
31   var $CopyPasteHandler         = NULL;
32   var $DivListApplication       = NULL;
33   var $applications             = array();
34   var $acl                      = "";
35   var $enableReleaseManagement  = false;
37   function applicationManagement ($config, $ui)
38   {
39     /* Save configuration for internal use */
40     $this->config   = $config;
41     $this->ui       = $ui;
43     /* Check if copy & paste is activated */
44     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
45       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
46     }
48     /* Creat dialog object */
49     $this->DivListApplication = new divListApplication($this->config,$this);
51     /* Check if we should enable the release selection */
52     $tmp = search_config($this->config->data,"faiManagement","CLASS");
53     if(!empty($tmp)){
54       $this->enableReleaseManagement = true;
56       /* Hide SubSearch checkbox */
57       $this->DivListApplication->DisableCheckBox("SubSearch");
58     }
59   }
61   function getReleases($base)
62   {
63     $ldap                   = $this->config->get_ldap_link();
64     $dn                     = "ou=apps,".$base;
65     $ret                    = array();
66     $ret [$base] = "/";
68     $ldap->cd($dn);
69     $ldap->search("objectClass=organizationalUnit",array("ou"));
71     while($attrs = $ldap->fetch()){
72       $str = str_replace($dn,"",$attrs['dn']);
73       $tmp = array_reverse( split("ou=",$str));
74       $str = "";
75       foreach($tmp as $val){
76         $val = trim(preg_replace("/,/","",$val));
77         if(empty($val)) break;
78         $str .= "/".$val;
79       } 
80       if(!empty($str)){
81         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
82       }
83     }
84     asort($ret);
85     return($ret);
86   }
88   function execute()
89   {
90     /* Call parent execute */
91     plugin::execute();
94     /**************** 
95       Variable init 
96      ****************/
98     /* These vars will be stored if you try to open a locked app, 
99         to be able to perform your last requests after showing a warning message */
100     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/");
102     $smarty       = get_smarty();             // Smarty instance
103     $s_action     = "";                       // Contains the action to proceed
104     $s_entry      = "";                       // The value for s_action
105     $base_back    = "";                       // The Link for Backbutton
106     
107     /* Test Posts */
108     foreach($_POST as $key => $val){
109       // Post for delete
110       if(preg_match("/appl_del.*/",$key)){
111         $s_action = "del";
112         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
113         // Post for edit
114       }elseif(preg_match("/appl_edit_.*/",$key)){
115         $s_action="edit";
116         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
117         // Post for new
118       }elseif(preg_match("/^copy_.*/",$key)){
119         $s_action="copy";
120         $s_entry  = preg_replace("/^copy_/i","",$key);
121       }elseif(preg_match("/^cut_.*/",$key)){
122         $s_action="cut";
123         $s_entry  = preg_replace("/^cut_/i","",$key);
124         // Post for new
125       }elseif(preg_match("/^appl_new.*/",$key)){
126         $s_action="new";
127       }elseif(preg_match("/^editPaste.*/i",$key)){
128         $s_action="editPaste";
129       }
130     }
132     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
133       $s_action ="edit";
134       $s_entry  = $_GET['id'];
135     }
137     $s_entry  = preg_replace("/_.$/","",$s_entry);
140     /**************** 
141       Copy & Paste handling  
142      ****************/
144     /* Only perform copy / paste if it is enabled 
145      */
146     if($this->CopyPasteHandler){
147       if($str = $this->copyPasteHandling($s_action,$s_entry)){
148        return($str);
149       };
150     }
153     /**************** 
154       Create a new app 
155      ****************/
157     /* New application? */
158     if ($s_action=="new"){
160       /* By default we set 'dn' to 'new', all relevant plugins will
161          react on this. */
162       $this->dn= "new";
164       /* Create new usertab object */
165       $this->apptabs= new apptabs($this->config,
166           $this->config->data['TABS']['APPSTABS'], $this->dn);
167       $this->apptabs->set_acl(array(':all'));
168     }
171     /**************** 
172       Edit entry canceled 
173      ****************/
175     /* Cancel dialogs */
176     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
177       del_lock ($this->apptabs->dn);
178       unset ($this->apptabs);
179       $this->apptabs= NULL;
180       unset ($_SESSION['objectinfo']);
181     }
184     /**************** 
185       Edit entry finished 
186      ****************/
188     /* Finish apps edit is triggered by the tabulator dialog, so
189        the user wants to save edited data. Check and save at this
190        point. */
191     if ((isset($_POST['edit_finish'])) && (isset($this->apptabs->config))){
193       /* Check tabs, will feed message array */
194       $this->apptabs->last= $this->apptabs->current;
195       $this->apptabs->save_object();
196       $message= $this->apptabs->check();
198       /* Save, or display error message? */
199       if (count($message) == 0){
201         /* Save data data to ldap */
202         $this->apptabs->set_release($this->DivListApplication->selectedRelease);
203         $this->apptabs->save();
204         gosa_log ("Application object'".$this->dn."' has been saved");
206         /* Application has been saved successfully, remove lock from
207            LDAP. */
208         if ($this->dn != "new"){
209           del_lock ($this->dn);
210         }
211         unset ($this->apptabs);
212         $this->apptabs= NULL;
213         unset ($_SESSION['objectinfo']);
214       } else {
215         /* Ok. There seem to be errors regarding to the tab data,
216            show message and continue as usual. */
217         show_errors($message);
218       }
219     }
222     /**************** 
223       Edit entry  
224      ****************/
226     /* User wants to edit data? */
227     if (($s_action=="edit") && (!isset($this->apptabs->config))){
229       /* Get 'dn' from posted 'applist', must be unique */
230       $this->dn= $this->applications[$s_entry]['dn'];
232       /* Check locking, save current plugin in 'back_plugin', so
233          the dialog knows where to return. */
234       if (($user= get_lock($this->dn)) != ""){
235         return(gen_locked_message ($user, $this->dn));
236       }
238       /* Lock the current entry, so everyone will get the
239          above dialog */
240       add_lock ($this->dn, $this->ui->dn);
242       /* Set up the users ACL's for this 'dn' */
243       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
245       /* Register apptabs to trigger edit dialog */
246       $this->apptabs= new apptabs($this->config,
247           $this->config->data['TABS']['APPSTABS'], $this->dn);
248       $this->apptabs->set_acl($acl);
249       $_SESSION['objectinfo']= $this->dn;
250     }
253     /**************** 
254       Delete app 
255      ****************/
257     /* Remove user was requested */
258     if ($s_action == "del"){
260       /* Get 'dn' from posted 'uid' */
261       $this->dn= $this->applications[$s_entry]['dn'];
263       /* Load permissions for selected 'dn' and check if
264          we're allowed to remove this 'dn' */
265       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
266       $this->acl= get_module_permission($acl, "application", $this->dn);
267       if (chkacl($this->acl, "delete") == ""){
269         /* Check locking, save current plugin in 'back_plugin', so
270            the dialog knows where to return. */
271         if (($user= get_lock($this->dn)) != ""){
272           return (gen_locked_message ($user, $this->dn));
273         }
275         /* Lock the current entry, so nobody will edit it during deletion */
276         add_lock ($this->dn, $this->ui->dn);
277         $smarty= get_smarty();
278         $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
279         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
280       } else {
282         /* Obviously the user isn't allowed to delete. Show message and
283            clean session. */
284         print_red (_("You are not allowed to delete this application!"));
285       }
286     }
289     /**************** 
290       Delete app confirmed 
291      ****************/
293     /* Confirmation for deletion has been passed. Group should be deleted. */
294     if (isset($_POST['delete_app_confirm'])){
296       /* Some nice guy may send this as POST, so we've to check
297          for the permissions again. */
298       if (chkacl($this->acl, "delete") == ""){
300         /* Delete request is permitted, perform LDAP action */
301         $this->apptabs= new apptabs($this->config,
302             $this->config->data['TABS']['APPSTABS'], $this->dn);
303         $this->apptabs->set_acl(array($this->acl));
304         $this->apptabs->delete ();
305         gosa_log ("Application object'".$this->dn."' has been removed");
306         unset ($this->apptabs);
307         $this->apptabs= NULL;
309       } else {
311         /* Normally this shouldn't be reached, send some extra
312            logs to notify the administrator */
313         print_red (_("You are not allowed to delete this application!"));
314         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
315       }
317       /* Remove lock file after successfull deletion */
318       del_lock ($this->dn);
319     }
322     /**************** 
323       Delete app canceled 
324      ****************/
326     /* Delete application canceled? */
327     if (isset($_POST['delete_cancel'])){
328       del_lock ($this->dn);
329       unset($_SESSION['objectinfo']);
330     }
332     /* Show tab dialog if object is present */
333     if (($this->apptabs) && (isset($this->apptabs->config))){
334       $display= $this->apptabs->execute();
336       /* Don't show buttons if tab dialog requests this */
337       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
338         $display.= "<p style=\"text-align:right\">\n";
339         $display.= "<input type=\"submit\" name=\"edit_finish\" value=\""._("Save")."\">\n";
340         $display.= "&nbsp;\n";
341         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
342         $display.= "</p>";
343       }
344       return ($display);
345     }
348     /****************
349       Dialog display
350      ****************/
352     /* Display dialog with system list */
353     $this->DivListApplication->execute();
354     $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase);
355     $this->reload();
356     $this->DivListApplication->setEntries($this->applications);
357     return($this->DivListApplication->Draw());
358   }
361   function reload()
362   {
363     $this->applications= array();
365     /* Set base for all searches */
366     $base       = $this->DivListApplication->selectedBase;
367     $release    = $this->DivListApplication->selectedRelease;
368     $Regex      = $this->DivListApplication->Regex;
369     $SubSearch  = $this->DivListApplication->SubSearch; 
370     $Flags      =  GL_NONE | GL_SIZELIMIT;
371     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
372     $tmp        = array();
373     $Releases   = $this->getReleases($base);
375     if(!$this->enableReleaseManagement){
376       $use_base = $base;
377     }else{
378       if(isset($Releases[$release])){
379         $use_base  = $release;
380       }else{
381         $use_base  = $base;
382       }
383     }
385     if($SubSearch){
386       $Flags    |= GL_SUBSEARCH;  
387     }
388    
389     $res= get_list($Filter, $this->ui->subtreeACL,$use_base, array("cn","description","dn","objectClass"), $Flags);
390     foreach ($res as $val){
391       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
392     }
394     ksort($tmp);
395     $this->applications=array();
396     foreach($tmp as $val){
397       $this->applications[]=$val;
398     }
399     reset ($this->applications);
400   }
402   function remove_from_parent()
403   {
404     /* Optionally execute a command after we're done */
405     $this->postremove();
406   }
409   function copyPasteHandling($s_action,$s_entry)
410   {
411     /* Paste copied/cutted object in here
412      */
413     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
414       $this->CopyPasteHandler->save_object();
415       $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
417       if($str = $this->CopyPasteHandler->execute()) {
418         return($str);
419       }
420     }
422     /* Copy current object to CopyHandler
423      */
424     if($s_action == "copy"){
425       $this->CopyPasteHandler->Clear();
426       $dn = $this->applications[$s_entry]['dn'];
427       $obj    = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
428       $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new");
429       $this->CopyPasteHandler->Copy($obj,$objNew);
430     }
432     /* Copy current object to CopyHandler
433      */
434     if($s_action == "cut"){
435       $this->CopyPasteHandler->Clear();
436       $dn = $this->applications[$s_entry]['dn'];
437       $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
438       $this->CopyPasteHandler->Cut($obj);
439     }
440   }
442   /* Save to LDAP */
443   function save()
444   {
445     /* Optionally execute a command after we're done */
446     $this->postcreate();
447   }
449   function remove_lock()
450   {
451     if (isset($this->apptabs->dn)){
452       del_lock ($this->apptabs->dn);
453     }
454   }
456   function save_object() {
457     $this->DivListApplication->save_object();
458   }
460   function check() {}
461   function adapt_from_template($dn) {}
462   function password_change_needed() {}
463   function show_header($button_text, $text, $disabled= FALSE) {}
465 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
466 ?>