Code

Added license generic template
[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)){
162           $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$dn, "opsi");
163           $this->dialog->set_acl_base($this->config->current['BASE']);
164           $this->dialog->delete();
165           $this->dialog= NULL;
166         } else {
168           /* Normally this shouldn't be reached, send some extra
169              logs to notify the administrator */
170           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
171           new log("security","opsi/".get_class($this),$dn,array(),"Tried to trick deletion.");
172         }
173       }
175       /* Remove lock file after successfull deletion */
176       $this->remove_lock();
177       $this->dns = array();
178     }
181     /***************
182      * New handling
183      ***************/
185     if($s_action == "new" && !$this->dialog instanceOf tabs){
186       $this->dn = "new";
187       $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$this->dn, "opsi");
188       $this->dialog->set_acl_base($this->config->current['BASE']);
189     }
190     
191     /***************
192      * Edit handling
193      ***************/
195     if($s_action == "edit" && !$this->dialog instanceOf tabs){
196       if(!isset($this->licenses[$s_entry])){
197         trigger_error("Unknown entry!"); 
198       }else{
200         $entry = $this->licenses[$s_entry];
201         $this->dn = $entry['dn'];
203         /* Open the dialog */
204         $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'], 
205             $entry['dn'], "opsi");
206         $this->dialog->set_acl_base($this->config->current['BASE']);
207         set_object_info($this->dn);
208       }
209     }
212     /***************
213      * Dialog handling
214      ***************/
216     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && $this->dialog instanceOf tabs){
217       $this->dialog->save_object();
218       $msgs = $this->dialog->check();
219       if(count($msgs)){
220         msg_dialog::displayChecks($msgs);
221       }else{
222         $this->dialog->save();
223         if (!isset($_POST['edit_apply'])){
224           $this->remove_lock();
225           $this->dialog= NULL;
226           set_object_info();
227         }else{
228           $this->dialog->re_init();
229         }
230       }
231     }
233     if (isset($_POST['edit_cancel']) && $this->dialog instanceOf tabs){
234       $this->remove_lock();
235       $this->dialog= NULL;
236       set_object_info();
237     }
239     if($this->dialog instanceOf tabs){
240       $display= $this->dialog->execute();
242       $dialog_opened = ($this->dialog->by_object[$this->dialog->current]->dialog instanceOf plugin);
244       if(!$dialog_opened){
245         if($this->dialog->read_only   == TRUE){
246           $display.= "<p style=\"text-align:right\">
247             <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
248             </p>";
249         }else{
251           $display.= "<p style=\"text-align:right\">\n";
252           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" 
253             value=\"".msgPool::okButton(). "\">\n";
254           $display.= "&nbsp;\n";
255           if ($this->dn != "new"){
256             $display.= "<input type=submit name=\"edit_apply\" 
257               value=\"".msgPool::applyButton()."\">\n";
258             $display.= "&nbsp;\n";
259           }
260           $display.= "<input type=submit name=\"edit_cancel\" 
261             value=\"".msgPool::cancelButton()."\">\n";
262           $display.= "</p>";
263         }
264       }
265       return ($display);
266     }
269     /***************
270      * List handling
271      ***************/
273     // Display dialog with group list 
274     $this->DivListLicenses->parent = $this;
275     $this->DivListLicenses->execute();
276     $this->reload ();
277     $this->DivListLicenses->setEntries($this->licenses);
278     return($this->DivListLicenses->Draw());
279   }
282   // Refreshes the list of known license objects. 
283   function reload()
284   {
286     // Get current ldap base and filter settings.
287     $base     = $this->DivListLicenses->selectedBase;
288     $Regex    = $this->DivListLicenses->Regex;
290     $si = new opsiLicenceHandler($this->config);
291     $res = $si->listPools();
293     // Reset the list of licenses 
294     $this->licenses = array();
295     foreach($res as $item){
297       // Fake an ldap entry, this enables ACL checks.
298       $item['dn'] = "opsi:cn=".$item['cn'][0].",".$this->config->current['BASE'];
299       $this->licenses[] = $item;
300     }
301   }
304   /* \brief  Returns a list of selected entry ids.
305    *         E.g. remove multiple entries.
306    * @return Array  A list of entry IDs
307    */
308   function list_get_selected_items()
309   {
310     $ids = array();
311     foreach($_POST as $name => $value){
312       if(preg_match("/^item_selected_[0-9]*$/",$name)){
313         $id   = preg_replace("/^item_selected_/","",$name);
314         $ids[$id] = $id;
315       }
316     }
317     return($ids);
318   }
321   function remove_lock()
322   {
323     if (isset($this->dialog->dn)){
324       del_lock ($this->dialog->dn);
325     }elseif(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
326       del_lock($this->dn);
327     }
328     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
329       del_lock($this->dns);
330     }
331   }
333  
334   function save_object()
335   {
336     $this->DivListLicenses->save_object();
337   }
340 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
341 ?>