From 052a2a9cbb248c7aae893335e52c199d7db0ab02 Mon Sep 17 00:00:00 2001
From: Thomas Mortagne <thomas.mortagne@gmail.com>
Date: Mon, 3 Oct 2016 19:25:21 +0200
Subject: [PATCH] XWIKI-13742: Put back
 XWikiDocument#getRenderedContent(String) behavior and move the safeguard at
 Document level

---
 .../edit/internal/EditorWikiComponent.java    |  4 +++-
 .../main/java/com/xpn/xwiki/api/Document.java | 13 ++++++++++++-
 .../java/com/xpn/xwiki/doc/XWikiDocument.java | 19 ++-----------------
 3 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/xwiki-platform-core/xwiki-platform-edit/xwiki-platform-edit-default/src/main/java/org/xwiki/edit/internal/EditorWikiComponent.java b/xwiki-platform-core/xwiki-platform-edit/xwiki-platform-edit-default/src/main/java/org/xwiki/edit/internal/EditorWikiComponent.java
index 9b504400bb9..656fd37ef11 100644
--- a/xwiki-platform-core/xwiki-platform-edit/xwiki-platform-edit-default/src/main/java/org/xwiki/edit/internal/EditorWikiComponent.java
+++ b/xwiki-platform-core/xwiki-platform-edit/xwiki-platform-edit-default/src/main/java/org/xwiki/edit/internal/EditorWikiComponent.java
@@ -142,10 +142,12 @@ protected String render() throws EditException
             XWikiDocument editorDocument = xcontext.getWiki().getDocument(this.getDocumentReference(), xcontext);
             BaseObject editorObject = editorDocument.getXObject(EDITOR_CLASS_REFERENCE);
             String editorCode = editorObject.getStringValue("code");
+            // Make sure the editor code is executed with the rights of the editor document author.
+            XWikiDocument sdoc = editorDocument;
             // Execute the editor code in the context of the current document (because the editor code needs to access
             // the data that has been put on the script context).
             return xcontext.getDoc().getRenderedContent(editorCode, editorDocument.getSyntax().toIdString(), false,
-                editorDocument, xcontext);
+                sdoc, xcontext);
         } catch (Exception e) {
             throw new EditException("Failed to render the editor code.", e);
         } finally {
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
index 1234211fac7..093d1f59948 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Document.java
@@ -753,9 +753,20 @@ private String getRenderedContent(String text, String syntaxId, boolean restrict
     public String getRenderedContent(String text, String sourceSyntaxId, String targetSyntaxId) throws XWikiException
     {
         // Make sure we keep using current author as passed content author
-        return this.doc.getRenderedContent(text, sourceSyntaxId, targetSyntaxId, false, null, getXWikiContext());
+        return this.doc.getRenderedContent(text, sourceSyntaxId, targetSyntaxId, false, getCallerDocument(getXWikiContext()),
+            getXWikiContext());
     }
 
+    private XWikiDocument getCallerDocument(XWikiContext xcontext)
+    {
+        XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
+        if (sdoc == null) {
+            sdoc = xcontext.getDoc();
+        }
+
+        return sdoc;
+    }
+    
     /**
      * @param targetSyntax the syntax in which to render the document content
      * @return the rendered content
diff --git a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
index c0faf0dc2a9..d90c2bb77c7 100644
--- a/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
+++ b/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
@@ -1268,7 +1268,7 @@ public String getRenderedContent(String text, String sourceSyntaxId, String targ
     public String getRenderedContent(String text, String sourceSyntaxId, String targetSyntaxId,
         boolean restrictedTransformationContext, XWikiContext context)
     {
-        return getRenderedContent(text, sourceSyntaxId, targetSyntaxId, restrictedTransformationContext, this, context);
+        return getRenderedContent(text, sourceSyntaxId, targetSyntaxId, restrictedTransformationContext, null, context);
     }
 
     /**
@@ -1288,11 +1288,6 @@ public String getRenderedContent(String text, String sourceSyntaxId, String targ
 
         XWikiDocument currentSDocument = (XWikiDocument) context.get(CKEY_SDOC);
         try {
-            // Remember what is the current caller document because #setAsContextDoc reset it
-            if (sDocument == null) {
-                sDocument = getCallerDocument(context);
-            }
-
             // We have to render the given text in the context of this document. Check if this document is already
             // on the context (same Java object reference). We don't check if the document references are equal
             // because this document can have temporary changes that are not present on the context document even if
@@ -1303,7 +1298,7 @@ public String getRenderedContent(String text, String sourceSyntaxId, String targ
                 setAsContextDoc(context);
             }
 
-            // Make sure to execute the document with the right of the calling author
+            // Make sure to execute the document with the right of the provided sdocument's author
             if (sDocument != null) {
                 context.put(CKEY_SDOC, sDocument);
             }
@@ -1331,16 +1326,6 @@ public String getRenderedContent(String text, String sourceSyntaxId, String targ
         return "";
     }
 
-    private XWikiDocument getCallerDocument(XWikiContext xcontext)
-    {
-        XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
-        if (sdoc == null) {
-            sdoc = xcontext.getDoc();
-        }
-
-        return sdoc;
-    }
-
     public String getEscapedContent(XWikiContext context) throws XWikiException
     {
         return XMLUtils.escape(getTranslatedContent(context));
-- 
GitLab