From: hickert Date: Fri, 25 Sep 2009 09:37:02 +0000 (+0000) Subject: Updated license handling. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bedeeb2697aae091b9d7691f7fab8f256231ad30;p=gosa.git Updated license handling. git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14346 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-plugins/opsi/admin/opsiLicenses/class_licenseGeneric.inc b/gosa-plugins/opsi/admin/opsiLicenses/class_licenseGeneric.inc index bba0c8ea3..0443d7f38 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/class_licenseGeneric.inc +++ b/gosa-plugins/opsi/admin/opsiLicenses/class_licenseGeneric.inc @@ -27,6 +27,7 @@ class licenseGeneric extends plugin var $si = NULL; var $data = array(); var $cn = ""; + var $orig_cn = ""; var $description = ""; var $partner = ""; @@ -35,7 +36,7 @@ class licenseGeneric extends plugin var $notificationDate = ""; var $licenseModel = ""; - var $licenseKey = ""; + var $licenseKey = array(); var $licensePoolId = ""; var $boundToHost= array(); // Reserved for Host. var $usedByHost = array(); // Used by Host. @@ -43,8 +44,10 @@ class licenseGeneric extends plugin var $maximumInstallations = 0; var $opsiHosts; - var $attributes = array("cn","description","partner","conclusionDate","expirationDate", - "notificationDate","licenseModel","licenseKey","maximumInstallations","licensePoolId"); + var $attributes = array( + "cn","description","partner","conclusionDate","expirationDate", + "notificationDate","licenseModel","licenseKey","maximumInstallations", + "licensePoolId", "usedByHost","boundToHost"); function __construct(&$config, $license, $hosts = array()) { @@ -68,35 +71,16 @@ class licenseGeneric extends plugin function init() { - - $map = array( - "BOUNDTOHOST"=> "boundToHost", - "EXPIRATIONDATE"=> "expirationDate", - "LICENSECONTRACTID"=> "contractId", - "LICENSEPOOIDS"=> "licensePoolId", - "LICENSETYPE"=> "licenseModel", - "MAXINSTALLATIONS"=> "maximumInstallations", - "SOFTWARELICENSEID"=> "cn"); - - foreach($map as $source => $target){ - if(isset($this->data[$source][0]['VALUE'])){ - $this->$target = $this->data[$source][0]['VALUE']; - } - } - - // Get license key - if(isset($this->data['LICENSEKEYS'][0][strtoupper($this->licensePoolId)][0]['VALUE'])){ - $this->licenseKey = $this->data['LICENSEKEYS'][0][strtoupper($this->licensePoolId)][0]['VALUE']; - } - - // Get license usage - $this->usedByHost = array(); - if(isset($this->data['HOSTIDS'])){ - foreach($this->data['HOSTIDS'] as $host){ - $this->usedByHost[] = $host['VALUE']; + $this->boundToHost = array('0'=>""); + $this->usedByHost = array('0'=>""); + $this->licenseKey = array('0'=>""); + if($this->initially_was_account){ + foreach($this->attributes as $attr){ + $this->$attr = $this->data[$attr]; } } + $this->orig_cn = $this->cn; $this->init_successfull = TRUE; return; } @@ -104,8 +88,6 @@ class licenseGeneric extends plugin function execute() { - $this->init(); - // Handle initialization failures. if(isset($_POST['retry_init'])) $this->init(); if(!$this->init_successfull){ @@ -137,8 +119,8 @@ class licenseGeneric extends plugin $smarty->assign("hosts", $this->getHosts()); $smarty->assign("notUsedHosts", array_diff($this->getHosts(), $this->usedByHost)); - $smarty->assign("usedByHost", $this->usedByHost); - $smarty->assign("boundToHost", $this->boundToHost); + $smarty->assign("boundToHost", $this->boundToHost[0]); + $smarty->assign("licenseKey", $this->licenseKey[0]); return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__)))); } @@ -159,8 +141,19 @@ class licenseGeneric extends plugin */ function save_object() { + if(isset($_POST['opsiLicensesPosted'])){ - plugin::save_object(); + plugin::save_object(); + + // Force licenseKey to be of type array. + $this->licenseKey = array($this->licenseKey); + + // BoundToHost maybe multiple too, later. + $this->boundToHost = array($this->boundToHost); + + if($this->initially_was_account){ + $this->cn = $this->orig_cn; + } } } @@ -197,31 +190,11 @@ class licenseGeneric extends plugin */ function save() { - - echo "missing save"; -# plugin::save(); -# -# // Send modify/add events -# $mode = "modify"; -# if($this->orig_dn == "new"){ -# $mode = "add"; -# } -# -# $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);# -# if($this->si->is_error()){ -# msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG); -# }else{ -# $this->handle_post_events($mode); -# } -# -# // Log action -# if($mode == "modify"){ -# new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error()); -# }else{ -# new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error()); -# } -# -# return 0; + $data = array(); + foreach($this->attributes as $target){ + $data[$target] = $this->$target; + } + return($data); } static function plInfo() diff --git a/gosa-plugins/opsi/admin/opsiLicenses/class_licensePoolGeneric.inc b/gosa-plugins/opsi/admin/opsiLicenses/class_licensePoolGeneric.inc index 5f017604d..741ffcb08 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/class_licensePoolGeneric.inc +++ b/gosa-plugins/opsi/admin/opsiLicenses/class_licensePoolGeneric.inc @@ -34,6 +34,7 @@ class licensePoolGeneric extends plugin var $productIds = array(); var $softwareIds= array(); var $licenses = array(); + var $orig_licenses = array(); var $availableProductIds = array(); var $attributes =array("cn","description"); @@ -41,6 +42,24 @@ class licensePoolGeneric extends plugin var $opsiHosts = array(); + var $licenseMap = array( + "BOUNDTOHOST"=> "boundToHost", + "HOSTIDS"=> "usedByHost", + "LICENSEPOOLIDS"=> "licensePoolId", + "LICENSETYPE"=> "licenseModel", + "LICENSEKEY"=> "licenseKey", + "MAXINSTALLATIONS"=> "maximumInstallations", + "EXPIRATIONDATE"=> "expirationDate", + "SOFTWARELICENSEID"=> "cn" + ); + + var $licenseContractMap = array( + "CONCLUSIONDATE"=> "conclusionDate", + "NOTIFICATIONDATE"=> "notificationDate", + "NOTES"=> "description", + "PARTNER"=> "partner" + ); + function __construct(&$config,$dn) { $this->config = $config; @@ -113,9 +132,59 @@ class licensePoolGeneric extends plugin // Load Licences $this->licenses = array(); if(isset($this->data['licenses'])){ - $this->licenses = $this->data['licenses']; - unset($this->licenses['count']); + for($i =0; $i< $this->data['licenses']['count']; $i++){ + $license = $this->data['licenses'][$i]; + + // Do not parse invalid licenses + if(!is_array($license) || !isset($license['LICENSEPOOLIDS'])){ + echo "Invalid license"; + continue; + } + + // Prepare Software License Key(s) + $upper = strtoupper($license['LICENSEPOOLIDS'][0]['VALUE']); + $license['LICENSEKEYS'] = $license['LICENSEKEYS'][0][$upper]; + + $entry = array(); + foreach($this->licenseMap as $source => $target){ + $entry[$target] = ""; + if(isset($license[$source])){ + if(count($license[$source]) >= 2){ + $entry[$target] = array(); + foreach($license[$source] as $sub){ + $entry[$target][] = $sub['VALUE']; + } + }elseif(isset($license[$source][0]['VALUE'])){ + $entry[$target] = $license[$source][0]['VALUE']; + } + } + } + + $lData= $license['LICENSECONTRACTDATA'][0]; + foreach($this->licenseContractMap as $source => $target){ + if(isset($lData[$source])){ + if(count($lData[$source]) >= 2){ + $entry[$target] = array(); + foreach($lData[$source] as $sub){ + $entry[$target][] = $sub['VALUE']; + } + }elseif(isset($lData[$source][0]['VALUE'])){ + $entry[$target] = $lData[$source][0]['VALUE']; + } + } + } + + // There are some multi value attributes - force them to be of type array. + foreach(array("boundToHost","usedByHost","licenseKey") as $attr){ + if(!is_array($entry[$attr])){ + $entry[$attr] = array($entry[$attr]); + } + } + $this->licenses[$entry['cn']] = $entry; + } } + + $this->orig_licenses = $this->licenses; $this->init_successfull = TRUE; return; } @@ -172,20 +241,14 @@ class licensePoolGeneric extends plugin $list = new divSelectBox("test"); $list->setHeight(100); foreach($this->licenses as $i => $license){ - $keys = ""; - foreach($license['LICENSEKEYS'] as $key_by_pool){ - foreach($key_by_pool as $key){ - $keys .= $key[0]['VALUE'].", "; - } - } - + $keys = implode($license['licenseKey'],", "); $link = ""; $link.= ""; - $f1 = array("string" => $license['SOFTWARELICENSEID'][0]['VALUE'], "attach" => ""); - $f2 = array("string" => $license['LICENSETYPE'][0]['VALUE'], "attach" => ""); - $f3 = array("string" => $license['EXPIRATIONDATE'][0]['VALUE'], "attach" => ""); - $f4 = array("string" => $license['MAXINSTALLATIONS'][0]['VALUE'], "attach" => ""); + $f1 = array("string" => $license['cn']); + $f2 = array("string" => $license['licenseModel']); + $f3 = array("string" => $license['expirationDate']); + $f4 = array("string" => $license['maximumInstallations']); $f5 = array("string" => rtrim($keys,", ")); $f6 = array("string" => $link, "attach" => "style='border-right: 0px; width:32px;'"); @@ -207,8 +270,16 @@ class licensePoolGeneric extends plugin // Save license modifications if($this->dialog instanceOf plugin && isset($_POST['license_finish'])){ - echo "Save"; - $this->dialog = NULL; + $this->dialog->save_object(); + + $msgs = $this->dialog->check(); + if(count($msgs)){ + msg_dialog::displayChecks($msgs); + }else{ + $attrs = $this->dialog->save(); + $this->licenses[$attrs['cn']] = $attrs; + $this->dialog = NULL; + } return; } @@ -252,17 +323,24 @@ class licensePoolGeneric extends plugin } } + // We've to create a new license + if(isset($_POST['addLicense'])){ + $this->dialog = new licenseGeneric($this->config,array(), $this->opsiHosts); + $this->dialog->set_acl_base($this->config->current['BASE']); + } + // Search post for image button clicks. foreach($_POST as $name => $value){ if(preg_match("/^editLicense_/",$name)){ - $id = preg_replace("/^editLicense_([^_]*)_.*$/","\\1",$name); + $id = preg_replace("/^editLicense_(.*)_.$/","\\1",$name); if(isset($this->licenses[$id])){ $this->dialog = new licenseGeneric($this->config,$this->licenses[$id], $this->opsiHosts); + $this->dialog->set_acl_base($this->config->current['BASE']); } break; } if(preg_match("/^removeLicense_/",$name)){ - $id = preg_replace("/^removeLicense_([^_]*)_.*$/","\\1",$name); + $id = preg_replace("/^removeLicense_(.*)_.$/","\\1",$name); if(isset($this->licenses[$id])){ unset($this->licenses[$id]); } @@ -312,6 +390,7 @@ class licensePoolGeneric extends plugin $mode = "add"; } + // Create or update the pool $this->si->createPool($this->cn, $this->description,$this->productIds,$this->softwareIds);# if($this->si->is_error()){ msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG); @@ -319,6 +398,38 @@ class licensePoolGeneric extends plugin $this->handle_post_events($mode); } + // Create, remove or update licenses + $add = array_diff_assoc($this->licenses,$this->orig_licenses); + $del = array_diff_assoc($this->orig_licenses,$this->licenses); + + // Remove licenses + foreach($del as $license){ + $this->si->removeLicenseFromPool($this->cn, $license['cn']); + if($this->si->is_error()){ + msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG); + } + } + + // Add / update licenses + foreach($add as $license){ + $this->si->createLicense( + $this->cn, // Pool id + $license['cn'], + $license['licenseKey'][0], + $license['licenseModel'], + $license['partner'], + $license['conclusionDate'], + $license['notificationDate'], + $license['description'], + $license['cn'], + $license['maximumInstallations'], + $license['boundToHost'], + $license['expirationDate']); + if($this->si->is_error()){ + msg_dialog::display(_("Error"),msgPool::siError($this->si->get_error()),ERROR_DIALOG); + } + } + // Log action if($mode == "modify"){ new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$this->si->get_error()); diff --git a/gosa-plugins/opsi/admin/opsiLicenses/class_licenseUsage.inc b/gosa-plugins/opsi/admin/opsiLicenses/class_licenseUsage.inc index c4fde8e69..fda4c732c 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/class_licenseUsage.inc +++ b/gosa-plugins/opsi/admin/opsiLicenses/class_licenseUsage.inc @@ -44,7 +44,7 @@ class licenseUsage extends plugin } // Extract pool name out of the fake dn. - $this->init(); +# $this->init(); } diff --git a/gosa-plugins/opsi/admin/opsiLicenses/class_opsiLicenseHandler.inc b/gosa-plugins/opsi/admin/opsiLicenses/class_opsiLicenseHandler.inc index 7082a1093..2a6f6048d 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/class_opsiLicenseHandler.inc +++ b/gosa-plugins/opsi/admin/opsiLicenses/class_opsiLicenseHandler.inc @@ -164,7 +164,7 @@ class opsiLicenceHandler extends opsi { $notes = "", $softwareLicenseId = "", $maxInstallations = "", - $boudToHost = "", + $boundToHost = "", $expirationDate = "") { @@ -175,12 +175,14 @@ class opsiLicenceHandler extends opsi { // Append optional attributes foreach(array("partner","conclusionDate","notificationDate","notes","softwareLicenseId", - "licenseTyp","maxInstallations","boudToHost","expirationDate") as $attr){ + "licenseTyp","maxInstallations","boundToHost","expirationDate") as $attr){ if(!empty($$attr)){ $data[$attr] = $$attr; } } + print_a($data); + $res = $this->send_data("gosa_opsi_createLicense",$this->target,$data,TRUE); if(isset($res['XML'][0]['ANSWER_OPSI_GETSOFTWARELICENSE_HASH'])){ return(TRUE); @@ -284,6 +286,25 @@ class opsiLicenceHandler extends opsi { return(FALSE); } + /* + * @brief Removes a single license from a license pool + * Attention, the software license has to exists + * otherwise it will lead to an Opsi internal server error. + * @param softwareLicenseId + * @param licensePoolId + */ + function removeLicenseFromPool($licensePoolId,$softwareLicenseId) + { + $data= array(); + $data['licensePoolId'] = htmlentities($licensePoolId); + $data['softwareLicenseId'] = htmlentities($softwareLicenseId); + $res = $this->send_data("gosa_opsi_removeLicense",$this->target,$data,TRUE); + if(isset($res['XML'][0]['ANSWER_OPSI_REMOVELICENSE'])){ + return(TRUE); + } + return(FALSE); + } + /* * @brief Unassign all software licenses from a host diff --git a/gosa-plugins/opsi/admin/opsiLicenses/licenseGeneric.tpl b/gosa-plugins/opsi/admin/opsiLicenses/licenseGeneric.tpl index 23fa57373..c375d2b24 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/licenseGeneric.tpl +++ b/gosa-plugins/opsi/admin/opsiLicenses/licenseGeneric.tpl @@ -19,7 +19,11 @@ {t}Name{/t} - + {if $initially_was_account} + + {else} + + {/if} diff --git a/gosa-plugins/opsi/admin/opsiLicenses/licensePoolGeneric.tpl b/gosa-plugins/opsi/admin/opsiLicenses/licensePoolGeneric.tpl index 3755e82f6..e67729f74 100644 --- a/gosa-plugins/opsi/admin/opsiLicenses/licensePoolGeneric.tpl +++ b/gosa-plugins/opsi/admin/opsiLicenses/licensePoolGeneric.tpl @@ -44,6 +44,7 @@ {$licenses} +