create scmadmin also if only _anonymous user exists

This commit is contained in:
Eduard Heimbuch
2019-11-20 11:42:32 +01:00
parent 6ac0ba3ab9
commit 906c27ebba
2 changed files with 20 additions and 4 deletions

View File

@@ -61,7 +61,7 @@ public class SetupContextListener implements ServletContextListener {
@Override @Override
public void run() { public void run() {
if (isFirstStart()) { if (shouldCreateAdminAccount()) {
createAdminAccount(); createAdminAccount();
} }
if (anonymousUserRequiredButNotExists()) { if (anonymousUserRequiredButNotExists()) {
@@ -73,8 +73,12 @@ public class SetupContextListener implements ServletContextListener {
return scmConfiguration.isAnonymousAccessEnabled() && !userManager.contains(SCMContext.USER_ANONYMOUS); return scmConfiguration.isAnonymousAccessEnabled() && !userManager.contains(SCMContext.USER_ANONYMOUS);
} }
private boolean isFirstStart() { private boolean shouldCreateAdminAccount() {
return userManager.getAll().isEmpty(); return userManager.getAll().isEmpty() || onlyAnonymousUserExists();
}
private boolean onlyAnonymousUserExists() {
return userManager.getAll().size() == 1 && userManager.contains(SCMContext.USER_ANONYMOUS);
} }
private void createAdminAccount() { private void createAdminAccount() {

View File

@@ -70,7 +70,19 @@ class SetupContextListenerTest {
} }
@Test @Test
void shouldCreateAdminAccountAndAssignPermissions() { void shouldCreateAdminAccountIfNoUserExistsAndAssignPermissions() {
when(passwordService.encryptPassword("scmadmin")).thenReturn("secret");
setupContextListener.contextInitialized(null);
verifyAdminCreated();
verifyAdminPermissionsAssigned();
}
@Test
void shouldCreateAdminAccountIfOnlyAnonymousUserExistsAndAssignPermissions() {
when(userManager.getAll()).thenReturn(Lists.newArrayList(SCMContext.ANONYMOUS));
when(userManager.contains(SCMContext.USER_ANONYMOUS)).thenReturn(true);
when(passwordService.encryptPassword("scmadmin")).thenReturn("secret"); when(passwordService.encryptPassword("scmadmin")).thenReturn("secret");
setupContextListener.contextInitialized(null); setupContextListener.contextInitialized(null);