From a0ebc8343eff20d1b231090095bac8c747368b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Fri, 18 Dec 2009 10:50:49 +0100 Subject: [PATCH] ItEr39S10CUAltaUsuario: implemented the password field in user edition page If the password field is left empty when *editing* a user, its password remains unchanged. If it's left empty when *creating* a user, the business layer will complain. TODO: make this behaviour explicit for the user (label with instructions, ConstraintChecker when creating a user...) --- .../navalplanner/web/users/IUserModel.java | 7 ++++ .../web/users/UserCRUDController.java | 10 ++++++ .../org/navalplanner/web/users/UserModel.java | 32 +++++++++++++++++++ .../src/main/webapp/users/_editUser.zul | 5 +++ 4 files changed, 54 insertions(+) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/IUserModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/IUserModel.java index 64f07f013..cea2ee934 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/IUserModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/IUserModel.java @@ -112,4 +112,11 @@ public interface IUserModel { */ void addProfile(Profile profile); + /** + * Sets the password attribute to the inner {@ link User} object. + * + * @param password String with the unencrypted password. + */ + void setPassword(String password); + } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserCRUDController.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserCRUDController.java index 04a9bb6eb..9e459ef06 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserCRUDController.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserCRUDController.java @@ -189,6 +189,16 @@ public class UserCRUDController extends GenericForwardComposer implements Util.reloadBindings(createWindow); } + /** + * Tells the XXXModel to set the password attribute of the inner + * {@ link User} object. + * + * @param password String with the unencrypted password. + */ + public void setPassword(String password) { + userModel.setPassword(password); + } + private OnlyOneVisible getVisibility() { return (visibility == null) ? new OnlyOneVisible(createWindow, listWindow) diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserModel.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserModel.java index e692199e8..0442e3c8b 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserModel.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/users/UserModel.java @@ -30,6 +30,7 @@ import org.navalplanner.business.users.daos.IUserDAO; import org.navalplanner.business.users.entities.Profile; import org.navalplanner.business.users.entities.User; import org.navalplanner.business.users.entities.UserRole; +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; @@ -48,8 +49,13 @@ public class UserModel implements IUserModel { @Autowired private IUserDAO userDAO; + @Autowired + private IDBPasswordEncoderService dbPasswordEncoderService; + private User user; + private String clearNewPassword; + @Override @Transactional(readOnly=true) public List getUsers() { @@ -59,6 +65,19 @@ public class UserModel implements IUserModel { @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 (clearNewPassword != null) { + user.setPassword(dbPasswordEncoderService. + encodePassword(clearNewPassword, user.getLoginName())); + } + } + catch (IllegalArgumentException e) {} + + user.validate(); userDAO.save(user); } @@ -147,4 +166,17 @@ public class UserModel implements IUserModel { public void addProfile(Profile profile) { user.addProfile(profile); } + + @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 != "") { + clearNewPassword = password; + } + else{ + clearNewPassword = null; + } + } } diff --git a/navalplanner-webapp/src/main/webapp/users/_editUser.zul b/navalplanner-webapp/src/main/webapp/users/_editUser.zul index d4adf279e..fe168be7e 100644 --- a/navalplanner-webapp/src/main/webapp/users/_editUser.zul +++ b/navalplanner-webapp/src/main/webapp/users/_editUser.zul @@ -43,6 +43,11 @@ value="@{controller.user.loginName}" width="300px" constraint="no empty:${i18n:_('cannot be null or empty')}"/> + +