Modified behavior of UsersBootstrapInDB in order to create users only if there are no users yet

This will work like the rest of bootstraps. Accordingly to its behavior the
class has been renamed from MandatoryUser to PredefinedUsers.

FEA: ItEr76S30PermissionsEnhancements
This commit is contained in:
Manuel Rego Casasnovas 2012-06-21 09:08:13 +02:00
parent 26d5638b41
commit bdf731d473
12 changed files with 88 additions and 89 deletions

View file

@ -29,7 +29,8 @@ import org.apache.commons.lang.BooleanUtils;
* Currently we have two options:
* <ul>
* <li>Enable/Disable the warning changing default password</li>
* <li>Enable/Disable default users (such as user, wsreader and wswriter)</li>
* <li>Enable/Disable default users (such as wsreader, wswriter,
* wssubcontracting, manager, hresources, outsourcing and reports)</li>
* </ul>
*
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>

View file

@ -24,7 +24,7 @@ package org.libreplan.web.common;
import java.util.List;
import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
/**
* Contract for {@link TemplateModel}.
@ -50,7 +50,7 @@ public interface ITemplateModel {
boolean isScenariosVisible();
boolean hasChangedDefaultPassword(MandatoryUser user);
boolean hasChangedDefaultPassword(PredefinedUsers user);
boolean adminPasswordChangedAndSomeOtherNotChanged();

View file

@ -34,7 +34,7 @@ import org.libreplan.business.scenarios.entities.Scenario;
import org.libreplan.web.common.ITemplateModel.IOnFinished;
import org.libreplan.web.common.components.bandboxsearch.BandboxSearch;
import org.libreplan.web.security.SecurityUtils;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
@ -132,39 +132,39 @@ public class TemplateController extends GenericForwardComposer {
}
public String getDefaultPasswdAdminVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.ADMIN);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.ADMIN);
}
public String getDefaultPasswdWsreaderVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.WSREADER);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.WSREADER);
}
public String getDefaultPasswdWswriterVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.WSWRITER);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.WSWRITER);
}
public String getDefaultPasswdWssubcontractingVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.WSSUBCONTRACTING);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.WSSUBCONTRACTING);
}
public String getDefaultPasswdManagerVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.MANAGER);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.MANAGER);
}
public String getDefaultPasswdHresourcesVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.HRESOURCES);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.HRESOURCES);
}
public String getDefaultPasswdOutsourcingVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.OUTSOURCING);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.OUTSOURCING);
}
public String getDefaultPasswdReportsVisible() {
return notChangedPasswordWarningDisplayPropertyFor(MandatoryUser.REPORTS);
return notChangedPasswordWarningDisplayPropertyFor(PredefinedUsers.REPORTS);
}
private String notChangedPasswordWarningDisplayPropertyFor(
MandatoryUser mandatoryUser) {
PredefinedUsers mandatoryUser) {
return asDisplayProperty(templateModel
.hasChangedDefaultPassword(mandatoryUser));
}
@ -180,37 +180,37 @@ public class TemplateController extends GenericForwardComposer {
}
public String getIdAdminUser() {
return templateModel.getIdUser(MandatoryUser.ADMIN.getLoginName());
return templateModel.getIdUser(PredefinedUsers.ADMIN.getLoginName());
}
public String getIdWsreaderUser() {
return templateModel.getIdUser(MandatoryUser.WSREADER.getLoginName());
return templateModel.getIdUser(PredefinedUsers.WSREADER.getLoginName());
}
public String getIdWswriterUser() {
return templateModel.getIdUser(MandatoryUser.WSWRITER.getLoginName());
return templateModel.getIdUser(PredefinedUsers.WSWRITER.getLoginName());
}
public String getIdWssubcontractingUser() {
return templateModel.getIdUser(MandatoryUser.WSSUBCONTRACTING
return templateModel.getIdUser(PredefinedUsers.WSSUBCONTRACTING
.getLoginName());
}
public String getIdManagerUser() {
return templateModel.getIdUser(MandatoryUser.MANAGER.getLoginName());
return templateModel.getIdUser(PredefinedUsers.MANAGER.getLoginName());
}
public String getIdHresourcesUser() {
return templateModel.getIdUser(MandatoryUser.HRESOURCES.getLoginName());
return templateModel.getIdUser(PredefinedUsers.HRESOURCES.getLoginName());
}
public String getIdOutsourcingUser() {
return templateModel
.getIdUser(MandatoryUser.OUTSOURCING.getLoginName());
.getIdUser(PredefinedUsers.OUTSOURCING.getLoginName());
}
public String getIdReportsUser() {
return templateModel.getIdUser(MandatoryUser.REPORTS.getLoginName());
return templateModel.getIdUser(PredefinedUsers.REPORTS.getLoginName());
}
public boolean isUserAdmin() {

View file

@ -53,7 +53,7 @@ import org.libreplan.business.users.entities.User;
import org.libreplan.web.UserUtil;
import org.libreplan.web.planner.tabs.GanttDiagramBuilder;
import org.libreplan.web.security.SecurityUtils;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.libreplan.web.users.services.CustomUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
@ -426,14 +426,14 @@ public class TemplateModel implements ITemplateModel {
@Override
@Transactional(readOnly = true)
public boolean hasChangedDefaultPassword(MandatoryUser user) {
public boolean hasChangedDefaultPassword(PredefinedUsers user) {
return user.hasChangedDefaultPasswordOrDisabled();
}
@Override
@Transactional(readOnly = true)
public boolean adminPasswordChangedAndSomeOtherNotChanged() {
return MandatoryUser.adminChangedAndSomeOtherNotChanged();
return PredefinedUsers.adminChangedAndSomeOtherNotChanged();
}
@Override

View file

@ -21,7 +21,7 @@ package org.libreplan.web.users;
import org.libreplan.business.common.Registry;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.zkoss.zk.ui.util.Clients;
/**
@ -35,49 +35,49 @@ public class PasswordUtil {
public static void checkIfChangeDefaultPasswd(User user,
String clearPassword) {
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.ADMIN.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.ADMIN, clearPassword);
PredefinedUsers.ADMIN.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.ADMIN, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSREADER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSREADER, clearPassword);
PredefinedUsers.WSREADER.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.WSREADER, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSWRITER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSWRITER, clearPassword);
PredefinedUsers.WSWRITER.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.WSWRITER, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSSUBCONTRACTING.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSSUBCONTRACTING,
PredefinedUsers.WSSUBCONTRACTING.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.WSSUBCONTRACTING,
clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.MANAGER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.MANAGER, clearPassword);
PredefinedUsers.MANAGER.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.MANAGER, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.HRESOURCES.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.HRESOURCES, clearPassword);
PredefinedUsers.HRESOURCES.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.HRESOURCES, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.OUTSOURCING.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.OUTSOURCING, clearPassword);
PredefinedUsers.OUTSOURCING.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.OUTSOURCING, clearPassword);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.REPORTS.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.REPORTS, clearPassword);
PredefinedUsers.REPORTS.getLoginName())) {
checkIfChangeDefaultPasswd(PredefinedUsers.REPORTS, clearPassword);
return;
}
}
private static void checkIfChangeDefaultPasswd(MandatoryUser user,
private static void checkIfChangeDefaultPasswd(PredefinedUsers user,
String clearPassword) {
boolean changedPasswd = true;
if (clearPassword.isEmpty()
@ -97,21 +97,21 @@ public class PasswordUtil {
* been disabled
*/
public static void showOrHideDefaultPasswordWarnings() {
boolean adminNotDefaultPassword = MandatoryUser.ADMIN
boolean adminNotDefaultPassword = PredefinedUsers.ADMIN
.hasChangedDefaultPasswordOrDisabled();
boolean wsreaderNotDefaultPassword = MandatoryUser.WSREADER
boolean wsreaderNotDefaultPassword = PredefinedUsers.WSREADER
.hasChangedDefaultPasswordOrDisabled();
boolean wswriterNotDefaultPassword = MandatoryUser.WSWRITER
boolean wswriterNotDefaultPassword = PredefinedUsers.WSWRITER
.hasChangedDefaultPasswordOrDisabled();
boolean wssubcontractingNotDefaultPassword = MandatoryUser.WSSUBCONTRACTING
boolean wssubcontractingNotDefaultPassword = PredefinedUsers.WSSUBCONTRACTING
.hasChangedDefaultPasswordOrDisabled();
boolean managerNotDefaultPassword = MandatoryUser.MANAGER
boolean managerNotDefaultPassword = PredefinedUsers.MANAGER
.hasChangedDefaultPasswordOrDisabled();
boolean hresourcesNotDefaultPassword = MandatoryUser.HRESOURCES
boolean hresourcesNotDefaultPassword = PredefinedUsers.HRESOURCES
.hasChangedDefaultPasswordOrDisabled();
boolean outsourcingNotDefaultPassword = MandatoryUser.OUTSOURCING
boolean outsourcingNotDefaultPassword = PredefinedUsers.OUTSOURCING
.hasChangedDefaultPasswordOrDisabled();
boolean reportsNotDefaultPassword = MandatoryUser.REPORTS
boolean reportsNotDefaultPassword = PredefinedUsers.REPORTS
.hasChangedDefaultPasswordOrDisabled();
Clients.evalJavaScript("showOrHideDefaultPasswordWarnings("

View file

@ -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
@ -35,17 +35,19 @@ import org.libreplan.business.users.entities.Profile;
import org.libreplan.business.users.entities.UserRole;
/**
* It enumerates the mandatory users (usernames) for running the application.<br />
* It enumerates the default users (usernames) for the application.<br />
*
* <code>ADMIN</code> user will be always enabled, however <code>USER</code>,
* <code>WSREADER</code> and <code>WSWRITER</code> could be disabled in
* copilation time with a Maven option specified via {@link Configuration}
* class.
* {@link PredefinedUsers#ADMIN} user will be always enabled, however
* {@link PredefinedUsers#WSREADER}, {@link PredefinedUsers#WSWRITER},
* {@link PredefinedUsers#WSSUBCONTRACTING}, {@link PredefinedUsers#MANAGER},
* {@link PredefinedUsers#HRESOURCES}, {@link PredefinedUsers#OUTSOURCING} and
* {@link PredefinedUsers#REPORTS} users could be disabled in compilation time
* with a Maven option specified via {@link Configuration} class.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
public enum MandatoryUser {
public enum PredefinedUsers {
ADMIN(Arrays.asList(UserRole.ROLE_SUPERUSER,
UserRole.ROLE_READ_ALL_PROJECTS,
@ -117,8 +119,8 @@ public enum MandatoryUser {
}
public static boolean someKeepsDefaultPassword(
Collection<MandatoryUser> mandatoryUsers) {
for (MandatoryUser each : mandatoryUsers) {
Collection<PredefinedUsers> mandatoryUsers) {
for (PredefinedUsers each : mandatoryUsers) {
if (!each.hasChangedDefaultPasswordOrDisabled()) {
return true;
}
@ -137,12 +139,12 @@ public enum MandatoryUser {
private final boolean userDisabled;
private MandatoryUser(Collection<UserRole> initialUserRoles,
private PredefinedUsers(Collection<UserRole> initialUserRoles,
boolean userDisabled) {
this(initialUserRoles, null, userDisabled);
}
private MandatoryUser(Collection<UserRole> initialUserRoles,
private PredefinedUsers(Collection<UserRole> initialUserRoles,
Collection<Profile> initialProfiles, boolean userDisabled) {
if (initialUserRoles != null) {
this.initialRoles = new HashSet<UserRole>(initialUserRoles);
@ -179,7 +181,7 @@ public enum MandatoryUser {
return initialProfiles;
}
public static EnumSet<MandatoryUser> allExcept(MandatoryUser mandatoryUser) {
public static EnumSet<PredefinedUsers> allExcept(PredefinedUsers mandatoryUser) {
return EnumSet.complementOf(EnumSet.of(mandatoryUser));
}

View file

@ -29,7 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
* Bootstrapt to create the default {@link User}s.
* Bootstrapt to create the default {@link User Users}.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
* @author Manuel Rego Casasnovas <rego@igalia.com>
@ -53,25 +53,20 @@ public class UsersBootstrapInDB implements IUsersBootstrapInDB {
@Override
public void loadRequiredData() {
for (MandatoryUser u : MandatoryUser.values()) {
createUserIfNotExists(u);
if (userDAO.list(User.class).isEmpty()) {
for (PredefinedUsers u : PredefinedUsers.values()) {
User user = User.create(u.getLoginName(),
getEncodedPassword(u), u.getInitialRoles(),
u.getInitialProfiles());
user.setDisabled(u.isUserDisabled());
userDAO.save(user);
}
}
}
private void createUserIfNotExists(MandatoryUser u) {
if (!userDAO.existsByLoginName(u.getLoginName())) {
User user = User.create(u.getLoginName(), getEncodedPassword(u),
u.getInitialRoles(), u.getInitialProfiles());
user.setDisabled(u.isUserDisabled());
userDAO.save(user);
}
}
private String getEncodedPassword(MandatoryUser u) {
private String getEncodedPassword(PredefinedUsers u) {
return dbPasswordEncoderService.encodePassword(u.getClearPassword(),
u.getLoginName());

View file

@ -35,7 +35,7 @@ import org.libreplan.business.users.bootstrap.IProfileBootstrap;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
@ -79,7 +79,7 @@ public class UsersBootstrapInDBTest {
usersBootstrap.loadRequiredData();
for (MandatoryUser u : MandatoryUser.values()) {
for (PredefinedUsers u : PredefinedUsers.values()) {
User user = userDAO.findByLoginName(u.getLoginName());

View file

@ -34,7 +34,7 @@ import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.libreplan.web.users.services.IDBPasswordEncoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
@ -68,7 +68,7 @@ public class DBPasswordEncoderServiceTest {
usersBootstrap.loadRequiredData();
for (MandatoryUser u : MandatoryUser.values()) {
for (PredefinedUsers u : PredefinedUsers.values()) {
String encodedPassword = dbPasswordEncoderService.encodePassword(
u.getClearPassword(), u.getLoginName());

View file

@ -40,7 +40,7 @@ import org.libreplan.business.scenarios.bootstrap.IScenariosBootstrap;
import org.libreplan.business.users.entities.Profile;
import org.libreplan.business.users.entities.UserRole;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
@ -95,7 +95,7 @@ public class DBUserDetailsServiceTest {
public void testLoadUserByUsername() {
usersBootstrap.loadRequiredData();
for (MandatoryUser u : MandatoryUser.values()) {
for (PredefinedUsers u : PredefinedUsers.values()) {
UserDetails userDetails = userDetailsService.loadUserByUsername(u
.getLoginName());
@ -107,7 +107,7 @@ public class DBUserDetailsServiceTest {
}
private Object getUserRoles(MandatoryUser u) {
private Object getUserRoles(PredefinedUsers u) {
Set<UserRole> userRoles = new HashSet<UserRole>();
userRoles.addAll(u.getInitialRoles());

View file

@ -38,7 +38,7 @@ import org.libreplan.business.scenarios.bootstrap.IScenariosBootstrap;
import org.libreplan.business.users.entities.Profile;
import org.libreplan.business.users.entities.UserRole;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
import org.libreplan.web.users.bootstrap.PredefinedUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
@ -98,7 +98,7 @@ public class LDAPUserDetailsServiceTest {
public void testLoadUserByUsername() {
usersBootstrap.loadRequiredData();
for (MandatoryUser u : MandatoryUser.values()) {
for (PredefinedUsers u : PredefinedUsers.values()) {
UserDetails userDetails = userDetailsService.loadUserByUsername(u
.getLoginName());
@ -111,7 +111,7 @@ public class LDAPUserDetailsServiceTest {
}
private Object getUserRoles(MandatoryUser u) {
private Object getUserRoles(PredefinedUsers u) {
Set<UserRole> userRoles = new HashSet<UserRole>();
userRoles.addAll(u.getInitialRoles());

View file

@ -85,7 +85,8 @@
<hibernate.use_sql_comments>true</hibernate.use_sql_comments>
<hibernate.hbm2ddl.auto>validate</hibernate.hbm2ddl.auto>
<!-- Enable example users (user, wsreader and wswriter) -->
<!-- Enable example users (wsreader, wswriter, wssubcontracting,
manager, hresources, outsourcing and reports) -->
<default.exampleUsersDisabled>false</default.exampleUsersDisabled>
</properties>
</profile>