Incorporate password and mail to settings window for each user

FEA: ItEr75S07UserSettings
This commit is contained in:
Cristina Alvarino 2011-07-04 10:55:24 +02:00 committed by Manuel Rego Casasnovas
parent 8cdaf4e503
commit d39e59effa
6 changed files with 239 additions and 44 deletions

View file

@ -0,0 +1,78 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 ComtecSF, 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.users;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.users.entities.User;
import org.navalplanner.web.users.bootstrap.MandatoryUser;
/**
* A class which is used to encapsulate some common behaviour of passwords.
*
* @author Cristina Alvarino Perez <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
public class PasswordUtil {
private String clearNewPassword;
public void checkIfChangeDefaultPasswd(User user) {
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.ADMIN.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.ADMIN);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.USER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.USER);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSREADER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSREADER);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSWRITER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSWRITER);
return;
}
}
private void checkIfChangeDefaultPasswd(MandatoryUser user) {
boolean changedPasswd = true;
if (getClearNewPassword().isEmpty()
|| getClearNewPassword().equals(user.getClearPassword())) {
changedPasswd = false;
}
// save the field changedDefaultAdminPassword in configuration.
Registry.getConfigurationDAO().saveChangedDefaultPassword(
user.getLoginName(), changedPasswd);
}
public void setClearNewPassword(String clearNewPassword) {
this.clearNewPassword = clearNewPassword;
}
public String getClearNewPassword() {
return clearNewPassword;
}
}

View file

@ -50,7 +50,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/users/users.zul")
public class UserModel implements IUserModel {
public class UserModel extends PasswordUtil implements IUserModel {
@Autowired
private IUserDAO userDAO;
@ -95,7 +95,7 @@ public class UserModel implements IUserModel {
* changedDefaultAdminPassword.
*/
if (Configuration.isDefaultPasswordsControl()) {
checkIfChangeDefaultPasswd();
checkIfChangeDefaultPasswd(user);
}
user.setPassword(dbPasswordEncoderService.encodePassword(
@ -108,40 +108,6 @@ public class UserModel implements IUserModel {
userDAO.save(user);
}
private void checkIfChangeDefaultPasswd() {
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.ADMIN.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.ADMIN);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.USER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.USER);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSREADER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSREADER);
return;
}
if (user.getLoginName().equalsIgnoreCase(
MandatoryUser.WSWRITER.getLoginName())) {
checkIfChangeDefaultPasswd(MandatoryUser.WSWRITER);
return;
}
}
private void checkIfChangeDefaultPasswd(MandatoryUser user) {
boolean changedPasswd = true;
if (getClearNewPassword().isEmpty()
|| getClearNewPassword().equals(user.getClearPassword())) {
changedPasswd = false;
}
// save the field changedDefaultAdminPassword in configuration.
Registry.getConfigurationDAO().saveChangedDefaultPassword(
user.getLoginName(), changedPasswd);
}
@Override
public User getUser() {
return user;

View file

@ -58,4 +58,20 @@ public interface ISettingsModel {
String getFirstName();
String getEmail();
void setEmail(String email);
void setLoginName(String loginName);
String getLoginName();
/**
* Sets the password attribute to the inner {@ link User} object.
*
* @param password String with the <b>unencrypted</b> password.
*/
void setPassword(String password);
String getClearNewPassword();
}

View file

@ -21,17 +21,17 @@ package org.navalplanner.web.users.settings;
import static org.navalplanner.web.I18nHelper._;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.entities.Language;
import org.navalplanner.web.common.ConfigurationController;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MessagesForUser;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Textbox;
/**
* Controller for user settings
@ -41,16 +41,14 @@ import org.zkoss.zul.ListitemRenderer;
*/
public class SettingsController extends GenericForwardComposer {
private static final Log LOG = LogFactory
.getLog(ConfigurationController.class);
private IMessagesForUser messages;
private Component messagesContainer;
private ISettingsModel settingsModel;
private Textbox password;
public static ListitemRenderer languagesRenderer = new ListitemRenderer() {
@Override
public void render(org.zkoss.zul.Listitem item, Object data)
@ -142,4 +140,41 @@ public class SettingsController extends GenericForwardComposer {
settingsModel.setLastName(lastName);
}
/**
* Tells the SettingsModel to set the password attribute of the inner
* {@ link User} object.
*
* @param password String with the <b>unencrypted</b> password.
*/
public void setPassword(String password) {
settingsModel.setPassword(password);
}
public Constraint validatePasswordConfirmation() {
return new Constraint() {
@Override
public void validate(Component comp, Object value)
throws WrongValueException {
if(!((String)value).equals(password.getValue())) {
throw new WrongValueException(comp, _("passwords don't match"));
}
}
};
}
public String getLoginName() {
return settingsModel.getLoginName();
}
public void setLoginName(String loginName) {
settingsModel.setLoginName(loginName);
}
public void setEmail(String email) {
settingsModel.setEmail(email);
}
public String getEmail() {
return settingsModel.getEmail();
}
}

View file

@ -16,8 +16,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.users.settings;
import org.apache.commons.lang.Validate;
import org.navalplanner.business.common.Configuration;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.daos.IUserDAO;
@ -27,6 +31,9 @@ import org.navalplanner.business.users.entities.User;
import org.navalplanner.business.users.entities.UserRole;
import org.navalplanner.web.common.concurrentdetection.OnConcurrentModification;
import org.navalplanner.web.security.SecurityUtils;
import org.navalplanner.web.users.PasswordUtil;
import org.navalplanner.web.users.bootstrap.MandatoryUser;
import org.navalplanner.web.users.services.IDBPasswordEncoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
@ -42,13 +49,18 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/settings/settings.zul")
public class SettingsModel implements ISettingsModel {
public class SettingsModel extends PasswordUtil implements ISettingsModel {
@Autowired
private IUserDAO userDAO;
private User user;
private String clearNewPassword;
@Autowired
private IDBPasswordEncoderService dbPasswordEncoderService;
@Override
public Language getApplicationLanguage() {
return user.getApplicationLanguage();
@ -102,9 +114,51 @@ public class SettingsModel implements ISettingsModel {
@Override
@Transactional
public void confirmSave() throws ValidationException {
try {
// user.getLoginName() has to be validated before encoding password,
// because it must exist to perform the encoding
Validate.notEmpty(user.getLoginName());
if (getClearNewPassword() != null) {
/*
* it ckecks if the user password who have admin role has
* changed and if so sets true in the field
* changedDefaultAdminPassword.
*/
if (Configuration.isDefaultPasswordsControl()) {
checkIfChangeDefaultPasswd(user);
}
user.setPassword(dbPasswordEncoderService.encodePassword(
getClearNewPassword(), user.getLoginName()));
}
} catch (IllegalArgumentException e) {
}
user.validate();
userDAO.save(user);
}
@Override
public void setPassword(String password) {
// password is not encrypted right away, because
// user.getLoginName must exist to do that, and we're
// not sure at this point
if (password != "") {
setClearNewPassword(password);
} else {
setClearNewPassword(null);
}
}
public void setClearNewPassword(String clearNewPassword) {
this.clearNewPassword = clearNewPassword;
}
@Override
public String getClearNewPassword() {
return clearNewPassword;
}
@Override
public boolean isExpandCompanyPlanningViewCharts() {
@ -169,4 +223,28 @@ public class SettingsModel implements ISettingsModel {
}
}
@Override
public String getLoginName() {
return user.getLoginName();
}
@Override
public void setLoginName(String loginName) {
if (user != null) {
user.setLoginName(loginName);
}
}
@Override
public void setEmail(String email) {
if (user != null) {
user.setEmail(email);
}
}
@Override
public String getEmail() {
return user.getEmail();
}
}

View file

@ -58,6 +58,28 @@
<textbox id="lastName"
value="@{settingsController.lastName}" width="300px"/>
</row>
<row>
<label value="${i18n:_('Login name')}:" />
<textbox id="loginName"
value="@{settingsController.loginName}" width="300px"
constraint="no empty:${i18n:_('cannot be null or empty')}"/>
</row>
<row>
<label value="${i18n:_('Password')}:" />
<textbox id="password" type="password"
onChange="settingsController.setPassword(self.value);" width="300px"/>
</row>
<row>
<label value="${i18n:_('Password confirmation')}:" />
<textbox id="passwordConfirmation" type="password"
constraint="@{settingsController.validatePasswordConfirmation}"
width="300px"/>
</row>
<row>
<label value="${i18n:_('E-mail')}:" />
<textbox id="email"
value="@{settingsController.email}" width="300px"/>
</row>
</rows>
</grid>
</groupbox>