Skip to content
Snippets Groups Projects
Commit ab9f3d68 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 fbab6b6b
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 ...@@ -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 // 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 // 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. // 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()) { if (editedDocument.isContentDirty()) {
editedDocument.getAuthors().setContentAuthor(editedDocument.getAuthors().getEffectiveMetadataAuthor()); editedDocument.getAuthors().setContentAuthor(editedDocument.getAuthors().getEffectiveMetadataAuthor());
} }
......
...@@ -241,7 +241,7 @@ public boolean save(XWikiContext context) throws XWikiException ...@@ -241,7 +241,7 @@ public boolean save(XWikiContext context) throws XWikiException
UserReference currentUserReference = this.currentUserResolver.resolve(CurrentUserReference.INSTANCE); UserReference currentUserReference = this.currentUserResolver.resolve(CurrentUserReference.INSTANCE);
tdoc.getAuthors().setOriginalMetadataAuthor(currentUserReference); tdoc.getAuthors().setOriginalMetadataAuthor(currentUserReference);
tdoc.getAuthors().setEffectiveMetadataAuthor(request.getEffectiveAuthor()); request.getEffectiveAuthor().ifPresent(tdoc.getAuthors()::setEffectiveMetadataAuthor);
if (tdoc.isNew()) { if (tdoc.isNew()) {
tdoc.getAuthors().setCreator(currentUserReference); tdoc.getAuthors().setCreator(currentUserReference);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import javax.servlet.AsyncContext; import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
...@@ -498,7 +499,7 @@ public Cookie getCookie(String cookieName) ...@@ -498,7 +499,7 @@ public Cookie getCookie(String cookieName)
} }
@Override @Override
public UserReference getEffectiveAuthor() public Optional<UserReference> getEffectiveAuthor()
{ {
return this.request.getEffectiveAuthor(); return this.request.getEffectiveAuthor();
} }
......
...@@ -19,11 +19,12 @@ ...@@ -19,11 +19,12 @@
*/ */
package com.xpn.xwiki.web; package com.xpn.xwiki.web;
import java.util.Optional;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.xwiki.stability.Unstable; import org.xwiki.stability.Unstable;
import org.xwiki.user.GuestUserReference;
import org.xwiki.user.UserReference; import org.xwiki.user.UserReference;
public interface XWikiRequest extends HttpServletRequest public interface XWikiRequest extends HttpServletRequest
...@@ -36,16 +37,16 @@ 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 * @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 * triggered by this request. If the request doesn't indicate an effective author then the user that gets
* information provided by this request. If the request doesn't indicate an effective author and no user is * authenticated with the information provided by this request (or the guest user, if authentication
* authenticated then the {@link GuestUserReference} is returned. * information is missing) should be considered the effective author.
* @since 15.10.11 * @since 15.10.11
* @since 16.4.1 * @since 16.4.1
* @since 16.5.0RC1 * @since 16.5.0RC1
*/ */
@Unstable @Unstable
default UserReference getEffectiveAuthor() default Optional<UserReference> getEffectiveAuthor()
{ {
return GuestUserReference.INSTANCE; return Optional.empty();
} }
} }
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
*/ */
package com.xpn.xwiki.web; package com.xpn.xwiki.web;
import java.util.Optional;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequestWrapper;
...@@ -107,8 +109,8 @@ public String getRemoteHost() ...@@ -107,8 +109,8 @@ public String getRemoteHost()
} }
@Override @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 @@ ...@@ -34,6 +34,7 @@
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Vector; import java.util.Vector;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -918,8 +919,8 @@ public void setDaemon(boolean daemon) ...@@ -918,8 +919,8 @@ public void setDaemon(boolean daemon)
} }
@Override @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 @@ ...@@ -33,6 +33,7 @@
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.inject.Named; import javax.inject.Named;
...@@ -148,7 +149,7 @@ void setup() ...@@ -148,7 +149,7 @@ void setup()
this.context.setWiki(this.xWiki); this.context.setWiki(this.xWiki);
this.mockRequest = mock(XWikiRequest.class); 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.context.setRequest(this.mockRequest);
this.mockResponse = mock(XWikiResponse.class); this.mockResponse = mock(XWikiResponse.class);
......
...@@ -397,7 +397,7 @@ private XWikiDocument createSecurityDocument() ...@@ -397,7 +397,7 @@ private XWikiDocument createSecurityDocument()
XWikiContext xwikiContext = this.xcontextProvider.get(); XWikiContext xwikiContext = this.xcontextProvider.get();
// We clone the document in order to not impact the environment (the document cache for example). // We clone the document in order to not impact the environment (the document cache for example).
XWikiDocument clonedDocument = xwikiContext.getDoc().clone(); XWikiDocument clonedDocument = xwikiContext.getDoc().clone();
clonedDocument.getAuthors().setContentAuthor(xwikiContext.getRequest().getEffectiveAuthor()); xwikiContext.getRequest().getEffectiveAuthor().ifPresent(clonedDocument.getAuthors()::setContentAuthor);
this.injectTemoraryAttachments(clonedDocument); this.injectTemoraryAttachments(clonedDocument);
return 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