snoopdave commented on code in PR #223:
URL: https://github.com/apache/roller/pull/223#discussion_r4419708613
##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/PreviewServlet.java:
##########
@@ -293,4 +301,21 @@ public void doGet(HttpServletRequest request,
HttpServletResponse response)
log.debug("Exiting");
}
+
+ /**
+ * Previews show unpublished content, so only an enabled user with a role
+ * in the weblog (or a global admin) may see them.
+ */
+ static boolean canPreview(Weblog weblog, User user) {
Review Comment:
🤖Claude: fixed by having `canPreview` return `weblog != null && user != null
&& enabled && weblog.hasUserPermission(user, WeblogPermission.EDIT_DRAFT)`
(e17b2e9a1).
##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/PreviewResourceServlet.java:
##########
@@ -91,6 +91,11 @@ public void doGet(HttpServletRequest request,
HttpServletResponse response)
return;
}
+ if (!PreviewServlet.canPreview(weblog, resourceRequest.getUser())) {
Review Comment:
🤖Claude: fixed by moving the check to
`ui/rendering/util/PreviewAccess.canPreview`; both servlets call it, and
`PreviewServletPermissionTest` is now `PreviewAccessTest` in the same package
(e17b2e9a1).
##########
app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/PreviewServlet.java:
##########
@@ -102,6 +105,11 @@ public void doGet(HttpServletRequest request,
HttpServletResponse response)
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
+
+ if (!canPreview(weblog, previewRequest.getUser())) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
Review Comment:
🤖Claude: fixed by adding `PreviewServletAccessTest`: a user with no role
gets 404 from `doGet`, with no content type, writer or output stream used. I
checked it fails when the call is removed. `PreviewAccess` now logs a refusal
at debug level with the user name and weblog handle (e17b2e9a1).
##########
app/src/test/java/org/apache/roller/weblogger/ui/rendering/util/PreviewAccessTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.roller.weblogger.ui.rendering.servlets;
+
+import java.util.Collections;
+
+import org.apache.roller.weblogger.TestUtils;
+import org.apache.roller.weblogger.business.UserManager;
+import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.pojos.User;
+import org.apache.roller.weblogger.pojos.Weblog;
+import org.apache.roller.weblogger.pojos.WeblogPermission;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Previews are for users with a role in the weblog being previewed.
+ */
+class PreviewServletPermissionTest {
+
+ private User owner;
+ private User other;
+ private Weblog weblog;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ TestUtils.setupWeblogger();
+ owner = TestUtils.setupUser("previewOwner");
+ other = TestUtils.setupUser("previewOther");
+ weblog = TestUtils.setupWeblog("previewWeblog", owner);
+ TestUtils.endSession(true);
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ TestUtils.teardownWeblog(weblog.getId());
+ TestUtils.teardownUser(owner.getUserName());
+ TestUtils.teardownUser(other.getUserName());
+ TestUtils.endSession(true);
+ }
+
+ @Test
+ void ownerCanPreview() throws Exception {
+
assertTrue(PreviewServlet.canPreview(TestUtils.getManagedWebsite(weblog),
user(owner)));
+ }
+
+ @Test
+ void userWithoutRoleCannotPreview() throws Exception {
+
assertFalse(PreviewServlet.canPreview(TestUtils.getManagedWebsite(weblog),
user(other)));
+
assertFalse(PreviewServlet.canPreview(TestUtils.getManagedWebsite(weblog),
null));
+ }
+
+ @Test
+ void memberWithDraftRoleCanPreview() throws Exception {
Review Comment:
🤖Claude: fixed by adding `pendingInviteCannotPreview` to
`PreviewAccessTest`: it grants `EDIT_DRAFT` with `grantWeblogPermissionPending`
and expects `canPreview` to be false (e17b2e9a1).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]