From b698d0fa1e41a1e8008671e6806a60d3c2fc4802 Mon Sep 17 00:00:00 2001 From: Manuel Rego Casasnovas Date: Fri, 13 Apr 2012 15:45:38 +0200 Subject: [PATCH] Add new compilation option to disable default users (user, wsreader and wswriter) A new Maven property has been added to disable the default users. This property is enabled by default except for the "dev" profile. You can manually specify the property with the following argument: -Ddefault.exampleUsersDisabled=false FEA: ItEr76S04BugFixing --- .../business/common/Configuration.java | 35 +++++++++++++++---- .../libreplan-business-spring-config.xml | 5 ++- .../web/users/bootstrap/MandatoryUser.java | 32 ++++++++++++----- .../users/bootstrap/UsersBootstrapInDB.java | 12 ++++--- pom.xml | 4 +++ 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/libreplan-business/src/main/java/org/libreplan/business/common/Configuration.java b/libreplan-business/src/main/java/org/libreplan/business/common/Configuration.java index c7b66b0bb..49a2e7db2 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/common/Configuration.java +++ b/libreplan-business/src/main/java/org/libreplan/business/common/Configuration.java @@ -2,6 +2,7 @@ * This file is part of LibrePlan * * Copyright (C) 2010-2011 Wireless Galicia, S.L. + * Copyright (C) 2012 Igalia, S.L. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -19,24 +20,29 @@ package org.libreplan.business.common; -import org.libreplan.business.common.daos.IConfigurationDAO; -import org.springframework.beans.factory.annotation.Autowired; +import org.apache.commons.lang.BooleanUtils; + /** - * It contains the compiling option to disable the warning changing default - * password and implements of singleton pattern. + * This is a singleton that contains the compilation options passed from Maven. + * + * Currently we have two options: + * * * @author Susana Montes Pedreira + * @author Manuel Rego Casasnovas */ public class Configuration { private static final Configuration singleton = new Configuration(); - @Autowired - private IConfigurationDAO configurationDAO; - private Boolean defaultPasswordsControl; + private Boolean exampleUsersDisabled; + private Configuration() { } @@ -61,4 +67,19 @@ public class Configuration { return defaultPasswordsControl; } + public void setExampleUsersDisabled(Boolean exampleUsersDisabled) { + this.exampleUsersDisabled = exampleUsersDisabled; + } + + public Boolean getExampleUsersDisabled() { + return exampleUsersDisabled; + } + + /** + * Returns the value of example users disabled compilation option + */ + public static boolean isExampleUsersDisabled() { + return BooleanUtils.isNotFalse(singleton.getExampleUsersDisabled()); + } + } diff --git a/libreplan-business/src/main/resources/libreplan-business-spring-config.xml b/libreplan-business/src/main/resources/libreplan-business-spring-config.xml index b3f6ee43a..99f32cb1b 100644 --- a/libreplan-business/src/main/resources/libreplan-business-spring-config.xml +++ b/libreplan-business/src/main/resources/libreplan-business-spring-config.xml @@ -141,7 +141,10 @@ factory-method="getInstance" lazy-init="false"> - ${default.passwordsControl} + ${default.passwordsControl} + + + ${default.exampleUsersDisabled} diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/MandatoryUser.java b/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/MandatoryUser.java index 3e87b7628..ff5bfcd4d 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/MandatoryUser.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/MandatoryUser.java @@ -28,19 +28,25 @@ import java.util.EnumSet; import java.util.HashSet; import java.util.Set; +import org.libreplan.business.common.Configuration; import org.libreplan.business.common.Registry; -import org.libreplan.business.common.entities.Configuration; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.users.entities.UserRole; /** - * It enumerates the mandatory users (login names) for running the application. + * It enumerates the mandatory users (login names) for running the application.
+ * + * ADMIN user will be always enabled, however USER, + * WSREADER and WSWRITER could be disabled in + * copilation time with a Maven option specified via {@link Configuration} + * class. * * @author Fernando Bellas Permuy + * @author Manuel Rego Casasnovas */ public enum MandatoryUser { - USER(new ArrayList()) { + USER(new ArrayList(), Configuration.isExampleUsersDisabled()) { @Override public boolean hasChangedDefaultPassword() { return getConfiguration().getChangedDefaultUserPassword(); @@ -49,20 +55,22 @@ public enum MandatoryUser { ADMIN(Arrays.asList(UserRole.ROLE_ADMINISTRATION, UserRole.ROLE_READ_ALL_ORDERS, UserRole.ROLE_EDIT_ALL_ORDERS, - UserRole.ROLE_CREATE_ORDER)) { + UserRole.ROLE_CREATE_ORDER), false) { @Override public boolean hasChangedDefaultPassword() { return getConfiguration().getChangedDefaultAdminPassword(); } }, - WSREADER(Arrays.asList(UserRole.ROLE_WS_READER)) { + WSREADER(Arrays.asList(UserRole.ROLE_WS_READER), Configuration + .isExampleUsersDisabled()) { @Override public boolean hasChangedDefaultPassword() { return getConfiguration().getChangedDefaultWsreaderPassword(); } }, - WSWRITER(Arrays.asList(UserRole.ROLE_WS_READER, UserRole.ROLE_WS_WRITER)) { + WSWRITER(Arrays.asList(UserRole.ROLE_WS_READER, UserRole.ROLE_WS_WRITER), + Configuration.isExampleUsersDisabled()) { @Override public boolean hasChangedDefaultPassword() { return getConfiguration().getChangedDefaultWswriterPassword(); @@ -84,15 +92,23 @@ public enum MandatoryUser { return false; } - private static Configuration getConfiguration() { + private static org.libreplan.business.common.entities.Configuration getConfiguration() { return Registry.getConfigurationDAO() .getConfigurationWithReadOnlyTransaction(); } private Set initialRoles; - private MandatoryUser(Collection initialUserRoles) { + private final boolean userDisabled; + + private MandatoryUser(Collection initialUserRoles, + boolean userDisabled) { this.initialRoles = new HashSet(initialUserRoles); + this.userDisabled = userDisabled; + } + + public boolean isUserDisabled() { + return userDisabled; } public boolean hasChangedDefaultPasswordOrDisabled() { diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/UsersBootstrapInDB.java b/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/UsersBootstrapInDB.java index 3317461df..e7c518384 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/UsersBootstrapInDB.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/users/bootstrap/UsersBootstrapInDB.java @@ -3,7 +3,7 @@ * * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e * Desenvolvemento Tecnolóxico de Galicia - * Copyright (C) 2010-2011 Igalia, S.L. + * Copyright (C) 2010-2012 Igalia, S.L. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -28,7 +28,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; /** + * Bootstrapt to create the default {@link User}s. + * * @author Fernando Bellas Permuy + * @author Manuel Rego Casasnovas */ @Transactional public class UsersBootstrapInDB implements IUsersBootstrapInDB { @@ -57,10 +60,11 @@ public class UsersBootstrapInDB implements IUsersBootstrapInDB { private void createUserIfNotExists(MandatoryUser u) { if (!userDAO.existsByLoginName(u.getLoginName())) { + User user = User.create(u.getLoginName(), getEncodedPassword(u), + u.getInitialRoles()); + user.setDisabled(u.isUserDisabled()); - userDAO.save(User.create(u.getLoginName(), getEncodedPassword(u), - u.getInitialRoles())); - + userDAO.save(user); } } diff --git a/pom.xml b/pom.xml index 80075596d..fa1c33809 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ public. true + true + false