Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XWIKI-21884: Uploading files during page creation step will mark XWikiGuest as creator #2885

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,6 +128,10 @@ public boolean isAlreadyExisting()
@Named("context")
private Provider<ComponentManager> componentManagerProvider;

@Inject
@Named("document")
private UserReferenceResolver<DocumentReference> 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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
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;
import org.xwiki.model.reference.DocumentReference;
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;
Expand All @@ -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;
Expand All @@ -75,6 +79,8 @@
@OldcoreTest
class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
{
private static final DocumentReference USER_REFERENCE = new DocumentReference("test", "XWiki", "User");

@InjectMockComponents
AttachmentsResourceImpl attachmentsResource;

Expand All @@ -91,6 +97,17 @@ class AttachmentsResourceImplTest extends AbstractAttachmentsResourceTest
@MockComponent
private AttachmentValidator attachmentValidator;

@MockComponent
@Named("document")
private UserReferenceResolver<DocumentReference> documentReferenceUserReferenceResolver;

@MockComponent
@Named("compactwiki")
EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer;

@Mock
private UserReference userReference;

@BeforeEach
@Override
public void setUp() throws Exception
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading