From 07170becfce3ff6b0d9ad3545602e5ec6801316e Mon Sep 17 00:00:00 2001 From: Oana-Lavinia Florean Date: Tue, 13 Feb 2024 18:19:35 +0200 Subject: [PATCH 1/2] XWIKI-21884: Uploading files during page creation step will mark XWikiGuest as creator * set the page creator --- .../internal/resources/BaseAttachmentsResource.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/BaseAttachmentsResource.java b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/BaseAttachmentsResource.java index 00eedec191bc..ac4e808318c1 100644 --- a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/BaseAttachmentsResource.java +++ b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/internal/resources/BaseAttachmentsResource.java @@ -56,6 +56,7 @@ import org.xwiki.rest.internal.Utils; import org.xwiki.rest.model.jaxb.Attachment; import org.xwiki.rest.model.jaxb.Attachments; +import org.xwiki.user.UserReferenceResolver; import com.xpn.xwiki.XWiki; import com.xpn.xwiki.XWikiContext; @@ -127,6 +128,10 @@ public boolean isAlreadyExisting() @Named("context") private Provider componentManagerProvider; + @Inject + @Named("document") + private UserReferenceResolver documentReferenceUserReferenceResolver; + /** * @param scope where to retrieve the attachments from; it should be a reference to a wiki, space or document * @param filters the filters used to restrict the set of attachments (you can filter by space name, document @@ -411,7 +416,11 @@ protected XWikiAttachment createOrUpdateAttachment(AttachmentReference attachmen String.format("Failed to instantiate a [%s] component.", AttachmentValidator.class.getName()), e); } - // Set the document author. + // Set the document creator / author. + if (document.isNew()) { + document.getAuthors() + .setCreator(this.documentReferenceUserReferenceResolver.resolve(xcontext.getUserReference())); + } document.setAuthorReference(xcontext.getUserReference()); // Calculate and store the attachment media type. From 68072acb283660c7c1aa7ce9230102eb6b30d9c1 Mon Sep 17 00:00:00 2001 From: Oana-Lavinia Florean Date: Wed, 14 Feb 2024 16:47:32 +0200 Subject: [PATCH 2/2] XWIKI-21884: Uploading files during page creation step will mark XWikiGuest as creator * adjust unit test to verify also creator --- .../AttachmentsResourceImplTest.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImplTest.java b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImplTest.java index 6acafd6e2fe5..3cebd625a559 100644 --- a/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImplTest.java +++ b/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/test/java/org/xwiki/rest/internal/resources/attachments/AttachmentsResourceImplTest.java @@ -35,6 +35,7 @@ import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.xwiki.attachment.validation.AttachmentValidationException; import org.xwiki.attachment.validation.AttachmentValidator; import org.xwiki.model.reference.AttachmentReference; @@ -42,6 +43,7 @@ import org.xwiki.model.reference.DocumentReferenceResolver; import org.xwiki.model.reference.EntityReference; import org.xwiki.model.reference.EntityReferenceSerializer; +import org.xwiki.model.reference.WikiReference; import org.xwiki.query.Query; import org.xwiki.rest.internal.resources.AbstractAttachmentsResourceTest; import org.xwiki.rest.model.jaxb.Attachment; @@ -50,6 +52,8 @@ import org.xwiki.security.authorization.Right; import org.xwiki.test.junit5.mockito.InjectMockComponents; import org.xwiki.test.junit5.mockito.MockComponent; +import org.xwiki.user.UserReference; +import org.xwiki.user.UserReferenceResolver; import com.xpn.xwiki.doc.XWikiAttachment; import com.xpn.xwiki.doc.XWikiDocument; @@ -75,6 +79,8 @@ @OldcoreTest class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest { + private static final DocumentReference USER_REFERENCE = new DocumentReference("test", "XWiki", "User"); + @InjectMockComponents AttachmentsResourceImpl attachmentsResource; @@ -91,6 +97,17 @@ class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest @MockComponent private AttachmentValidator attachmentValidator; + @MockComponent + @Named("document") + private UserReferenceResolver documentReferenceUserReferenceResolver; + + @MockComponent + @Named("compactwiki") + EntityReferenceSerializer compactWikiEntityReferenceSerializer; + + @Mock + private UserReference userReference; + @BeforeEach @Override public void setUp() throws Exception @@ -159,6 +176,13 @@ void getAttachments() throws Exception @Test void createAttachment() throws Exception { + this.xcontext.setMainXWiki("test"); + this.xcontext.setWikiId("test"); + this.xcontext.setUserReference(USER_REFERENCE); + when(this.documentReferenceUserReferenceResolver.resolve(USER_REFERENCE)).thenReturn(this.userReference); + when(this.compactWikiEntityReferenceSerializer.serialize(USER_REFERENCE, new WikiReference("test"))).thenReturn( + "XWiki.User"); + DocumentReference documentReference = new DocumentReference("test", Arrays.asList("Path", "To"), "Page"); XWikiDocument cachedDocument = prepareXWikiDocument(documentReference, "test:Path.To.Page", true, true, false); @@ -180,11 +204,12 @@ void createAttachment() throws Exception // The cached document should not have been modified. assertNull(cachedDocument.getAttachment("myBio.txt")); - XWikiAttachment xwikiAttachment = - this.xwiki.getDocument(documentReference, this.xcontext).getAttachment("myBio.txt"); + XWikiDocument xwikiDocument = this.xwiki.getDocument(documentReference, this.xcontext); + XWikiAttachment xwikiAttachment = xwikiDocument.getAttachment("myBio.txt"); assertEquals("myBio.txt", xwikiAttachment.getFilename()); assertEquals("text/plain", xwikiAttachment.getMimeType()); assertEquals("blah", IOUtils.toString(xwikiAttachment.getContentInputStream(this.xcontext))); + assertEquals(this.userReference, xwikiDocument.getAuthors().getCreator()); } @Test @@ -277,8 +302,9 @@ private XWikiDocument prepareXWikiDocument(DocumentReference documentReference, this.xwiki.saveDocument(document, this.xcontext); } - when(this.oldCore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", serializedDocumentReference, - this.xcontext)).thenReturn(hasView); + when(this.oldCore.getMockRightService() + .hasAccessLevel("view", this.xcontext.getUser(), serializedDocumentReference, this.xcontext)).thenReturn( + hasView); when(this.authorization.hasAccess(Right.EDIT, documentReference)).thenReturn(hasEdit); return this.xwiki.getDocument(documentReference, this.xcontext);