Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@
entry's search description containing a quote no longer cuts the
`<meta name="description">` tag short, whether or not weblog admins are
trusted.
- **Saving the "Create weblog" form follows the site setting.** When "Allow
users to create weblogs" is off, saving the form now refuses, as opening it
already did.
- **Only the site's first account becomes an administrator.** With
`users.firstUserAdmin` on, a new account was made an administrator whenever
no enabled account existed. Disabled accounts now count too.

## 6.1.7

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ public void addUser(User newUser) throws WebloggerException {
}

boolean adminUser = false;
List<User> existingUsers = this.getUsers(Boolean.TRUE, null, null, 0, 1);
boolean firstUserAdmin = WebloggerConfig.getBooleanProperty("users.firstUserAdmin");
if (existingUsers.isEmpty() && firstUserAdmin) {
// the first account of the site, enabled or not
if (firstUserAdmin && strategy.getNamedQuery("User.getCount", Long.class)
.getSingleResult() == 0) {
// Make first user an admin
adminUser = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public boolean isWeblogRequired() {
@Override
public String execute() {

// check if blog administrator has enabled creation of new blogs
if(!WebloggerRuntimeConfig.getBooleanProperty("site.allowUserWeblogCreation")) {
addError("createWebsite.disabled");
if (!isWeblogCreationAllowed()) {
return DISABLED_RETURN_CODE;
}

Expand Down Expand Up @@ -103,7 +101,11 @@ public String execute() {


public String save() {


if (!isWeblogCreationAllowed()) {
return DISABLED_RETURN_CODE;
}

User user = getAuthenticatedUser();
try {
if (!WebloggerConfig.getBooleanProperty("groupblogging.enabled")) {
Expand Down Expand Up @@ -161,6 +163,19 @@ public String save() {
}


/**
* True when the site lets users create weblogs. The form and its save
* both follow the site setting.
*/
private boolean isWeblogCreationAllowed() {
if (!WebloggerRuntimeConfig.getBooleanProperty("site.allowUserWeblogCreation")) {
addError("createWebsite.disabled");
return false;
}
return true;
}


public void myValidate() {

String allowed = WebloggerConfig.getProperty("username.allowedChars");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<named-query name="User.getCountByUserNameLike">
<query>SELECT COUNT(u) FROM User u WHERE UPPER(u.userName) LIKE ?1</query>
</named-query>
<named-query name="User.getCount">
<query>SELECT COUNT(u) FROM User u</query>
</named-query>
<named-query name="User.getCountEnabledDistinct">
<!--
DISTINCT is not required for this query as no duplicate User would be retrieved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.roller.weblogger.TestUtils;
import org.apache.roller.weblogger.config.WebloggerConfig;
import org.apache.roller.weblogger.pojos.User;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* With users.firstUserAdmin on, only the site's first account becomes an
* administrator. Accounts that exist but are disabled still count.
*/
class FirstUserAdminTest {

private static final String NEW_USER = TestUtils.JUNIT_PREFIX + "firstadminnew";

private String existingName;
private final List<String> disabledHere = new ArrayList<>();

@BeforeEach
void setUp() throws Exception {
TestUtils.setupWeblogger();
existingName = TestUtils.setupUser("firstadminexisting").getUserName();
TestUtils.endSession(true);
}

@AfterEach
void tearDown() throws Exception {
UserManager mgr = users();
for (String name : disabledHere) {
User user = mgr.getUserByUserName(name, null);
if (user != null) {
user.setEnabled(Boolean.TRUE);
mgr.saveUser(user);
}
}
TestUtils.endSession(true);
for (String name : new String[] { NEW_USER, existingName }) {
if (mgr.getUserByUserName(name, null) != null) {
TestUtils.teardownUser(name);
}
}
TestUtils.endSession(true);
}

@Test
void aNewAccountIsNotAdminWhenOnlyDisabledAccountsExist() throws Exception {
assertTrue(WebloggerConfig.getBooleanProperty("users.firstUserAdmin"));
UserManager mgr = users();
for (User user : mgr.getUsers(Boolean.TRUE, null, null, 0, -1)) {
user.setEnabled(Boolean.FALSE);
mgr.saveUser(user);
disabledHere.add(user.getUserName());
}
TestUtils.endSession(true);
assertTrue(disabledHere.contains(existingName));

User user = new User();
user.setUserName(NEW_USER);
user.setPassword("password");
user.setScreenName("First admin test");
user.setFullName("First admin test");
user.setEmailAddress("firstadmin@example.com");
user.setLocale("en_US");
user.setTimeZone("America/New_York");
user.setDateCreated(new Date());
user.setEnabled(Boolean.TRUE);
mgr.addUser(user);
TestUtils.endSession(true);

User saved = mgr.getUserByUserName(NEW_USER, null);
assertNotNull(saved);
assertTrue(mgr.hasRole("editor", saved));
assertFalse(mgr.hasRole("admin", saved), "an earlier account exists, though disabled");
}

private static UserManager users() {
return WebloggerFactory.getWeblogger().getUserManager();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 org.apache.roller.weblogger.TestUtils;
import org.apache.roller.weblogger.business.PropertiesManager;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.pojos.RuntimeConfigProperty;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.Weblog;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;

/**
* Saving the create-weblog form follows the site setting for user weblog
* creation, as opening the form does.
*/
class CreateWeblogSettingTest {

private static final String SETTING = "site.allowUserWeblogCreation";
private static final String HANDLE = "createsettingtest";

private String userName;
private String savedValue;

@BeforeEach
void setUp() throws Exception {
TestUtils.setupWeblogger();
userName = TestUtils.setupUser("createsettinguser").getUserName();
savedValue = setting().getValue();
TestUtils.endSession(true);
}

@AfterEach
void tearDown() throws Exception {
setSetting(savedValue);
Weblog weblog = WebloggerFactory.getWeblogger().getWeblogManager().getWeblogByHandle(HANDLE);
if (weblog != null) {
TestUtils.teardownWeblog(weblog.getId());
}
TestUtils.teardownUser(userName);
TestUtils.endSession(true);
}

@Test
void saveCreatesNoWeblogWhenTheSiteSettingIsOff() throws Exception {
setSetting("false");

CreateWeblog action = action();
assertEquals("disabled", action.save());
TestUtils.endSession(true);

assertNull(WebloggerFactory.getWeblogger().getWeblogManager().getWeblogByHandle(HANDLE));
}

@Test
void saveCreatesTheWeblogWhenTheSiteSettingIsOn() throws Exception {
setSetting("true");

CreateWeblog action = action();
assertEquals("success", action.save());
TestUtils.endSession(true);

assertNotNull(WebloggerFactory.getWeblogger().getWeblogManager().getWeblogByHandle(HANDLE));
}

private CreateWeblog action() throws Exception {
CreateWeblog action = spy(new CreateWeblog());
doAnswer(call -> call.getArgument(0)).when(action).getText(anyString());
doAnswer(call -> call.getArgument(0)).when(action).getText(anyString(), anyString());
doAnswer(call -> call.getArgument(0)).when(action).getText(anyString(), anyList());
doAnswer(call -> call.getArgument(0)).when(action)
.getText(anyString(), anyString(), anyString());
User user = WebloggerFactory.getWeblogger().getUserManager().getUserByUserName(userName);
action.setAuthenticatedUser(user);
CreateWeblogBean bean = action.getBean();
bean.setHandle(HANDLE);
bean.setName("Create setting test");
bean.setEmailAddress("createsetting@example.com");
bean.setTheme("basic");
bean.setLocale("en_US");
bean.setTimeZone("America/New_York");
return action;
}

private static RuntimeConfigProperty setting() throws Exception {
return WebloggerFactory.getWeblogger().getPropertiesManager().getProperty(SETTING);
}

private static void setSetting(String value) throws Exception {
PropertiesManager properties = WebloggerFactory.getWeblogger().getPropertiesManager();
RuntimeConfigProperty property = properties.getProperty(SETTING);
property.setValue(value);
properties.saveProperty(property);
TestUtils.endSession(true);
}
}
Loading