Code

Updated for icons
[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= "Software licences";
27   var $plDescription= "Software licenses";
28   var $plIcon= "plugins/opsi/images/plugin.png";
30   // The headpage list handler. 
31   var $DivListLicenses    = NULL;
33   // A list of currently visible licenses
34   var $licenses = array();
36   // A list of currently edited/removed/aso licenses.
37   var $dns = array();
39   // Permission modules to use.
40   var $acl_module   = array("opsi");  
42   // Construct and initialize the plugin 
43   function __construct (&$config, $dn= NULL)
44   {
45     // Include config object 
46     $this->config= &$config;
47     $this->ui= get_userinfo();
49     // Initialize the corresponding list class.
50     $this->DivListLicenses = new divListLicense($this->config,$this);
51   }
54   function execute()
55   {
56     // Call parent execute 
57     plugin::execute();
59     // Variables to restore after 'entry locked' warning was displayed 
60     session::set('LOCK_VARS_TO_USE',array('/^license_/','/^act/',
61           '/^id/','/^menu_action/','/^item/'));
63     $smarty     = get_smarty();
65     /***************
66      * Handle _POST/_GET variables
67      ***************/
68    
69     // Get entry related posts 
70     $s_action   = "";
71     $s_entry    = "";
72     foreach($_POST as $name => $value){
73       if(preg_match("/^license_edit_/",$name)){
74         $s_action = "edit";  
75         $s_entry = preg_replace("/^license_edit_([0-9]*)_.*$/","\\1",$name);
76         break;
77       }
78       if(preg_match("/^license_del_/",$name)){
79         $s_action = "remove";  
80         $s_entry = preg_replace("/^license_del_([0-9]*)_.*$/","\\1",$name);
81         break;
82       }
83     }
85     if(isset($_GET['act']) && $_GET['act'] == "edit_entry" && isset($_GET['id'])){
86       $id = $_GET['id'];
87       if(isset($this->licenses[$id])){
88         $s_action = "edit";
89         $s_entry = $id;
90       }
91     }
92  
93     // Get menu related posts 
94     if(isset($_POST['menu_action'])) {
95       if($_POST['menu_action'] == "remove_multiple_licenses"){
96         $s_action = "remove_multiple";
97       }
98       if($_POST['menu_action'] == "license_new"){
99         $s_action = "new";
100       }
101     }
105     /***************
106      * Remove handling
107      ***************/
109     if($s_action == "remove_multiple" || $s_action == "remove"){
110     
111       if($s_action == "remove_multiple"){
112         $ids = $this->list_get_selected_items();
113       }else{
114         $ids = array($s_entry);
115       }
117       if(count($ids)){
118         $this->dns = array();
119         $disallowed = array();
120         foreach($ids as $id){
121           $dn = $this->licenses[$id]['dn'];
122           $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
123           if(preg_match("/d/",$acl)){
124             $this->dns[$id] = $dn;
125           }else{
126             $disallowed[] = $dn;
127           }
128         }
130         if(count($disallowed)){
131           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
132         }
134         if(count($this->dns)){
136           $dns_names = array();
137           foreach($this->dns as $dn){
138             $dns_names[] = LDAP::fix($dn);
139           }
141           $smarty->assign("info", msgPool::deleteInfo($dns_names,_("License")));
142           $smarty->assign("multiple", true);
143           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
144         }
145       }
146     } 
149     /* Remove lock */
150     if(isset($_POST['delete_multiple_licenses_cancel'])){
152       /* Remove lock file after successfull deletion */
153       $this->remove_lock();
154       $this->dns = array();
155     }
158     /* Confirmation for deletion has been passed. Users should be deleted. */
159     if (isset($_POST['delete_multiple_licenses_confirm'])){
161       /* Remove user by user and check acls before removeing them */
162       foreach($this->dns as $key => $dn){
164         $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
165         if (preg_match('/d/', $acl)){
166           $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$dn, "opsi");
167           $this->dialog->set_acl_base($this->config->current['BASE']);
168           $this->dialog->delete();
169           $this->dialog= NULL;
170         } else {
172           /* Normally this shouldn't be reached, send some extra
173              logs to notify the administrator */
174           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
175           new log("security","opsi/".get_class($this),$dn,array(),"Tried to trick deletion.");
176         }
177       }
179       /* Remove lock file after successfull deletion */
180       $this->remove_lock();
181       $this->dns = array();
182     }
185     /***************
186      * New handling
187      ***************/
189     if($s_action == "new" && !$this->dialog instanceOf tabs){
190       $this->dn = "new";
191       $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$this->dn, "opsi");
192       $this->dialog->set_acl_base($this->config->current['BASE']);
193     }
194     
195     /***************
196      * Edit handling
197      ***************/
199     if($s_action == "edit" && !$this->dialog instanceOf tabs){
200       if(!isset($this->licenses[$s_entry])){
201         trigger_error("Unknown entry!"); 
202       }else{
204         $entry = $this->licenses[$s_entry];
205         $this->dn = $entry['dn'];
207         /* Open the dialog */
208         $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'], 
209             $entry['dn'], "opsi");
210         $this->dialog->set_acl_base($this->config->current['BASE']);
211         set_object_info($this->dn);
212       }
213     }
216     /***************
217      * Dialog handling
218      ***************/
220     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && $this->dialog instanceOf tabs){
221       $this->dialog->save_object();
222       $msgs = $this->dialog->check();
223       if(count($msgs)){
224         msg_dialog::displayChecks($msgs);
225       }else{
226         $this->dialog->save();
227         if (!isset($_POST['edit_apply'])){
228           $this->remove_lock();
229           $this->dialog= NULL;
230           set_object_info();
231         }else{
232           $this->dialog->re_init();
233         }
234       }
235     }
237     if (isset($_POST['edit_cancel']) && $this->dialog instanceOf tabs){
238       $this->remove_lock();
239       $this->dialog= NULL;
240       set_object_info();
241     }
243     if($this->dialog instanceOf tabs){
244       $display= $this->dialog->execute();
246       $dialog_opened = ($this->dialog->by_object[$this->dialog->current]->dialog instanceOf plugin);
248       if(!$dialog_opened){
249         if($this->dialog->read_only   == TRUE){
250           $display.= "<p style=\"text-align:right\">
251             <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
252             </p>";
253         }else{
255           $display.= "<p style=\"text-align:right\">\n";
256           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" 
257             value=\"".msgPool::okButton(). "\">\n";
258           $display.= "&nbsp;\n";
259           if ($this->dn != "new"){
260             $display.= "<input type=submit name=\"edit_apply\" 
261               value=\"".msgPool::applyButton()."\">\n";
262             $display.= "&nbsp;\n";
263           }
264           $display.= "<input type=submit name=\"edit_cancel\" 
265             value=\"".msgPool::cancelButton()."\">\n";
266           $display.= "</p>";
267         }
268       }
269       return ($display);
270     }
273     /***************
274      * List handling
275      ***************/
277     // Display dialog with group list 
278     $this->DivListLicenses->parent = $this;
279     $this->DivListLicenses->execute();
280     $this->reload ();
281     $this->DivListLicenses->setEntries($this->licenses);
282     return($this->DivListLicenses->Draw());
283   }
286   // Refreshes the list of known license objects. 
287   function reload()
288   {
290     // Get current ldap base and filter settings.
291     $base     = $this->DivListLicenses->selectedBase;
292     $Regex    = $this->DivListLicenses->Regex;
294     $si = new opsiLicenceHandler($this->config);
295     $this->licenses = array();
296     $res = $si->listPools();
297     if($si->is_error() || !$res){
298       $this->init_successfull = FALSE;
299       msg_dialog::display(_("Error"),msgPool::siError($si->get_error()),ERROR_DIALOG);
300       return;
301     }else{
303       // Reset the list of licenses 
304       foreach($res as $item){
306         // Fake an ldap entry, this enables ACL checks.
307         $item['dn'] = "opsi:cn=".$item['cn'][0].",".$this->config->current['BASE'];
308         $this->licenses[] = $item;
309       }
310     }
311   }
314   /* \brief  Returns a list of selected entry ids.
315    *         E.g. remove multiple entries.
316    * @return Array  A list of entry IDs
317    */
318   function list_get_selected_items()
319   {
320     $ids = array();
321     foreach($_POST as $name => $value){
322       if(preg_match("/^item_selected_[0-9]*$/",$name)){
323         $id   = preg_replace("/^item_selected_/","",$name);
324         $ids[$id] = $id;
325       }
326     }
327     return($ids);
328   }
331   function remove_lock()
332   {
333     if (isset($this->dialog->dn)){
334       del_lock ($this->dialog->dn);
335     }elseif(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
336       del_lock($this->dn);
337     }
338     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
339       del_lock($this->dns);
340     }
341   }
343  
344   function save_object()
345   {
346     $this->DivListLicenses->save_object();
347   }
350 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
351 ?>