This is an automated email from the ASF dual-hosted git repository.

snoopdave pushed a commit to branch roller-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git


The following commit(s) were added to refs/heads/roller-6.1.x by this push:
     new 515767be7 Fix registration after the first user and deleting drafts as 
a Limited member (6.1.x) (#399)
515767be7 is described below

commit 515767be757bffc3bb1587840458bbad176ae8ae
Author: David M. Johnson <[email protected]>
AuthorDate: Thu Nov 26 16:34:31 2026 -0500

    Fix registration after the first user and deleting drafts as a Limited 
member (6.1.x) (#399)
    
    * Let users register after the first administrator
    
    Registration saved a new setup-completed property every time, so every
    registration after the first failed on the duplicate key. Only the first
    administrator's registration records it now, and an existing row is
    updated rather than inserted again.
    
    * Return Limited members to the editor after deleting a draft
    
    The entries list needs the Author role, so chaining there after a
    removal showed the access denied page.
    
    * Address review: delete result, first-admin race, registration test
    
    - Work out where to go after a removal without failing: a permission
      lookup error now falls back to the editor instead of asking again for
      an entry that is already gone.
    - Handle the first administrator's registration one request at a time,
      so a second submission sees setup finished.
    - Drive later registrations through save() against the database, and
      remove the setup marker afterwards.
---
 CHANGES.md                                         |   6 +
 .../ui/core/security/BootstrapSecurity.java        |   7 +-
 .../roller/weblogger/ui/struts2/core/Register.java |  35 ++++-
 .../weblogger/ui/struts2/editor/EntryRemove.java   |  22 ++-
 app/src/main/resources/struts.xml                  |   2 +
 .../ui/core/security/BootstrapSecurityTest.java    |  12 ++
 .../ui/struts2/core/RegisterSetupMarkerTest.java   | 155 +++++++++++++++++++++
 .../ui/struts2/core/RegisterSiteUrlTest.java       |  21 +++
 .../struts2/editor/EntryWritePermissionTest.java   |  32 ++++-
 9 files changed, 286 insertions(+), 6 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 789f5df79..085f0e673 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -132,6 +132,12 @@
 
 ### Bug fixes
 
+- **New users can register after the first.** Since 6.1.6, every
+  registration after the first administrator's failed with a database error
+  on the setup-completed setting, so no account was created.
+- **Limited members no longer see an access denied page after deleting a
+  draft.** Deleting from the entry editor removed the draft, then opened the
+  entries list, which needs the Author role. They now return to the editor.
 - **The comment filter works after editing a comment.** Saving a comment
   on the Comments page left the filter form with a used-up form token, so
   filtering then failed with an error.
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurity.java
 
b/app/src/main/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurity.java
index 3bb105a5f..3ea8732c3 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurity.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurity.java
@@ -104,7 +104,12 @@ public final class BootstrapSecurity {
                     // first administrator is committed.
                     return false;
                 }
-                properties.saveProperty(new 
RuntimeConfigProperty(COMPLETION_PROPERTY, "true"));
+                if (marker == null) {
+                    marker = new RuntimeConfigProperty(COMPLETION_PROPERTY, 
"true");
+                } else {
+                    marker.setValue("true");
+                }
+                properties.saveProperty(marker);
                 weblogger.flush();
             }
             complete();
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
index ce3e84e61..996b5dc92 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/core/Register.java
@@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.weblogger.WebloggerException;
+import org.apache.roller.weblogger.business.PropertiesManager;
 import org.apache.roller.weblogger.business.WebloggerFactory;
 import org.apache.roller.weblogger.business.UserManager;
 import org.apache.roller.weblogger.config.AuthMethod;
@@ -58,6 +59,8 @@ import 
org.apache.struts2.interceptor.validation.SkipValidation;
 public class Register extends UIAction implements ServletRequestAware {
     
     private static Log log = LogFactory.getLog(Register.class);
+
+    private static final Object INITIAL_ADMIN_LOCK = new Object();
     private static final String DISABLED_RETURN_CODE = "disabled";
     public static final String DEFAULT_ALLOWED_CHARS = "A-Za-z0-9";
 
@@ -174,6 +177,17 @@ public class Register extends UIAction implements 
ServletRequestAware {
     
     
     public String save() {
+        if (BootstrapSecurity.isCompleted()) {
+            return register();
+        }
+        // Two submissions of the first administrator's form would both see
+        // setup as not done. One at a time, the second sees it finished.
+        synchronized (INITIAL_ADMIN_LOCK) {
+            return register();
+        }
+    }
+
+    private String register() {
         if (!BootstrapSecurity.isCompleted() && 
!BootstrapSecurity.isValid(getServletRequest())) {
             return DISABLED_RETURN_CODE;
         }
@@ -257,12 +271,12 @@ public class Register extends UIAction implements 
ServletRequestAware {
 
                 // save new user
                 mgr.addUser(ud);
-                
WebloggerFactory.getWeblogger().getPropertiesManager().saveProperty(
-                        new 
RuntimeConfigProperty(BootstrapSecurity.COMPLETION_PROPERTY, "true"));
 
                 // the first administrator is the one setting up the site, so
-                // take the site's url from this request if none is configured
+                // record that setup is done and take the site's url from this
+                // request if none is configured
                 if (initialAdmin) {
+                    markSetupCompleted();
                     saveSiteUrlIfUnset();
                 }
 
@@ -294,6 +308,21 @@ public class Register extends UIAction implements 
ServletRequestAware {
         return INPUT;
     }
 
+    /**
+     * Records that setup is done. The marker is a single row, so an existing
+     * one is updated rather than inserted again.
+     */
+    static void markSetupCompleted() throws WebloggerException {
+        PropertiesManager properties = 
WebloggerFactory.getWeblogger().getPropertiesManager();
+        RuntimeConfigProperty marker = 
properties.getProperty(BootstrapSecurity.COMPLETION_PROPERTY);
+        if (marker == null) {
+            marker = new 
RuntimeConfigProperty(BootstrapSecurity.COMPLETION_PROPERTY, "true");
+        } else {
+            marker.setValue("true");
+        }
+        properties.saveProperty(marker);
+    }
+
     /**
      * Failing to save the site's url doesn't stop the registration; an
      * administrator can set it later.
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
index 80bf288bf..977f177d9 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
@@ -41,6 +41,9 @@ public class EntryRemove extends UIAction {
 
     private static Log log = LogFactory.getLog(EntryRemove.class);
 
+    /** Result for a member who cannot see the entries list. */
+    static final String SUCCESS_EDITOR = "success-editor";
+
     // id of entry to remove
     private String removeId = null;
 
@@ -106,7 +109,7 @@ public class EntryRemove extends UIAction {
                 // note to user
                 addMessage("weblogEdit.entryRemoved", entry.getTitle());
 
-                return SUCCESS;
+                return canListEntries() ? SUCCESS : SUCCESS_EDITOR;
 
             } catch (Exception e) {
                 log.error("Error removing entry " + getRemoveId(), e);
@@ -120,6 +123,23 @@ public class EntryRemove extends UIAction {
         return INPUT;
     }
 
+    /**
+     * The entries list needs the Author role, so a Limited member returns to
+     * the editor after a removal instead. The entry is already gone when this
+     * runs, so a failed lookup falls back to the editor rather than an error.
+     */
+    private boolean canListEntries() {
+        WeblogPermission listPerm = new WeblogPermission(getActionWeblog(),
+                Collections.singletonList(WeblogPermission.POST));
+        try {
+            return WebloggerFactory.getWeblogger().getUserManager()
+                    .checkPermission(listPerm, getAuthenticatedUser());
+        } catch (WebloggerException ex) {
+            log.warn("Error checking permissions after removing entry " + 
getRemoveId(), ex);
+            return false;
+        }
+    }
+
     private boolean canRemoveEntry() {
         if (getRemoveEntry() == null) {
             return true;
diff --git a/app/src/main/resources/struts.xml 
b/app/src/main/resources/struts.xml
index b5af0308c..108d3c60b 100644
--- a/app/src/main/resources/struts.xml
+++ b/app/src/main/resources/struts.xml
@@ -431,6 +431,7 @@
                 <param name="namespace">/roller-ui</param>
             </result>
             <result name="success" type="chain">entryAdd</result>
+            <result name="success-editor" type="chain">entryAdd</result>
             <allowed-methods>execute,remove</allowed-methods>
         </action>
 
@@ -453,6 +454,7 @@
                 <param name="namespace">/roller-ui</param>
             </result>
           <result name="success" type="chain">entries</result>
+          <result name="success-editor" type="chain">entryAdd</result>
             <allowed-methods>execute,remove</allowed-methods>
         </action>
 
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurityTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurityTest.java
index 7b79d8170..93dbf607e 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurityTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/ui/core/security/BootstrapSecurityTest.java
@@ -86,6 +86,18 @@ class BootstrapSecurityTest {
         verify(users, never()).getUserCount();
     }
 
+    @Test
+    void existingUnsetMarkerIsUpdatedNotInsertedAgain() throws Exception {
+        RuntimeConfigProperty marker =
+                new 
RuntimeConfigProperty(BootstrapSecurity.COMPLETION_PROPERTY, "false");
+        
when(properties.getProperty(BootstrapSecurity.COMPLETION_PROPERTY)).thenReturn(marker);
+        when(users.getUserCount()).thenReturn(3L);
+
+        assertTrue(BootstrapSecurity.completeIfInstalled(weblogger));
+        
verify(properties).saveProperty(org.mockito.ArgumentMatchers.same(marker));
+        assertEquals("true", marker.getValue());
+    }
+
     @Test
     void unreadablePropertiesLeaveGateOpen() throws Exception {
         when(properties.getProperty(BootstrapSecurity.COMPLETION_PROPERTY))
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSetupMarkerTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSetupMarkerTest.java
new file mode 100644
index 000000000..799bcf7d0
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSetupMarkerTest.java
@@ -0,0 +1,155 @@
+/*
+ * 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
+ *
+ *   http://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.struts2.core;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.roller.weblogger.TestUtils;
+import org.apache.roller.weblogger.business.PropertiesManager;
+import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.business.startup.WebloggerStartup;
+import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
+import org.apache.roller.weblogger.ui.core.RollerContext;
+import org.apache.roller.weblogger.ui.core.security.BootstrapSecurity;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.springframework.security.crypto.password.NoOpPasswordEncoder;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+/**
+ * Once setup is recorded in the database, later registrations must still
+ * save their user: they used to insert the setup marker again and fail on
+ * the duplicate key when the session was flushed.
+ */
+class RegisterSetupMarkerTest {
+
+    private static final String REGISTRATION = "users.registration.enabled";
+    private static final String[] USERS = {"junitregistersecond", 
"junitregisterthird"};
+
+    private RuntimeConfigProperty savedMarker;
+    private String savedRegistration;
+    private MockedStatic<RollerContext> context;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        TestUtils.setupWeblogger();
+        PropertiesManager properties = properties();
+        RuntimeConfigProperty marker = 
properties.getProperty(BootstrapSecurity.COMPLETION_PROPERTY);
+        savedMarker = marker == null ? null : new 
RuntimeConfigProperty(marker.getName(), marker.getValue());
+        RuntimeConfigProperty registration = 
properties.getProperty(REGISTRATION);
+        savedRegistration = registration == null ? null : 
registration.getValue();
+        setProperty(REGISTRATION, "true");
+        TestUtils.endSession(true);
+
+        context = mockStatic(RollerContext.class);
+        
context.when(RollerContext::getPasswordEncoder).thenReturn(NoOpPasswordEncoder.getInstance());
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        context.close();
+        for (String user : USERS) {
+            if 
(WebloggerFactory.getWeblogger().getUserManager().getUserByUserName(user, null) 
!= null) {
+                TestUtils.teardownUser(user);
+            }
+        }
+        if (savedRegistration != null) {
+            setProperty(REGISTRATION, savedRegistration);
+        }
+        if (savedMarker != null) {
+            setProperty(savedMarker.getName(), savedMarker.getValue());
+        }
+        TestUtils.endSession(true);
+        if (savedMarker == null) {
+            try (Connection con = 
WebloggerStartup.getDatabaseProvider().getConnection();
+                 PreparedStatement delete = con.prepareStatement(
+                         "delete from roller_properties where name = ?")) {
+                delete.setString(1, BootstrapSecurity.COMPLETION_PROPERTY);
+                delete.executeUpdate();
+            }
+        }
+    }
+
+    @Test
+    void registrationsAfterSetupIsRecordedSaveTheirUsers() throws Exception {
+        Register.markSetupCompleted();
+        TestUtils.endSession(true);
+
+        try (MockedStatic<BootstrapSecurity> bootstrap = 
mockStatic(BootstrapSecurity.class)) {
+            bootstrap.when(BootstrapSecurity::isCompleted).thenReturn(true);
+            for (String user : USERS) {
+                Register action = register(user);
+                String result = action.save();
+                assertEquals("success", result,
+                        () -> user + " " + action.getActionErrors() + " " + 
action.getFieldErrors());
+                TestUtils.endSession(true);
+            }
+        }
+
+        for (String user : USERS) {
+            
assertNotNull(WebloggerFactory.getWeblogger().getUserManager().getUserByUserName(user),
 user);
+        }
+        assertEquals("true", 
properties().getProperty(BootstrapSecurity.COMPLETION_PROPERTY).getValue());
+    }
+
+    private static PropertiesManager properties() {
+        return WebloggerFactory.getWeblogger().getPropertiesManager();
+    }
+
+    private static void setProperty(String name, String value) throws 
Exception {
+        PropertiesManager properties = properties();
+        RuntimeConfigProperty property = properties.getProperty(name);
+        if (property == null) {
+            property = new RuntimeConfigProperty(name, value);
+        } else {
+            property.setValue(value);
+        }
+        properties.saveProperty(property);
+    }
+
+    private static Register register(String userName) {
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        when(request.getSession()).thenReturn(mock(HttpSession.class));
+
+        Register action = spy(new Register());
+        doAnswer(call -> 
call.getArgument(0)).when(action).getText(anyString());
+        action.setServletRequest(request);
+        action.getBean().setUserName(userName);
+        action.getBean().setScreenName(userName);
+        action.getBean().setFullName(userName);
+        action.getBean().setEmailAddress(userName + "@example.com");
+        action.getBean().setPasswordText("s3cret-Passw0rd");
+        action.getBean().setPasswordConfirm("s3cret-Passw0rd");
+        return action;
+    }
+}
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSiteUrlTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSiteUrlTest.java
index a2571ed41..3149e9f66 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSiteUrlTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/core/RegisterSiteUrlTest.java
@@ -157,6 +157,27 @@ class RegisterSiteUrlTest {
                 eq("site.absoluteurl"), any(), any());
     }
 
+    @Test
+    void firstAdministratorRecordsSetupCompleted() throws Exception {
+        bootstrap.when(BootstrapSecurity::isCompleted).thenReturn(false);
+        bootstrap.when(() -> 
BootstrapSecurity.isValid(any())).thenReturn(true);
+
+        assertEquals("success", register("https", "blog.example.org", 
443).save());
+        verify(properties).saveProperty(org.mockito.ArgumentMatchers.argThat(
+                p -> BootstrapSecurity.COMPLETION_PROPERTY.equals(p.getName())
+                        && "true".equals(p.getValue())));
+    }
+
+    @Test
+    void laterRegistrationsLeaveSetupMarkerAlone() throws Exception {
+        bootstrap.when(BootstrapSecurity::isCompleted).thenReturn(true);
+
+        assertEquals("success", register("http", "other.example.net", 
80).save());
+        verify(users).addUser(any());
+        verify(properties, 
never()).saveProperty(org.mockito.ArgumentMatchers.argThat(
+                p -> 
BootstrapSecurity.COMPLETION_PROPERTY.equals(p.getName())));
+    }
+
     @Test
     void firstAdministratorIsRegisteredEvenIfSiteUrlCannotBeSaved() throws 
Exception {
         bootstrap.when(BootstrapSecurity::isCompleted).thenReturn(false);
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/ui/struts2/editor/EntryWritePermissionTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/editor/EntryWritePermissionTest.java
index a029dc771..6eff5782a 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/ui/struts2/editor/EntryWritePermissionTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/ui/struts2/editor/EntryWritePermissionTest.java
@@ -19,6 +19,7 @@ package org.apache.roller.weblogger.ui.struts2.editor;
 import java.util.Collections;
 import java.util.Locale;
 
+import org.apache.roller.weblogger.WebloggerException;
 import org.apache.roller.weblogger.business.UserManager;
 import org.apache.roller.weblogger.business.WeblogEntryManager;
 import org.apache.roller.weblogger.business.Weblogger;
@@ -104,11 +105,38 @@ class EntryWritePermissionTest {
     void limitedMemberCanRemoveADraftOrPendingEntry() throws Exception {
         memberHas(WeblogPermission.EDIT_DRAFT);
         for (PubStatus status : new PubStatus[]{PubStatus.DRAFT, 
PubStatus.PENDING}) {
-            assertEquals(UIAction.SUCCESS, 
removeAction(entry(status)).remove());
+            assertEquals(EntryRemove.SUCCESS_EDITOR, 
removeAction(entry(status)).remove());
         }
         verify(entries, times(2)).removeWeblogEntry(any());
     }
 
+    @Test
+    void limitedMemberReturnsToTheEditorAfterRemoving() throws Exception {
+        // The entries list needs the Author role.
+        memberHas(WeblogPermission.EDIT_DRAFT);
+
+        assertEquals(EntryRemove.SUCCESS_EDITOR, 
removeAction(entry(PubStatus.DRAFT)).remove());
+    }
+
+    @Test
+    void failedRoleLookupAfterRemovingFallsBackToTheEditor() throws Exception {
+        memberHas(WeblogPermission.POST);
+        WeblogEntry entry = entry(PubStatus.DRAFT);
+        EntryRemove action = removeAction(entry);
+        when(users.checkPermission(any(WeblogPermission.class), 
any(User.class)))
+                .thenThrow(new WebloggerException("lookup failed"));
+
+        assertEquals(EntryRemove.SUCCESS_EDITOR, action.remove());
+        verify(entries).removeWeblogEntry(entry);
+    }
+
+    @Test
+    void authorReturnsToTheEntriesListAfterRemoving() throws Exception {
+        memberHas(WeblogPermission.POST);
+
+        assertEquals(UIAction.SUCCESS, 
removeAction(entry(PubStatus.DRAFT)).remove());
+    }
+
     @Test
     void authorCanRemoveAPublishedEntry() throws Exception {
         memberHas(WeblogPermission.POST);
@@ -172,6 +200,8 @@ class EntryWritePermissionTest {
         WeblogPermission permission = new WeblogPermission(
                 weblog, user, Collections.singletonList(action));
         when(users.getWeblogPermission(weblog, user)).thenReturn(permission);
+        when(users.checkPermission(any(WeblogPermission.class), 
any(User.class)))
+                .thenAnswer(call -> permission.implies(call.getArgument(0)));
     }
 
     private WeblogEntry entry(PubStatus status) throws Exception {

Reply via email to