Wiki-Quellcode von PDFViewerService
Zuletzt geändert von Team green-solutions am 2025/05/19 11:42
Verstecke letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
![]() |
1.1 | 1 | {{groovy}} |
| 2 | import org.xwiki.security.authorization.Right | ||
| 3 | import javax.servlet.ServletOutputStream | ||
| 4 | import org.apache.commons.io.IOUtils; | ||
| 5 | import org.xwiki.crypto.DigestFactory; | ||
| 6 | import org.xwiki.crypto.BinaryStringEncoder; | ||
| 7 | |||
| 8 | def reference = request.get('reference') | ||
| 9 | def filename = request.get('filename') | ||
| 10 | def user = request.get('user') | ||
| 11 | def key = request.get('key') | ||
| 12 | |||
| 13 | if (!reference || !filename || !user || !key) | ||
| 14 | return; | ||
| 15 | |||
| 16 | def userRef = services.model.resolveDocument(user) | ||
| 17 | def pdfDocRef = services.model.resolveDocument(reference) | ||
| 18 | |||
| 19 | if(services.security.authorization.hasAccess(Right.VIEW, userRef, pdfDocRef)) { | ||
| 20 | // Changing the context user | ||
| 21 | def currentUserRef = xcontext.context.getUserReference() | ||
| 22 | xcontext.context.setUserReference(userRef) | ||
| 23 | try { | ||
| 24 | // Getting the document | ||
| 25 | def pdfXDoc = xwiki.getDocument(pdfDocRef) | ||
| 26 | def attachment = pdfXDoc.getAttachment(filename) | ||
| 27 | def digestFactory = services.component.getInstance(DigestFactory.class, 'SHA-1') | ||
| 28 | def encoder = services.component.getInstance(BinaryStringEncoder.class, 'URLBase64') | ||
| 29 | def digest = digestFactory.instance | ||
| 30 | def is = digest.getInputStream(attachment.contentInputStream) | ||
| 31 | is.skip(1000) | ||
| 32 | is.close() | ||
| 33 | if (currentUserRef) { | ||
| 34 | digest.update(services.model.serialize(currentUserRef, 'default').getBytes('UTF-8')) | ||
| 35 | } | ||
| 36 | digest.update(services.model.serialize(userRef, 'default').getBytes('UTF-8')) | ||
| 37 | def check = encoder.encode(digest.digest) | ||
| 38 | if(key.equals(check)) { | ||
| 39 | // Adding the PDF content in the response | ||
| 40 | response.setContentType("application/pdf") | ||
| 41 | IOUtils.copyLarge(pdfXDoc.getAttachment(filename).getContentInputStream(), response.getOutputStream()) | ||
| 42 | } | ||
| 43 | } finally { | ||
| 44 | // Changing back the context user | ||
| 45 | xcontext.context.setUserReference(currentUserRef) | ||
| 46 | } | ||
| 47 | } | ||
| 48 | {{/groovy}} |
