Skip to content
Snippets Groups Projects
Commit 8762508a authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

XWIKI-22222: Add the concept of "request effective author"

* Make the request effective author optional (while keeping the event listener that sets it to the currently authenticated user by default)

(cherry picked from commit 6510f9da)
parent 63dbdd2c
No related branches found
No related tags found
No related merge requests found
Showing
with 21 additions and 15 deletions
......@@ -140,7 +140,7 @@ public String render(XWikiContext context) throws XWikiException
// Make sure the current user doesn't use the programming rights of the previous content author (by editing a
// document saved with programming rights, changing it and then previewing it). Also make sure the code
// requiring programming rights is executed in preview mode if the current user has programming rights.
editedDocument.getAuthors().setEffectiveMetadataAuthor(context.getRequest().getEffectiveAuthor());
context.getRequest().getEffectiveAuthor().ifPresent(editedDocument.getAuthors()::setEffectiveMetadataAuthor);
if (editedDocument.isContentDirty()) {
editedDocument.getAuthors().setContentAuthor(editedDocument.getAuthors().getEffectiveMetadataAuthor());
}
......
......@@ -240,7 +240,7 @@ public boolean save(XWikiContext context) throws XWikiException
UserReference currentUserReference = this.currentUserResolver.resolve(CurrentUserReference.INSTANCE);
tdoc.getAuthors().setOriginalMetadataAuthor(currentUserReference);
tdoc.getAuthors().setEffectiveMetadataAuthor(request.getEffectiveAuthor());
request.getEffectiveAuthor().ifPresent(tdoc.getAuthors()::setEffectiveMetadataAuthor);
if (tdoc.isNew()) {
tdoc.getAuthors().setCreator(currentUserReference);
......
......@@ -27,6 +27,7 @@
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
......@@ -498,7 +499,7 @@ public Cookie getCookie(String cookieName)
}
@Override
public UserReference getEffectiveAuthor()
public Optional<UserReference> getEffectiveAuthor()
{
return this.request.getEffectiveAuthor();
}
......
......@@ -19,11 +19,12 @@
*/
package com.xpn.xwiki.web;
import java.util.Optional;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.xwiki.stability.Unstable;
import org.xwiki.user.GuestUserReference;
import org.xwiki.user.UserReference;
public interface XWikiRequest extends HttpServletRequest
......@@ -36,16 +37,16 @@ public interface XWikiRequest extends HttpServletRequest
/**
* @return the user that holds the responsibility, in terms of access rights, for the submitted data and the changes
* triggered by this request. By default the effective author is the user that gets authenticated with the
* information provided by this request. If the request doesn't indicate an effective author and no user is
* authenticated then the {@link GuestUserReference} is returned.
* triggered by this request. If the request doesn't indicate an effective author then the user that gets
* authenticated with the information provided by this request (or the guest user, if authentication
* information is missing) should be considered the effective author.
* @since 15.10.11
* @since 16.4.1
* @since 16.5.0RC1
*/
@Unstable
default UserReference getEffectiveAuthor()
default Optional<UserReference> getEffectiveAuthor()
{
return GuestUserReference.INSTANCE;
return Optional.empty();
}
}
......@@ -19,6 +19,8 @@
*/
package com.xpn.xwiki.web;
import java.util.Optional;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
......@@ -107,8 +109,8 @@ public String getRemoteHost()
}
@Override
public UserReference getEffectiveAuthor()
public Optional<UserReference> getEffectiveAuthor()
{
return (UserReference) getAttribute(ATTRIBUTE_EFFECTIVE_AUTHOR);
return Optional.ofNullable((UserReference) getAttribute(ATTRIBUTE_EFFECTIVE_AUTHOR));
}
}
......@@ -34,6 +34,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.Vector;
import java.util.stream.Collectors;
......@@ -918,8 +919,8 @@ public void setDaemon(boolean daemon)
}
@Override
public UserReference getEffectiveAuthor()
public Optional<UserReference> getEffectiveAuthor()
{
return (UserReference) getAttribute(XWikiServletRequest.ATTRIBUTE_EFFECTIVE_AUTHOR);
return Optional.ofNullable((UserReference) getAttribute(XWikiServletRequest.ATTRIBUTE_EFFECTIVE_AUTHOR));
}
}
......@@ -33,6 +33,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.inject.Named;
......@@ -148,7 +149,7 @@ void setup()
this.context.setWiki(this.xWiki);
this.mockRequest = mock(XWikiRequest.class);
when(this.mockRequest.getEffectiveAuthor()).thenReturn(this.effectiveAuthor);
when(this.mockRequest.getEffectiveAuthor()).thenReturn(Optional.of(this.effectiveAuthor));
this.context.setRequest(this.mockRequest);
this.mockResponse = mock(XWikiResponse.class);
......
......@@ -393,7 +393,7 @@ private XWikiDocument createSecurityDocument()
XWikiContext xwikiContext = this.xcontextProvider.get();
// We clone the document in order to not impact the environment (the document cache for example).
XWikiDocument clonedDocument = xwikiContext.getDoc().clone();
clonedDocument.getAuthors().setContentAuthor(xwikiContext.getRequest().getEffectiveAuthor());
xwikiContext.getRequest().getEffectiveAuthor().ifPresent(clonedDocument.getAuthors()::setContentAuthor);
this.injectTemoraryAttachments(clonedDocument);
return clonedDocument;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment