This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository wao. See http://git.codelutin.com/wao.git commit 229ead8e0b9204d01f0cba30584cb612d74649ae Author: Brendan Le Ny <bleny@codelutin.com> Date: Tue Mar 31 12:12:53 2015 +0200 Remaniement de askForPasswordReminder pour réutiliser du code au sein de WaoUsersService --- .../src/main/java/fr/ifremer/wao/WaoUtils.java | 5 + .../service/administration/WaoUsersService.java | 109 ++++++++++----------- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java b/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java index 8f07aaf..0f4814e 100644 --- a/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java +++ b/wao-persistence/src/main/java/fr/ifremer/wao/WaoUtils.java @@ -29,6 +29,7 @@ import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.time.DateUtils; import org.nuiton.i18n.I18n; import org.nuiton.util.DateUtil; +import org.nuiton.util.StringUtil; import java.text.DateFormat; import java.text.ParseException; @@ -291,4 +292,8 @@ public class WaoUtils { } return keyWithHighestValue; } + + public static String hashPassword(String password) { + return StringUtil.encodeMD5(password); + } } diff --git a/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/WaoUsersService.java b/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/WaoUsersService.java index 2e31b91..c9bea58 100644 --- a/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/WaoUsersService.java +++ b/wao-services/src/main/java/fr/ifremer/wao/services/service/administration/WaoUsersService.java @@ -25,6 +25,7 @@ import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; +import fr.ifremer.wao.WaoUtils; import fr.ifremer.wao.entity.Boat; import fr.ifremer.wao.entity.Boats; import fr.ifremer.wao.entity.ObsProgram; @@ -44,7 +45,6 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.topia.persistence.TopiaEntities; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaQueryBuilderAddCriteriaOrRunQueryStep; -import org.nuiton.util.StringUtil; import java.util.HashSet; import java.util.List; @@ -183,17 +183,13 @@ public class WaoUsersService extends WaoServiceSupport { } if (newPassword != null) { - String hashedNewPassword = StringUtil.encodeMD5(newPassword); + + String hashedNewPassword = WaoUtils.hashPassword(newPassword); waoUser.setPassword(hashedNewPassword); } if (updateWaoUserCommand.getPasswordStrategy().isGeneratePassword()) { - UserCredentialsEmail userCredentialsEmail = - getEmailService().newUserCredentialsEmail(); - userCredentialsEmail.addTo(waoUser); - userCredentialsEmail.setWaoUser(waoUser); - userCredentialsEmail.setNewPassword(newPassword); - getEmailService().send(userCredentialsEmail); + sendNewPasswordToUser(waoUser, newPassword); } if (waoUser.isUserProfileNotEmpty()) { @@ -204,19 +200,26 @@ public class WaoUsersService extends WaoServiceSupport { } } - WaoUserTopiaDao dao = getWaoUserDao(); - if (waoUser.isPersisted()) { - dao.update(waoUser); - } else { - dao.create(waoUser); - } + getWaoUserDao().save(waoUser); commit(); } + protected void sendNewPasswordToUser(WaoUser waoUser, String newPassword) { + + UserCredentialsEmail userCredentialsEmail = + getEmailService().newUserCredentialsEmail(); + userCredentialsEmail.addTo(waoUser); + userCredentialsEmail.setWaoUser(waoUser); + userCredentialsEmail.setNewPassword(newPassword); + + getEmailService().send(userCredentialsEmail); + + } + protected String newRandomPassword() { - return RandomStringUtils.random(8, true, true); + return RandomStringUtils.random(8, 0, 0, true, true, null, serviceContext.getRandom()); } @@ -249,21 +252,9 @@ public class WaoUsersService extends WaoServiceSupport { public WaoUser authenticate(String login, String clearPassword) throws WrongCredentialsException, NoRoleAttributedException, InactiveWaoUserException { - WaoUserTopiaDao dao = getWaoUserDao(); - - Optional<WaoUser> optionalWaoUser = dao.tryFindByEmailAndFetchCollections(login); - - WaoUser waoUser; - - if ( ! optionalWaoUser.isPresent()) { - if (log.isInfoEnabled()) { - log.info("no such user " + login); - } - throw new WrongCredentialsException(); - } + WaoUser waoUser = findWaoUserByLogin(login, true); - waoUser = optionalWaoUser.get(); - String hashedPassword = StringUtil.encodeMD5(clearPassword); + String hashedPassword = WaoUtils.hashPassword(clearPassword); if ( ! waoUser.getPassword().equals(hashedPassword)) { if (log.isWarnEnabled()) { @@ -272,10 +263,6 @@ public class WaoUsersService extends WaoServiceSupport { throw new WrongCredentialsException(); } - if ( ! waoUser.isActive()) { - throw new InactiveWaoUserException(waoUser); - } - if (waoUser.isUserProfileEmpty()) { throw new NoRoleAttributedException(waoUser); } @@ -284,28 +271,16 @@ public class WaoUsersService extends WaoServiceSupport { } - public void acceptCgu(WaoUser waoUser) { - - waoUser.setCguAccepted(serviceContext.getNow()); - - commit(); - - } - - protected UpdateWaoUserCommand getPasswordRecoveryUpdateWaoUserCommand(WaoUser waoUser) { - UpdateWaoUserCommand updateWaoUserCommand = new UpdateWaoUserCommand(); - updateWaoUserCommand.setWaoUser(waoUser); - updateWaoUserCommand.setPasswordStrategy(UpdateWaoUserCommandPasswordStrategy.GENERATE_NEW_PASSWORD); - updateWaoUserCommand.setPasswordStrategies(UpdateWaoUserCommandPasswordStrategy.getPasswordStrategiesForWaoUserCreation()); - return updateWaoUserCommand; - } + protected WaoUser findWaoUserByLogin(String login, boolean fetchDataForSession) throws WrongCredentialsException, InactiveWaoUserException { - public void askForPasswordReminder(String login) throws WrongCredentialsException, InactiveWaoUserException, NoRoleAttributedException { WaoUserTopiaDao dao = getWaoUserDao(); - Optional<WaoUser> optionalWaoUser = dao.tryFindByEmailAndFetchCollections(login); - - WaoUser waoUser; + Optional<WaoUser> optionalWaoUser; + if (fetchDataForSession) { + optionalWaoUser = dao.tryFindByEmailAndFetchCollections(login); + } else { + optionalWaoUser = dao.forLoginEquals(login).tryFindUnique(); + } if ( ! optionalWaoUser.isPresent()) { if (log.isInfoEnabled()) { @@ -314,18 +289,36 @@ public class WaoUsersService extends WaoServiceSupport { throw new WrongCredentialsException(); } - waoUser = optionalWaoUser.get(); + WaoUser waoUser = optionalWaoUser.get(); if ( ! waoUser.isActive()) { throw new InactiveWaoUserException(waoUser); } - if (waoUser.isUserProfileEmpty()) { - throw new NoRoleAttributedException(waoUser); - } + return waoUser; + } - UpdateWaoUserCommand updateWaoUserCommand = getPasswordRecoveryUpdateWaoUserCommand(waoUser); + public void acceptCgu(WaoUser waoUser) { + + waoUser.setCguAccepted(serviceContext.getNow()); + + commit(); + + } + + public void askForPasswordReminder(String login) throws WrongCredentialsException, InactiveWaoUserException { + + WaoUser waoUser = findWaoUserByLogin(login, false); + + String newPassword = newRandomPassword(); + String hashedNewPassword = WaoUtils.hashPassword(newPassword); + waoUser.setPassword(hashedNewPassword); + + sendNewPasswordToUser(waoUser, newPassword); + + getWaoUserDao().save(waoUser); + commit(); - save(updateWaoUserCommand); } + } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.