Migrate UserRegitration to MVC 3.0.

wisemapping-servlet.xml is not used anymore.
This commit is contained in:
Paulo Gustavo Veiga
2012-06-16 19:27:22 -03:00
parent ea2bff60f7
commit 33131d4e9e
14 changed files with 116 additions and 76 deletions

View File

@@ -42,7 +42,7 @@ public class LoginController {
result = new ModelAndView("forward:/c/maps/");
} else {
result = new ModelAndView("login");
result.addObject("isHsql", driver.indexOf("hsql") != -1);
result.addObject("isHsql", driver.contains("hsql"));
}
return result;
}

View File

@@ -19,11 +19,19 @@
package com.wisemapping.ncontroller;
import com.wisemapping.controller.Messages;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.model.User;
import com.wisemapping.service.InvalidUserEmailException;
import com.wisemapping.service.UserService;
import com.wisemapping.validator.UserValidator;
import com.wisemapping.view.UserBean;
import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
@@ -32,6 +40,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Properties;
@Controller
public class UsersController {
@@ -39,6 +50,16 @@ public class UsersController {
@Autowired
private UserService userService;
@Value("${registration.email.enabled}")
boolean emailConfirmEnabled;
@Autowired
private ReCaptcha captchaService;
@Value("${registration.recaptcha.enabled}")
private boolean captchaEnabled;
@RequestMapping(value = "user/resetPassword", method = RequestMethod.GET)
public ModelAndView showResetPasswordPage() {
return new ModelAndView("forgotPassword");
@@ -59,7 +80,62 @@ public class UsersController {
return result;
}
public void setUserService(@NotNull UserService userService) {
this.userService = userService;
@RequestMapping(value = "user/registration", method = RequestMethod.GET)
public ModelAndView showRegistrationPage(@NotNull HttpServletRequest request) {
if (captchaEnabled) {
// If captcha is enabled, generate it ...
final Properties prop = new Properties();
prop.put("theme", "white");
final String captchaHtml = captchaService.createRecaptchaHtml(null, prop);
request.setAttribute("captchaHtml", captchaHtml);
request.setAttribute("captchaEnabled", true);
}
return new ModelAndView("userRegistration", "user", new UserBean());
}
@RequestMapping(value = "user/registration", method = RequestMethod.POST)
public ModelAndView registerUser(@ModelAttribute("user") UserBean userBean, @NotNull HttpServletRequest request, @NotNull BindingResult bindingResult) throws WiseMappingException {
ModelAndView result;
validateRegistrationForm(userBean, request, bindingResult);
if (bindingResult.hasErrors()) {
result = this.showRegistrationPage(request);
result.addObject("user", userBean);
} else {
final User user = new User();
// trim() the email email in order to remove spaces ...
user.setEmail(userBean.getEmail().trim());
user.setUsername(userBean.getUsername());
user.setFirstname(userBean.getFirstname());
user.setLastname(userBean.getLastname());
user.setPassword(userBean.getPassword());
userService.createUser(user, emailConfirmEnabled);
// Forward to the success view ...
result = new ModelAndView("userRegistrationSuccess");
result.addObject("confirmByEmail", emailConfirmEnabled);
}
return result;
}
private BindingResult validateRegistrationForm(@NotNull UserBean userBean, @NotNull HttpServletRequest request, @NotNull BindingResult bindingResult) {
final UserValidator userValidator = new UserValidator();
userValidator.setUserService(userService);
userValidator.setCaptchaService(captchaService);
userValidator.validate(userBean, bindingResult);
// If captcha is enabled, generate it ...
if (captchaEnabled) {
final String challenge = request.getParameter("recaptcha_challenge_field");
final String uresponse = request.getParameter("recaptcha_response_field");
final String remoteAddr = request.getRemoteAddr();
final ReCaptchaResponse reCaptchaResponse = captchaService.checkAnswer(remoteAddr, challenge, uresponse);
if (!reCaptchaResponse.isValid()) {
bindingResult.rejectValue("captcha", Messages.CAPTCHA_ERROR);
}
}
return bindingResult;
}
}

View File

@@ -22,20 +22,20 @@ import com.wisemapping.model.CollaborationRole;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
public class ColaboratorBean
public class CollaboratorBean
{
private CollaborationRole collaborationRole;
private boolean isUser;
private Collaborator collaborator;
public ColaboratorBean(Collaborator collaborator, CollaborationRole role)
public CollaboratorBean(Collaborator collaborator, CollaborationRole role)
{
this.collaborator = collaborator;
this.collaborationRole = role;
this.isUser = false;
}
public ColaboratorBean(User user, CollaborationRole role)
public CollaboratorBean(User user, CollaborationRole role)
{
this.collaborator = user;
this.collaborationRole = role;

View File

@@ -29,8 +29,8 @@ import java.util.*;
public class MindMapBean {
private MindMap mindMap;
private List<ColaboratorBean> viewers;
private List<ColaboratorBean> colaborators;
private List<CollaboratorBean> viewers;
private List<CollaboratorBean> colaborators;
public MindMapBean(final MindMap mindmap) {
this.mindMap = mindmap;
@@ -62,11 +62,11 @@ public class MindMapBean {
return mindMap.isStarred(Utils.getUser());
}
public List<ColaboratorBean> getViewers() {
public List<CollaboratorBean> getViewers() {
return viewers;
}
public List<ColaboratorBean> getCollaborators() {
public List<CollaboratorBean> getCollaborators() {
return colaborators;
}
@@ -86,12 +86,12 @@ public class MindMapBean {
return mindMap.getTags();
}
private List<ColaboratorBean> getColaboratorBy(Set<Collaboration> source, CollaborationRole role) {
List<ColaboratorBean> col = new ArrayList<ColaboratorBean>();
private List<CollaboratorBean> getColaboratorBy(Set<Collaboration> source, CollaborationRole role) {
List<CollaboratorBean> col = new ArrayList<CollaboratorBean>();
if (source != null) {
for (Collaboration mu : source) {
if (mu.getRole() == role) {
col.add(new ColaboratorBean(mu.getCollaborator(), mu.getRole()));
col.add(new CollaboratorBean(mu.getCollaborator(), mu.getRole()));
}
}
}