snoopdave commented on code in PR #223:
URL: https://github.com/apache/roller/pull/223#discussion_r4419609421
##########
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:
Two small things:
- Nothing tests this call. `PreviewServletPermissionTest` tests `canPreview`
directly, and only `PreviewResourceServletTest` checks the servlet wiring. A
`doGet` test where a user with no role gets 404 and nothing renders would keep
this from being lost in a refactor.
- A debug log with the user name and weblog handle here would make "why does
preview give 404?" quick to diagnose.
##########
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:
`Weblog.hasUserPermission(user, action)` already builds the
`WeblogPermission`, calls `checkPermission`, logs and returns false on error.
This could be:
```java
return weblog != null && user != null &&
Boolean.TRUE.equals(user.getEnabled())
&& weblog.hasUserPermission(user, WeblogPermission.EDIT_DRAFT);
```
Then "has a role in this weblog" is defined in one place.
##########
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:
Calling a static method on another servlet is an odd dependency. Could
`canPreview` move to a small shared class in `ui/rendering/util`? (A filter on
both preview URL patterns would also work and would cover future preview
servlets, but a shared class is the smaller change for 6.1.x.)
##########
app/src/test/java/org/apache/roller/weblogger/ui/rendering/servlets/PreviewServletPermissionTest.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:
Could we add a pending-invite case? Refusing a user who hasn't accepted an
invite depends on the named query excluding `pending` rows. A test that grants
a pending permission and expects `canPreview` to be false would protect that.
--
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]