Wiki zdrojový kód pre UpdateLicenses
Naposledy upravil Admin-metais MetaIS 2024/10/30 11:04
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 | #set ($discard = $data.put('featureId', $collectiontool.set)) | ||
15 | ## | ||
16 | ## Collect the feature ids. | ||
17 | ## | ||
18 | #getVisiblePaidExtensions | ||
19 | #foreach ($visiblePaidExtension in $visiblePaidExtensions) | ||
20 | #set ($featureId = $visiblePaidExtension.id.id) | ||
21 | #set ($discard = $data.featureId.add($featureId)) | ||
22 | ## Pass the known expiration date in order to avoid retrieving licenses we already have. | ||
23 | #set ($license = $services.licensing.licensor.getLicenseForExtension($visiblePaidExtension.id)) | ||
24 | #if ($license) | ||
25 | #set ($discard = $data.put("expirationDate:$featureId", $license.expirationDate)) | ||
26 | #end | ||
27 | #end | ||
28 | ## | ||
29 | ## Retrieve license updates from the XWiki Store. | ||
30 | ## | ||
31 | #if ($data.featureId.size() > 0) | ||
32 | #set ($licensingConfigDoc = $xwiki.getDocument('Licenses.Code.LicensingConfig')) | ||
33 | #set ($storeUpdateURL = $licensingConfigDoc.getValue('storeUpdateURL')) | ||
34 | #set ($licensesJSON = $xwiki.getURLContent("$storeUpdateURL?$escapetool.url($data)")) | ||
35 | #set ($licenses = $jsontool.fromString($licensesJSON)) | ||
36 | #if (!$licenses) | ||
37 | ## Invalid JSON. | ||
38 | #set ($licenses = $jsontool.parse($licensesJSON)) | ||
39 | #end | ||
40 | #else | ||
41 | #set ($licenses = []) | ||
42 | #end | ||
43 | ## | ||
44 | ## Apply the updates. | ||
45 | ## | ||
46 | #if (!$licenses) | ||
47 | #set ($jsonResponse = { | ||
48 | 'status': 'error', | ||
49 | 'notification': $services.localization.render('licensor.updateLicenses.failedToRetrieve') | ||
50 | }) | ||
51 | #elseif ($licenses.isEmpty()) | ||
52 | #set ($jsonResponse = { | ||
53 | 'status': 'info', | ||
54 | 'notification': $services.localization.render('licensor.updateLicenses.noUpdates') | ||
55 | }) | ||
56 | #else | ||
57 | #set ($successCount = 0) | ||
58 | #foreach ($license in $licenses) | ||
59 | #set ($result = $NULL) | ||
60 | #try('addLicenseException') | ||
61 | #set ($result = $services.licensing.licensor.addLicense($license)) | ||
62 | #end | ||
63 | #if ($result) | ||
64 | #set ($successCount = $successCount + 1) | ||
65 | #end | ||
66 | #end | ||
67 | #set ($jsonResponse = { | ||
68 | 'status': 'done', | ||
69 | 'notification': $services.localization.render('licensor.updateLicenses.applied', [$successCount, $licenses.size()]), | ||
70 | 'updateCount': $successCount | ||
71 | }) | ||
72 | #if ($successCount == 0) | ||
73 | #set ($jsonResponse.status = 'error') | ||
74 | #elseif ($successCount < $licenses.size()) | ||
75 | #set ($jsonResponse.status = 'warning') | ||
76 | #end | ||
77 | #end | ||
78 | ## | ||
79 | ## Return the response. | ||
80 | ## | ||
81 | #if ($xcontext.action == 'get') | ||
82 | #set ($discard = $response.setContentType('application/json')) | ||
83 | #end | ||
84 | #if ($jsonResponse.status == 'error') | ||
85 | #set ($discard = $response.setStatus(500)) | ||
86 | #end | ||
87 | $jsontool.serialize($jsonResponse) | ||
88 | {{/velocity}} |