Code

Added ACL handling to license managmenet
[gosa.git] / gosa-plugins / opsi / admin / opsiLicenses / class_licenseGeneric.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_opsiLicenses.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 */
24 class licenseGeneric extends plugin 
25 {
27   var $si = NULL;
28   var $data = array();
29   var $cn = "";
30   var $orig_cn = "";
31   var $description = "";  
32   var $partner = "";
34   var $conclusionDate = "";
35   var $expirationDate = "";
36   var $notificationDate = "";
38   var $licenseModel = "";
39   var $licenseKey = array();
40   var $orig_licenseModel = "";
41   var $licensePoolId = "";
42   var $boundToHost= array(); // Reserved for Host.   
43   var $usedByHost = array(); // Used by Host.   
45   var $maximumInstallations = 0;
46   var $opsiHosts; 
47   
48   var $attributes = array(
49         "cn","description","partner","conclusionDate","expirationDate",
50         "notificationDate","licenseModel","licenseKey","maximumInstallations",
51         "licensePoolId", "usedByHost","boundToHost");
53   function __construct(&$config, $dn, $license, $hosts = array())
54   {
55   
56     $this->config = $config;
57     $this->data = $license;
58     $this->dn = $dn;
59     $this->si = new opsiLicenceHandler($this->config);
60     $this->opsiHosts = $hosts;
62     // Detect account state.
63     if(count($this->data) == 0){
64       $this->initially_was_account = FALSE;
65     }else{
66       $this->initially_was_account = TRUE;
67     }
68     $this->is_account = TRUE;
70     // Extract pool name out of the fake dn.
71     $this->init();
72   }
74   
75   function init()
76   {
77     $this->boundToHost = array('0'=>"");
78     $this->usedByHost = array('0'=>"");
79     $this->licenseKey = array('0'=>"");
80     if($this->initially_was_account){
81       foreach($this->attributes as $attr){
82         $this->$attr = $this->data[$attr];
83       }
84     }
86     $this->orig_cn = $this->cn;
87     $this->orig_licenseModel = $this->licenseModel;
88     $this->init_successfull = TRUE;
89     return;    
90   }
93   function execute()
94   {
95     // Handle initialization failures.
96     if(isset($_POST['retry_init'])) $this->init();
97     if(!$this->init_successfull){
98       $smarty = get_smarty();
99       $smarty->assign("init_successfull", $this->init_successfull);
100       return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
101     }
103     $smarty = get_smarty();
105     // Assign ACls 
106     $plInfo = $this->plInfo();
107     foreach($plInfo['plProvidedAcls'] as $name => $desc){
108       $smarty->assign("{$name}ACL",$this->getacl($name));
109     }
110     foreach($this->attributes as $attr){
111       $smarty->assign($attr,$this->$attr);
112     }
114     $smarty->assign("licenseModels",array(
115           "RETAIL" => _("Retail"),
116           "OEM"=>_("OEM"),
117           "VOLUME" => _("Volume"),
118           "CONCURRENT" => _("Concurrent")));
121     $smarty->assign("init_successfull", $this->init_successfull);
122     $smarty->assign("initially_was_account", $this->initially_was_account);
123     $smarty->assign("hosts", $this->getHosts());
125     $ui = get_userinfo();
127     $acl_base = $this->dn;
128     if($acl_base == "new"){
129       $acl_base = $this->config->current['BASE'];
130     }
132     $smarty->assign("licenseACL", $ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses"));
133     $smarty->assign("writeable", preg_match("/w/",$ui->get_permissions($acl_base,"opsi/licensePoolGeneric","licenses")));
135     $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost));
136     $smarty->assign("boundToHost", $this->boundToHost[0]);
137     $smarty->assign("licenseKey", $this->licenseKey[0]);
138     return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
139   }
142   function getHosts()
143   {
144     $ret = array();
145     foreach($this->opsiHosts as $host){
146       $cn = $host['NAME'][0]['VALUE'];
147       $ret[$cn] = $cn;
148     }
149     return($ret);
150   }
153  
154   /* Save HTML inputs
155    */
156   function save_object()
157   {
159     if(isset($_POST['opsiLicensesPosted'])){
160       plugin::save_object();
162       if(isset($_POST['addLicenseUsage']) && isset($_POST['selectedHostToAdd'])){
163         $host = get_post('selectedHostToAdd');
164         if(!empty($host) && 
165             in_array($host,$this->getHosts()) && 
166             !in_array($host, $this->usedByHost)){
167           $this->usedByHost[] = $host;
168         }
169       }
171       if(isset($_POST['removeLicenseUsage']) && isset($_POST['selectedUsedHosts'])){
172         $todel = $_POST['selectedUsedHosts'];
173         foreach($todel as $host){
174           if(isset($this->usedByHost[$host])){
175             unset($this->usedByHost[$host]);
176           }
177         }
178       }
180       // Force licenseKey to be of type array.
181       if(!is_array($this->licenseKey)){
182         $this->licenseKey = array($this->licenseKey);
183       }
185       // BoundToHost maybe multiple too, later.
186       if(!is_array($this->boundToHost)){
187         $this->boundToHost = array($this->boundToHost);
188       }    
190       if($this->initially_was_account){
191         $this->cn = $this->orig_cn;
192         $this->licenseModel = $this->orig_licenseModel;
193       }
194     }
195   }  
198   /* Check user input and return a list of 'invalid input' messages.
199    */
200   function check()
201   {
202     $message = plugin::check();
204     // Very simple date input checks
205     if(!empty($this->expirationDate) && 
206        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->expirationDate)){
207       $message[] = msgPool::invalid(_("Expiration date"),$this->expirationDate,"","2009-02-23");
208     }
209     if(!empty($this->conclusionDate) && 
210        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->conclusionDate)){
211       $message[] = msgPool::invalid(_("Expiration date"),$this->conclusionDate,"","2009-02-23");
212     }
213     if(!empty($this->notificationDate) && 
214        !preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/",$this->notificationDate)){
215       $message[] = msgPool::invalid(_("Expiration date"),$this->notificationDate,"","2009-02-23");
216     }
218     if(empty($this->cn)){
219       $message[] = msgPool::required(_("Name"));
220     }
222     if(empty($this->licenseKey[0])){
223       $message[] = msgPool::required(_("License key"));
224     }
226     return($message);
227   }
228   
229  
230   /* Removes the object from the opsi database
231    */ 
232   function remove_from_parent() {}
235   /* Saves object modifications
236    */  
237   function save()
238   {
239     $data = array();
240     foreach($this->attributes as $target){
241       $data[$target] = $this->$target;
242     }
243     return($data);
244   }
245  
246   static function plInfo()
247   {
248     return (array(
249           "plShortName"   => _("Generic"),
250           "plDescription" => _("License generic"),
251           "plSelfModify"  => FALSE,
252           "plDepends"     => array(),
253           "plPriority"    => 1,
254           "plSection"     => array("administration"),
255           "plCategory"    => array("opsi"),
256           "plProvidedAcls"=> array(
257             "cn"                => _("Name"),
258             "description" => _("Description"))
259           ));
260   }
264 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
265 ?>