Code

Updated licenses
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_opsiLicenses.inc
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: class_licenseManagement.inc 13520 2009-03-09 14:54:13Z hickert $$
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 opsiLicenses extends plugin
24 {
25   /* Definitions */
26   var $plHeadline= "Opsi";
27   var $plDescription= "Opsi licenses";
29   // The headpage list handler. 
30   var $DivListLicenses    = NULL;
32   // A list of currently visible licenses
33   var $licenses = array();
35   // A list of currently edited/removed/aso licenses.
36   var $dns = array();
38   // Permission modules to use.
39   var $acl_module   = array("opsi");  
41   // Construct and initialize the plugin 
42   function __construct (&$config, $dn= NULL)
43   {
44     // Include config object 
45     $this->config= &$config;
46     $this->ui= get_userinfo();
48     // Initialize the corresponding list class.
49     $this->DivListLicenses = new divListLicense($this->config,$this);
50   }
53   function execute()
54   {
55     // Call parent execute 
56     plugin::execute();
58     // Variables to restore after 'entry locked' warning was displayed 
59     session::set('LOCK_VARS_TO_USE',array('/^license_/','/^act/',
60           '/^id/','/^menu_action/','/^item/'));
62     $smarty     = get_smarty();
64     /***************
65      * Handle _POST/_GET variables
66      ***************/
67    
68     // Get entry related posts 
69     $s_action   = "";
70     $s_entry    = "";
71     foreach($_POST as $name => $value){
72       if(preg_match("/^license_edit_/",$name)){
73         $s_action = "edit";  
74         $s_entry = preg_replace("/^license_edit_([0-9]*)_.*$/","\\1",$name);
75         break;
76       }
77       if(preg_match("/^license_del_/",$name)){
78         $s_action = "remove";  
79         $s_entry = preg_replace("/^license_del_([0-9]*)_.*$/","\\1",$name);
80         break;
81       }
82     }
84     if(isset($_GET['act']) && $_GET['act'] == "edit_entry" && isset($_GET['id'])){
85       $id = $_GET['id'];
86       if(isset($this->licenses[$id])){
87         $s_action = "edit";
88         $s_entry = $id;
89       }
90     }
91  
92     // Get menu related posts 
93     if(isset($_POST['menu_action'])) {
94       if($_POST['menu_action'] == "remove_multiple_licenses"){
95         $s_action = "remove_multiple";
96       }
97     }
101     /***************
102      * Remove handling
103      ***************/
105     if($s_action == "remove_multiple" || $s_action == "remove"){
106     
107       if($s_action == "remove_multiple"){
108         $ids = $this->list_get_selected_items();
109       }else{
110         $ids = array($s_entry);
111       }
113       if(count($ids)){
114         $this->dns = array();
115         $disallowed = array();
116         foreach($ids as $id){
117           $dn = $this->licenses[$id]['dn'];
118           $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
119           if(preg_match("/d/",$acl)){
120             $this->dns[$id] = $dn;
121           }else{
122             $disallowed[] = $dn;
123           }
124         }
126         if(count($disallowed)){
127           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
128         }
130         if(count($this->dns)){
132           $dns_names = array();
133           foreach($this->dns as $dn){
134             $dns_names[] = LDAP::fix($dn);
135           }
137           $smarty->assign("info", msgPool::deleteInfo($dns_names,_("License")));
138           $smarty->assign("multiple", true);
139           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
140         }
141       }
142     } 
145     /* Remove lock */
146     if(isset($_POST['delete_multiple_licenses_cancel'])){
148       /* Remove lock file after successfull deletion */
149       $this->remove_lock();
150       $this->dns = array();
151     }
154     /* Confirmation for deletion has been passed. Users should be deleted. */
155     if (isset($_POST['delete_multiple_licenses_confirm'])){
157       /* Remove user by user and check acls before removeing them */
158       foreach($this->dns as $key => $dn){
160         $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
161         if (preg_match('/d/', $acl)){
163           echo "<br><b>Delete ".$dn."</b>";
165 #         /* Delete request is permitted, perform LDAP action */
166 #         $this->dialog= new licensetabs($this->config,$this->config->data['TABS']['LICENSETABS'], $dn);
167 #         $this->dialog->delete();
168 #         $this->dialog= NULL;
169         } else {
171           /* Normally this shouldn't be reached, send some extra
172              logs to notify the administrator */
173           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
174           new log("security","opsi/".get_class($this),$dn,array(),"Tried to trick deletion.");
175         }
176       }
178       /* Remove lock file after successfull deletion */
179       $this->remove_lock();
180       $this->dns = array();
181     }
184     /***************
185      * New handling
186      ***************/
188     if($s_action == "new" && !$this->dialog instanceOf tabs){
190       echo "<br><b>Delete ".$dn."</b>";
192  #    $this->dialog = new licensetabs($this->config, $this->config->data['TABS']['LICENSETABS'], "new");
193  #    $this->dialog->set_acl_base($this->config->current['BASE']);
194     }
195     
196     /***************
197      * Edit handling
198      ***************/
200     if($s_action == "edit" && !$this->dialog instanceOf tabs){
201       if(!isset($this->licenses[$s_entry])){
202         trigger_error("Unknown entry!"); 
203       }else{
205         $entry = $this->licenses[$s_entry];
206         $this->dn = $entry['dn'];
208         echo "<br><b>Edit ".$dn."</b>";
210 #       /* Open the dialog */
211 #       $this->dialog = new licensetabs($this->config, $this->config->data['TABS']['LICENSETABS'], 
212 #           $entry['dn'], "opsi");
213 #       $this->dialog->set_acl_base($this->config->current['BASE']);
214 #       set_object_info($this->dn);
215       }
216     }
219     /***************
220      * Dialog handling
221      ***************/
223     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && $this->dialog instanceOf tabs){
224       $this->dialog->save_object();
225       $msgs = $this->dialog->check();
226       if(count($msgs)){
227         msg_dialog::displayChecks($msgs);
228       }else{
229         $this->dialog->save();
230         if (!isset($_POST['edit_apply'])){
231           $this->remove_lock();
232           $this->dialog= NULL;
233           set_object_info();
234         }else{
235           $this->dialog->re_init();
236         }
237       }
238     }
240     if (isset($_POST['edit_cancel']) && $this->dialog instanceOf tabs){
241       $this->remove_lock();
242       $this->dialog= NULL;
243       set_object_info();
244     }
246     if($this->dialog instanceOf tabs){
247       $display= $this->dialog->execute();
249       $dialog_opened = ($this->dialog->by_object[$this->dialog->current]->dialog instanceOf plugin);
251       if(!$dialog_opened){
252         if($this->dialog->read_only   == TRUE){
253           $display.= "<p style=\"text-align:right\">
254             <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
255             </p>";
256         }else{
258           $display.= "<p style=\"text-align:right\">\n";
259           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" 
260             value=\"".msgPool::okButton(). "\">\n";
261           $display.= "&nbsp;\n";
262           if ($this->dn != "new"){
263             $display.= "<input type=submit name=\"edit_apply\" 
264               value=\"".msgPool::applyButton()."\">\n";
265             $display.= "&nbsp;\n";
266           }
267           $display.= "<input type=submit name=\"edit_cancel\" 
268             value=\"".msgPool::cancelButton()."\">\n";
269           $display.= "</p>";
270         }
271       }
272       return ($display);
273     }
276     /***************
277      * List handling
278      ***************/
280     // Display dialog with group list 
281     $this->DivListLicenses->parent = $this;
282     $this->DivListLicenses->execute();
283     $this->reload ();
284     $this->DivListLicenses->setEntries($this->licenses);
285     return($this->DivListLicenses->Draw());
286   }
289   // Refreshes the list of known license objects. 
290   function reload()
291   {
293     // Get current ldap base and filter settings.
294     $base     = $this->DivListLicenses->selectedBase;
295     $Regex    = $this->DivListLicenses->Regex;
297     $si = new opsiLicenceHandler($this->config);
298     $res = $si->listPools();
300     // Reset the list of licenses 
301     $this->licenses = array();
302     foreach($res as $item){
304       // Fake an ldap entry, this enables ACL checks.
305       $item['dn'] = "opsi:cn=".$item['cn'][0].",".$this->config->current['BASE'];
306       $this->licenses[] = $item;
307     }
308   }
311   /* \brief  Returns a list of selected entry ids.
312    *         E.g. remove multiple entries.
313    * @return Array  A list of entry IDs
314    */
315   function list_get_selected_items()
316   {
317     $ids = array();
318     foreach($_POST as $name => $value){
319       if(preg_match("/^item_selected_[0-9]*$/",$name)){
320         $id   = preg_replace("/^item_selected_/","",$name);
321         $ids[$id] = $id;
322       }
323     }
324     return($ids);
325   }
328   function remove_lock()
329   {
330     if (isset($this->dialog->dn)){
331       del_lock ($this->dialog->dn);
332     }elseif(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
333       del_lock($this->dn);
334     }
335     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
336       del_lock($this->dns);
337     }
338   }
340  
341   function save_object()
342   {
343     $this->DivListLicenses->save_object();
344   }
347 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
348 ?>