Wiki源代码UpdateLicenses
由 leeb chi 于 2020/09/02, 17:18 最后修改
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{include reference="Licenses.Code.VelocityMacros" /}} | ||
| 2 | |||
| 3 | {{velocity wiki="false"}} | ||
| 4 | #if ($request.confirm != 'true') | ||
| 5 | #stop | ||
| 6 | #elseif (!$services.csrf.isTokenValid($request.form_token)) | ||
| 7 | #set ($discard = $response.sendError(401, $services.localization.render('licensor.badCSRFToken'))) | ||
| 8 | #stop | ||
| 9 | #end | ||
| 10 | #set ($data = { | ||
| 11 | 'outputSyntax': 'plain', | ||
| 12 | 'instanceId': "$services.instance.instanceId" | ||
| 13 | }) | ||
| 14 | ## CollectionsTool is deprecated since 12.1RC1. | ||
| 15 | #if ($collectiontool) | ||
| 16 | #set ($discard = $data.put('featureId', $collectiontool.set)) | ||
| 17 | #else | ||
| 18 | #set ($discard = $data.put('featureId', $collectionstool.set)) | ||
| 19 | #end | ||
| 20 | ## | ||
| 21 | ## Collect the feature ids. | ||
| 22 | ## | ||
| 23 | #getVisiblePaidExtensions | ||
| 24 | #foreach ($visiblePaidExtension in $visiblePaidExtensions) | ||
| 25 | #set ($featureId = $visiblePaidExtension.id.id) | ||
| 26 | #set ($discard = $data.featureId.add($featureId)) | ||
| 27 | ## Pass the known expiration date in order to avoid retrieving licenses we already have. | ||
| 28 | #set ($license = $services.licensing.licensor.getLicenseForExtension($visiblePaidExtension.id)) | ||
| 29 | #if ($license) | ||
| 30 | #set ($discard = $data.put("expirationDate:$featureId", $license.expirationDate)) | ||
| 31 | #end | ||
| 32 | #end | ||
| 33 | ## | ||
| 34 | ## Retrieve license updates from the XWiki Store. | ||
| 35 | ## | ||
| 36 | #if ($data.featureId.size() > 0) | ||
| 37 | #set ($licensingConfigDoc = $xwiki.getDocument('Licenses.Code.LicensingConfig')) | ||
| 38 | #set ($storeUpdateURL = $licensingConfigDoc.getValue('storeUpdateURL')) | ||
| 39 | #set ($licensesJSON = $xwiki.getURLContent("$storeUpdateURL?$escapetool.url($data)")) | ||
| 40 | #set ($licenses = $jsontool.fromString($licensesJSON)) | ||
| 41 | #if (!$licenses) | ||
| 42 | ## Invalid JSON. | ||
| 43 | #set ($licenses = $jsontool.parse($licensesJSON)) | ||
| 44 | #end | ||
| 45 | #else | ||
| 46 | #set ($licenses = []) | ||
| 47 | #end | ||
| 48 | ## | ||
| 49 | ## Apply the updates. | ||
| 50 | ## | ||
| 51 | #if (!$licenses) | ||
| 52 | #set ($jsonResponse = { | ||
| 53 | 'status': 'error', | ||
| 54 | 'notification': $services.localization.render('licensor.updateLicenses.failedToRetrieve') | ||
| 55 | }) | ||
| 56 | #elseif ($licenses.isEmpty()) | ||
| 57 | #set ($jsonResponse = { | ||
| 58 | 'status': 'info', | ||
| 59 | 'notification': $services.localization.render('licensor.updateLicenses.noUpdates') | ||
| 60 | }) | ||
| 61 | #else | ||
| 62 | #set ($successCount = 0) | ||
| 63 | #foreach ($license in $licenses) | ||
| 64 | #set ($result = $NULL) | ||
| 65 | #try('addLicenseException') | ||
| 66 | #set ($result = $services.licensing.licensor.addLicense($license)) | ||
| 67 | #end | ||
| 68 | #if ($result) | ||
| 69 | #set ($successCount = $successCount + 1) | ||
| 70 | #end | ||
| 71 | #end | ||
| 72 | #set ($jsonResponse = { | ||
| 73 | 'status': 'done', | ||
| 74 | 'notification': $services.localization.render('licensor.updateLicenses.applied', [$successCount, $licenses.size()]), | ||
| 75 | 'updateCount': $successCount | ||
| 76 | }) | ||
| 77 | #if ($successCount == 0) | ||
| 78 | #set ($jsonResponse.status = 'error') | ||
| 79 | #elseif ($successCount < $licenses.size()) | ||
| 80 | #set ($jsonResponse.status = 'warning') | ||
| 81 | #end | ||
| 82 | #end | ||
| 83 | ## | ||
| 84 | ## Return the response. | ||
| 85 | ## | ||
| 86 | #if ($xcontext.action == 'get') | ||
| 87 | #set ($discard = $response.setContentType('application/json')) | ||
| 88 | #end | ||
| 89 | #if ($jsonResponse.status == 'error') | ||
| 90 | #set ($discard = $response.setStatus(500)) | ||
| 91 | #end | ||
| 92 | $jsontool.serialize($jsonResponse) | ||
| 93 | {{/velocity}} |