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 ***************/
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 }
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 if($_POST['menu_action'] == "license_new"){
98 $s_action = "new";
99 }
100 }
104 /***************
105 * Remove handling
106 ***************/
108 if($s_action == "remove_multiple" || $s_action == "remove"){
110 if($s_action == "remove_multiple"){
111 $ids = $this->list_get_selected_items();
112 }else{
113 $ids = array($s_entry);
114 }
116 if(count($ids)){
117 $this->dns = array();
118 $disallowed = array();
119 foreach($ids as $id){
120 $dn = $this->licenses[$id]['dn'];
121 $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
122 if(preg_match("/d/",$acl)){
123 $this->dns[$id] = $dn;
124 }else{
125 $disallowed[] = $dn;
126 }
127 }
129 if(count($disallowed)){
130 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
131 }
133 if(count($this->dns)){
135 $dns_names = array();
136 foreach($this->dns as $dn){
137 $dns_names[] = LDAP::fix($dn);
138 }
140 $smarty->assign("info", msgPool::deleteInfo($dns_names,_("License")));
141 $smarty->assign("multiple", true);
142 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
143 }
144 }
145 }
148 /* Remove lock */
149 if(isset($_POST['delete_multiple_licenses_cancel'])){
151 /* Remove lock file after successfull deletion */
152 $this->remove_lock();
153 $this->dns = array();
154 }
157 /* Confirmation for deletion has been passed. Users should be deleted. */
158 if (isset($_POST['delete_multiple_licenses_confirm'])){
160 /* Remove user by user and check acls before removeing them */
161 foreach($this->dns as $key => $dn){
163 $acl = $this->ui->get_permissions($dn, "opsi/licenseGeneric");
164 if (preg_match('/d/', $acl)){
165 $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$dn, "opsi");
166 $this->dialog->set_acl_base($this->config->current['BASE']);
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){
189 $this->dn = "new";
190 $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],$this->dn, "opsi");
191 $this->dialog->set_acl_base($this->config->current['BASE']);
192 }
194 /***************
195 * Edit handling
196 ***************/
198 if($s_action == "edit" && !$this->dialog instanceOf tabs){
199 if(!isset($this->licenses[$s_entry])){
200 trigger_error("Unknown entry!");
201 }else{
203 $entry = $this->licenses[$s_entry];
204 $this->dn = $entry['dn'];
206 /* Open the dialog */
207 $this->dialog = new licenseTabs($this->config, $this->config->data['TABS']['OPSILICENSETABS'],
208 $entry['dn'], "opsi");
209 $this->dialog->set_acl_base($this->config->current['BASE']);
210 set_object_info($this->dn);
211 }
212 }
215 /***************
216 * Dialog handling
217 ***************/
219 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && $this->dialog instanceOf tabs){
220 $this->dialog->save_object();
221 $msgs = $this->dialog->check();
222 if(count($msgs)){
223 msg_dialog::displayChecks($msgs);
224 }else{
225 $this->dialog->save();
226 if (!isset($_POST['edit_apply'])){
227 $this->remove_lock();
228 $this->dialog= NULL;
229 set_object_info();
230 }else{
231 $this->dialog->re_init();
232 }
233 }
234 }
236 if (isset($_POST['edit_cancel']) && $this->dialog instanceOf tabs){
237 $this->remove_lock();
238 $this->dialog= NULL;
239 set_object_info();
240 }
242 if($this->dialog instanceOf tabs){
243 $display= $this->dialog->execute();
245 $dialog_opened = ($this->dialog->by_object[$this->dialog->current]->dialog instanceOf plugin);
247 if(!$dialog_opened){
248 if($this->dialog->read_only == TRUE){
249 $display.= "<p style=\"text-align:right\">
250 <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
251 </p>";
252 }else{
254 $display.= "<p style=\"text-align:right\">\n";
255 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\"
256 value=\"".msgPool::okButton(). "\">\n";
257 $display.= " \n";
258 if ($this->dn != "new"){
259 $display.= "<input type=submit name=\"edit_apply\"
260 value=\"".msgPool::applyButton()."\">\n";
261 $display.= " \n";
262 }
263 $display.= "<input type=submit name=\"edit_cancel\"
264 value=\"".msgPool::cancelButton()."\">\n";
265 $display.= "</p>";
266 }
267 }
268 return ($display);
269 }
272 /***************
273 * List handling
274 ***************/
276 // Display dialog with group list
277 $this->DivListLicenses->parent = $this;
278 $this->DivListLicenses->execute();
279 $this->reload ();
280 $this->DivListLicenses->setEntries($this->licenses);
281 return($this->DivListLicenses->Draw());
282 }
285 // Refreshes the list of known license objects.
286 function reload()
287 {
289 // Get current ldap base and filter settings.
290 $base = $this->DivListLicenses->selectedBase;
291 $Regex = $this->DivListLicenses->Regex;
293 $si = new opsiLicenceHandler($this->config);
294 $res = $si->listPools();
296 // Reset the list of licenses
297 $this->licenses = array();
298 foreach($res as $item){
300 // Fake an ldap entry, this enables ACL checks.
301 $item['dn'] = "opsi:cn=".$item['cn'][0].",".$this->config->current['BASE'];
302 $this->licenses[] = $item;
303 }
304 }
307 /* \brief Returns a list of selected entry ids.
308 * E.g. remove multiple entries.
309 * @return Array A list of entry IDs
310 */
311 function list_get_selected_items()
312 {
313 $ids = array();
314 foreach($_POST as $name => $value){
315 if(preg_match("/^item_selected_[0-9]*$/",$name)){
316 $id = preg_replace("/^item_selected_/","",$name);
317 $ids[$id] = $id;
318 }
319 }
320 return($ids);
321 }
324 function remove_lock()
325 {
326 if (isset($this->dialog->dn)){
327 del_lock ($this->dialog->dn);
328 }elseif(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
329 del_lock($this->dn);
330 }
331 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
332 del_lock($this->dns);
333 }
334 }
337 function save_object()
338 {
339 $this->DivListLicenses->save_object();
340 }
341 }
343 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
344 ?>