Commit 3e2e07f6 authored by Aleksandr Sharaban's avatar Aleksandr Sharaban Committed by Matija Obreza
Browse files

New account registration - with Spring forms

parent 60a14e8f
......@@ -21,6 +21,7 @@ import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import com.mysql.jdbc.StringUtils;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.LoginType;
import org.genesys2.server.model.impl.User;
......@@ -125,12 +126,14 @@ public class HtmlController extends BaseController {
public String registration(ModelMap model) {
model.addAttribute("captchaSiteKey", captchaSiteKey);
model.addAttribute("blurp", contentService.getGlobalArticle("registration", getLocale()));
model.addAttribute("user", new User());
return "/registration";
}
@RequestMapping(value = "new-user")
public String addUser(@ModelAttribute User user, BindingResult bindingResult, HttpServletRequest req,
@RequestParam(value = "g-recaptcha-response", required = false) String response, RedirectAttributes redirectAttributes) throws IOException {
public String addUser(@ModelAttribute("user") User user, BindingResult bindingResult, HttpServletRequest req,
@RequestParam(value = "g-recaptcha-response", required = false) String response, RedirectAttributes redirectAttributes, ModelMap model) throws IOException {
user.getRoles().add(UserRole.USER);
validator.validate(user, bindingResult);
......@@ -138,12 +141,16 @@ public class HtmlController extends BaseController {
// Validate the reCAPTCHA
if (!ReCaptchaUtil.isValid(response, req.getRemoteAddr(), captchaPrivateKey)) {
_logger.warn("Invalid captcha.");
redirectAttributes.addFlashAttribute("error", "errors.badCaptcha");
redirectAttributes.addFlashAttribute("captchaError", "errors.badCaptcha");
return "redirect:/registration.html";
}
try {
if (!bindingResult.hasErrors()) {
String confirmPassword = req.getParameter("confirm_password");
if (!bindingResult.hasErrors()
&& user.getPassword().equals(confirmPassword)
&& !StringUtils.isNullOrEmpty(confirmPassword)) {
if (!userService.exists(user.getEmail())) {
final User newUser = userService.createAccount(user.getEmail(), user.getPassword(), user.getName(), LoginType.PASSWORD);
......@@ -151,21 +158,35 @@ public class HtmlController extends BaseController {
return "redirect:/content/account-created";
} else {
redirectAttributes.addFlashAttribute("error", "registration.user-exists");
model.addAttribute("error", "registration.user-exists");
}
} else {
redirectAttributes.addFlashAttribute("error", "New account form has errors: " + bindingResult.getErrorCount());
_logger.warn("New account form has errors: " + bindingResult.getErrorCount());
int errorCount = bindingResult.getErrorCount();
if (!user.getPassword().equals(confirmPassword)) {
model.addAttribute("passwordRepeatError", "errors.second-password-doesnt-match");
errorCount++;
}
if (StringUtils.isNullOrEmpty(confirmPassword)) {
model.addAttribute("passwordRepeatError", "sample.error.not.empty");
errorCount++;
}
_logger.warn("New account form has errors: " + errorCount);
}
} catch (final PasswordPolicyException e) {
redirectAttributes.addFlashAttribute("error", e.getMessage());
model.addAttribute("passwordError", e.getMessage());
} catch (final Exception e) {
_logger.error(e.getMessage(), e);
// simpleExceptionHandler(e);
redirectAttributes.addFlashAttribute("error", e.getMessage());
model.addAttribute("error", e.getMessage());
}
return "redirect:/registration.html";
model.addAttribute("user", user);
model.addAttribute("captchaSiteKey", captchaSiteKey);
model.addAttribute("blurp", contentService.getGlobalArticle("registration", getLocale()));
return "/registration";
}
@RequestMapping("/access-denied")
......
......@@ -52,8 +52,11 @@ registration.create-account=Create account
captcha.text=Captcha text
errors.badCaptcha=Captcha check failed.
errors.no-such-user=Genesys does not have a user account with provided email address.
errors.second-password-doesnt-match=Second password doesn't match
errors.reset-password.invalid-login-type=Password for users with login type GOOGLE can't be reset!
sample.error.not.empty=Field must not be empty
sample.error.wrong.email=Invalid e-mail format
id=ID
......
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<%@ include file="init.jsp" %>
......@@ -26,29 +27,73 @@
<spring:message code="registration.user-exists" />
</gui:alert>
<form role="form" method="POST" action="new-user.html" class="form-horizontal validate">
<form:form method="POST" action="new-user.html" cssClass="form-horizontal validate" modelAttribute="user">
<div class="form-group">
<label for="email" class="col-lg-2 control-label"><spring:message code="registration.email" /></label>
<form:label for="email" cssClass="col-lg-2 control-label" path="email">
<spring:message code="registration.email" />
</form:label>
<div class="col-lg-3">
<input type="text" id="email" name="email" class="span3 required email form-control" />
<form:input id="email" name="email" cssClass="span3 required email form-control" path="email"/>
</div>
<div class="col-lg-4">
<spring:bind path="email">
<c:if test="${status.error}">
<gui:alert type="danger">
<spring:message code="${status.errorMessage}" />
</gui:alert>
</c:if>
</spring:bind>
</div>
</div>
<div class="form-group">
<label for="password" class="col-lg-2 control-label"><spring:message code="registration.password" /></label>
<form:label for="password" cssClass="col-lg-2 control-label" path="password">
<spring:message code="registration.password" />
</form:label>
<div class="col-lg-3">
<input type="password" id="password" name="password" class="span3 required form-control" />
<form:password id="password" name="password" cssClass="span3 required form-control" path="password"/>
</div>
<div class="col-lg-4">
<spring:bind path="password">
<c:if test="${status.error}">
<gui:alert type="danger">
<spring:message code="${status.errorMessage}" />
</gui:alert>
</c:if>
</spring:bind>
<gui:alert type="danger" display="${not empty passwordError}">
<spring:message code="${passwordError}" />
</gui:alert>
</div>
</div>
<div class="form-group">
<label for="confirm_password" class="col-lg-2 control-label"><spring:message code="registration.confirm-password" /></label>
<label for="confirm_password" class="col-lg-2 control-label">
<spring:message code="registration.confirm-password" />
</label>
<div class="col-lg-3">
<input type="password" id="confirm_password" name="confirm_password" class="span3 required form-control" equalTo="#password" />
</div>
<div class="col-lg-4">
<gui:alert type="danger" display="${not empty passwordRepeatError}">
<spring:message code="${passwordRepeatError}" />
</gui:alert>
</div>
</div>
<div class="form-group">
<label for="name" class="col-lg-2 control-label"><spring:message code="registration.full-name" /></label>
<form:label for="name" cssClass="col-lg-2 control-label" path="name">
<spring:message code="registration.full-name" />
</form:label>
<div class="col-lg-3">
<input type="text" id="name" name="name" class="span3 required form-control" />
<form:input id="name" name="name" cssClass="span3 required form-control" path="name"/>
</div>
<div class="col-lg-4">
<spring:bind path="name">
<c:if test="${status.error}">
<gui:alert type="danger">
<spring:message code="${status.errorMessage}" />
</gui:alert>
</c:if>
</spring:bind>
</div>
</div>
<div class="form-group">
......@@ -56,16 +101,25 @@
<div class="col-lg-3">
<local:captcha siteKey="${captchaSiteKey}" />
</div>
<div class="col-lg-4">
<gui:alert type="danger" display="${not empty captchaError}">
<spring:message code="${captchaError}" />
</gui:alert>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" value="<spring:message code="registration.create-account"/>" class="btn btn-primary" /> <a class="btn btn-default" href="<c:url value="/" />" id="registration" class="btn"> <spring:message code="cancel" />
<form:button class="btn btn-primary">
<spring:message code="registration.create-account"/>
</form:button>
<a class="btn btn-default" href='<c:url value="/" />' id="registration">
<spring:message code="cancel" />
</a>
</div>
</div>
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
</form:form>
<content tag="javascript">
<script type="text/javascript" src="/html/js/login.js"></script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment