- Fix forgot password email

- Rewrite forgot password to use the new MVC model.
- Show a dialog when the password has been sent.
This commit is contained in:
Paulo Gustavo Veiga
2012-06-16 15:59:59 -03:00
parent 8a638e8de6
commit 7d6b82c626
25 changed files with 532 additions and 569 deletions

View File

@@ -1,56 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.controller;
import com.wisemapping.exceptions.EmailNotExistsException;
import com.wisemapping.service.InvalidUserEmailException;
import com.wisemapping.service.UserService;
import com.wisemapping.view.ForgotPasswordBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
public class ForgotPasswordController
extends BaseSimpleFormController
{
//~ Instance fields ......................................................................................
private UserService userService;
//~ Methods ..............................................................................................
public ModelAndView onSubmit(Object command)
throws ServletException, EmailNotExistsException {
final ForgotPasswordBean bean = (ForgotPasswordBean) command;
try {
userService.sendEmailPassword(bean.getEmail());
} catch (InvalidUserEmailException e) {
throw new EmailNotExistsException(e);
}
return new ModelAndView(new RedirectView(getSuccessView()));
}
public void setUserService(UserService userService)
{
this.userService = userService;
}
}

View File

@@ -35,22 +35,18 @@ public final class Mailer {
private JavaMailSender mailSender;
private VelocityEngine velocityEngine;
private String registrationEmail;
private String siteEmail;
private String serverFromEmail;
private String supportEmail;
//~ Methods ..............................................................................................
public Mailer(String registrationEmail, String siteEmail) {
this.registrationEmail = registrationEmail;
this.siteEmail = siteEmail;
public Mailer(@NotNull String siteEmail, @NotNull String supportEmail) {
this.serverFromEmail = siteEmail;
this.supportEmail = supportEmail;
}
public String getRegistrationEmail() {
return registrationEmail;
}
public String getSiteEmail() {
return siteEmail;
public String getServerSenderEmail() {
return serverFromEmail;
}
public void sendEmail(final String from, final String to, final String subject, final Map model,
@@ -79,4 +75,8 @@ public final class Mailer {
public void setVelocityEngine(VelocityEngine engine) {
this.velocityEngine = engine;
}
public String getSupportEmail() {
return supportEmail;
}
}

View File

@@ -43,7 +43,7 @@ final public class NotificationService {
try {
// Sent collaboration email ...
final String formMail = mailer.getSiteEmail();
final String formMail = mailer.getServerSenderEmail();
// Is the user already registered user ?.
final String collabEmail = collaboration.getCollaborator().getEmail();
@@ -69,6 +69,21 @@ final public class NotificationService {
}
public void resetPassword(@NotNull User user, @NotNull String temporalPassword) {
try {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("temporalPassword", temporalPassword);
model.put("supportEmail", mailer.getSupportEmail());
model.put("password", temporalPassword);
model.put("baseUrl", this.baseUrl);
mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "Reset Your WiseMapping Password", model, "passwordRecovery.vm");
} catch (Exception e) {
handleException(e);
}
}
private void handleException(Exception e) {
e.printStackTrace();
}
@@ -82,5 +97,20 @@ final public class NotificationService {
}
public void activateAccount(@NotNull User user) {
final Map<String, User> model = new HashMap<String, User>();
model.put("user", user);
mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "WiseMapping : Active account", model, "activationAccountMail.vm");
}
public void sendRegistrationEmail(@NotNull User user) {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
final String activationUrl = "http://wisemapping.com/c/activation?code=" + user.getActivationCode();
model.put("emailcheck", activationUrl);
mailer.sendEmail(mailer.getServerSenderEmail(), user.getEmail(), "Welcome to Wisemapping!", model,
"confirmationMail.vm");
}
}

View File

@@ -20,6 +20,7 @@ package com.wisemapping.ncontroller;
import com.wisemapping.service.MindmapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -27,6 +28,7 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
public class PublicPagesController {
@Qualifier("mindmapService")
@Autowired
private MindmapService mindmapService;
@@ -57,8 +59,7 @@ public class PublicPagesController {
}
@RequestMapping(value = "iframeWrapper")
public ModelAndView showIframe(@RequestParam(required = true) String url) {
return new ModelAndView("iframeWrapper", "url", url);
}
public ModelAndView showIframe(@RequestParam(required = true) String url) {
return new ModelAndView("iframeWrapper", "url", url);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.ncontroller;
import com.wisemapping.service.InvalidUserEmailException;
import com.wisemapping.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UsersController {
@Qualifier("userService")
@Autowired
private UserService userService;
@RequestMapping(value = "user/resetPassword", method = RequestMethod.GET)
public ModelAndView showResetPasswordPage() {
return new ModelAndView("forgotPassword");
}
@RequestMapping(value = "user/resetPassword", method = RequestMethod.POST)
public ModelAndView resetPassword(@RequestParam(required = true) String email) {
ModelAndView result;
try {
userService.resetPassword(email);
result = new ModelAndView("forgotPasswordSuccess");
} catch (InvalidUserEmailException e) {
result = new ModelAndView("forgotPasswordError");
}
return result;
}
public void setUserService(@NotNull UserService userService) {
this.userService = userService;
}
}

View File

@@ -1,46 +1,46 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.service;
import com.wisemapping.model.User;
import com.wisemapping.exceptions.WiseMappingException;
import org.jetbrains.annotations.NotNull;
public interface UserService {
public void activateAccount(long code) throws InvalidActivationCodeException;
public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException;
public void changePassword(User user);
public User getUserBy(String email);
public User getUserBy(long id);
public User getUserByUsername(String username);
public void updateUser(User user);
public void sendEmailPassword(String email) throws InvalidUserEmailException;
public User reloadUser(final User user);
public void deleteUser(@NotNull User user);
}
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.service;
import com.wisemapping.model.User;
import com.wisemapping.exceptions.WiseMappingException;
import org.jetbrains.annotations.NotNull;
public interface UserService {
public void activateAccount(long code) throws InvalidActivationCodeException;
public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException;
public void changePassword(@NotNull User user);
public User getUserBy(String email);
public User getUserBy(long id);
public User getUserByUsername(String username);
public void updateUser(User user);
public void resetPassword(@NotNull String email) throws InvalidUserEmailException;
public User reloadUser(final User user);
public void deleteUser(@NotNull User user);
}

View File

@@ -1,170 +1,157 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.service;
import com.wisemapping.dao.UserManager;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.mail.Mailer;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class UserServiceImpl
implements UserService {
private UserManager userManager;
private MindmapService mindmapService;
private Mailer mailer;
final static Logger logger = Logger.getLogger("org.wisemapping.service");
public void activateAccount(long code)
throws InvalidActivationCodeException {
final User user = userManager.getUserByActivationCode(code);
if (user == null || user.isActive()) {
throw new InvalidActivationCodeException("Invalid Activation Code");
} else {
final Calendar now = Calendar.getInstance();
user.setActivationDate(now);
userManager.updateUser(user);
final Map<String, User> model = new HashMap<String, User>();
model.put("user", user);
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "WiseMapping : Active account", model, "activationAccountMail.vm");
}
}
public User reloadUser(final User user) {
return this.getUserBy(user.getId());
}
public void sendEmailPassword(String email)
throws InvalidUserEmailException {
final User user = userManager.getUserBy(email);
if (user != null) {
final Map<String, Object> model = new HashMap<String, Object>();
final String password = randomstring(8, 10);
user.setPassword(password);
changePassword(user);
model.put("user", user);
model.put("password", password);
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "WiseMapping : Recovery Password", model, "passwordRecovery.vm");
} else {
throw new InvalidUserEmailException("The email '" + email + "' does not exists.");
}
}
private String randomstring(int lo, int hi) {
int n = rand(lo, hi);
byte b[] = new byte[n];
for (int i = 0; i < n; i++)
b[i] = (byte) rand('@', 'Z');
return new String(b);
}
private int rand(int lo, int hi) {
java.util.Random rn = new java.util.Random();
int n = hi - lo + 1;
int i = rn.nextInt() % n;
if (i < 0)
i = -i;
return lo + i;
}
public void deleteUser(@NotNull User user){
userManager.deleteUser(user);
}
public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException {
final UUID uuid = UUID.randomUUID();
user.setCreationDate(Calendar.getInstance());
user.setActivationCode(uuid.getLeastSignificantBits());
if (emailConfirmEnabled) {
user.setActivationDate(null);
} else {
user.setActivationDate(Calendar.getInstance());
}
Collaborator col = userManager.getCollaboratorBy(user.getEmail());
if (col != null) {
userManager.createUser(user, col);
} else {
userManager.createUser(user);
}
//create welcome map
mindmapService.addWelcomeMindmap(user);
// Send registration email.
if (emailConfirmEnabled) {
sendRegistrationEmail(user);
}
return user;
}
private void sendRegistrationEmail(User user) {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
final String activationUrl = "http://wisemapping.com/c/activation?code=" + user.getActivationCode();
logger.info("create User - acrivationUrl: " + activationUrl);
model.put("emailcheck", activationUrl);
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "Welcome to Wisemapping!", model,
"confirmationMail.vm");
}
public void changePassword(User user) {
userManager.updateUser(user);
}
public User getUserBy(String email) {
return userManager.getUserBy(email);
}
public User getUserByUsername(String username) {
return userManager.getUserByUsername(username);
}
public User getUserBy(long id) {
return userManager.getUserBy(id);
}
public void updateUser(User user) {
userManager.updateUser(user);
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
public void setMailer(Mailer mailer) {
this.mailer = mailer;
}
public void setMindmapService(MindmapService mindmapService) {
this.mindmapService = mindmapService;
}
}
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.service;
import com.wisemapping.dao.UserManager;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.mail.Mailer;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class UserServiceImpl
implements UserService {
private UserManager userManager;
private MindmapService mindmapService;
private NotificationService notificationService;
public void activateAccount(long code)
throws InvalidActivationCodeException {
final User user = userManager.getUserByActivationCode(code);
if (user == null || user.isActive()) {
throw new InvalidActivationCodeException("Invalid Activation Code");
} else {
final Calendar now = Calendar.getInstance();
user.setActivationDate(now);
userManager.updateUser(user);
notificationService.activateAccount(user);
}
}
public User reloadUser(final User user) {
return this.getUserBy(user.getId());
}
public void resetPassword(@NotNull String email)
throws InvalidUserEmailException {
final User user = userManager.getUserBy(email);
if (user != null) {
// Generate a random password ...
final String password = randomstring(8, 10);
user.setPassword(password);
changePassword(user);
// Send an email with the new temporal password ...
notificationService.resetPassword(user, password);
} else {
throw new InvalidUserEmailException("The email '" + email + "' does not exists.");
}
}
private String randomstring(int lo, int hi) {
int n = rand(lo, hi);
byte b[] = new byte[n];
for (int i = 0; i < n; i++)
b[i] = (byte) rand('@', 'Z');
return new String(b);
}
private int rand(int lo, int hi) {
java.util.Random rn = new java.util.Random();
int n = hi - lo + 1;
int i = rn.nextInt() % n;
if (i < 0)
i = -i;
return lo + i;
}
public void deleteUser(@NotNull User user) {
userManager.deleteUser(user);
}
public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException {
final UUID uuid = UUID.randomUUID();
user.setCreationDate(Calendar.getInstance());
user.setActivationCode(uuid.getLeastSignificantBits());
if (emailConfirmEnabled) {
user.setActivationDate(null);
} else {
user.setActivationDate(Calendar.getInstance());
}
Collaborator col = userManager.getCollaboratorBy(user.getEmail());
if (col != null) {
userManager.createUser(user, col);
} else {
userManager.createUser(user);
}
//create welcome map
mindmapService.addWelcomeMindmap(user);
// Send registration email.
if (emailConfirmEnabled) {
notificationService.sendRegistrationEmail(user);
}
return user;
}
public void changePassword(@NotNull User user) {
userManager.updateUser(user);
}
public User getUserBy(String email) {
return userManager.getUserBy(email);
}
public User getUserByUsername(String username) {
return userManager.getUserByUsername(username);
}
public User getUserBy(long id) {
return userManager.getUserBy(id);
}
public void updateUser(@NotNull User user) {
userManager.updateUser(user);
}
public void setUserManager(@NotNull UserManager userManager) {
this.userManager = userManager;
}
public void setMindmapService(@NotNull MindmapService mindmapService) {
this.mindmapService = mindmapService;
}
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.validator;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import com.wisemapping.view.ForgotPasswordBean;
import com.wisemapping.controller.Messages;
public class ForgotPasswordValidator
implements Validator {
public boolean supports(final Class clazz) {
return clazz.equals(ForgotPasswordBean.class);
}
public void validate(Object obj, Errors errors) {
ForgotPasswordBean bean = (ForgotPasswordBean) obj;
if (bean == null) {
errors.rejectValue("forgotPassword", "error.not-specified", null, "Value required.");
} else {
final String email = bean.getEmail();
boolean isValid = Utils.isValidateEmailAddress(email);
if (!isValid) {
errors.rejectValue("email", Messages.NO_VALID_EMAIL_ADDRESS);
}
}
}
}

View File

@@ -1,32 +0,0 @@
/*
* Copyright [2011] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.view;
public class ForgotPasswordBean {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String e) {
this.email = e;
}
}