Commit 28861c41 authored by ourfbht's avatar ourfbht
Browse files

close #19

parent c410295f
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
package com.example.demo;

import com.example.demo.model.User;
import com.example.demo.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component
public class UserRegisterValidator implements Validator{

    @Autowired
    private IUserService userService;

    private static final String REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";

    @Override
    public boolean supports(Class<?> arg0) {
        return User.class.equals(arg0);
    }

    @Override
    public void validate(Object oUser, Errors errors) {
        User user = (User) oUser;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "username.empty", "You must enter a username!");
        if (user.getUsername().length() < 4 || user.getUsername().length() > 16) {
            errors.rejectValue("username", "username.size", "The username length must be between 4 and 16!");
        }

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.empty", "Password can't be empty!");
        if (user.getPassword().length() < 5 || user.getPassword().length() > 32) {
            errors.rejectValue("password", "password.length", "The password length must be between 5 and 32!");
        }

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.empty", "You must enter an email!");
        if (!(user.getEmail()).matches(REGEX)) {
            errors.rejectValue("email", "email.structure", "This is not a valid email!");
        }

        if (userService.findByEmail(user.getEmail()) != null) {
            errors.rejectValue("email", "email.dupplicate", "This email has already been used!");
        }
    
    }

}
 No newline at end of file
+11 −5
Original line number Diff line number Diff line
@@ -2,11 +2,11 @@ package com.example.demo.controller;

import java.security.Principal;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;
@@ -17,6 +17,7 @@ import com.example.demo.repository.HelpPropositionRepository;
import com.example.demo.repository.HelpRequestRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.IUserService;
import com.example.demo.UserRegisterValidator;

@Controller
public class UserController {
@@ -33,6 +34,9 @@ public class UserController {
    @Autowired 
    UserRepository userRepository;

    @Autowired
    private UserRegisterValidator userRegisterValidator;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView register() {
        ModelAndView modelAndView = new ModelAndView();
@@ -43,24 +47,26 @@ public class UserController {
    }

    @RequestMapping(value="/register", method=RequestMethod.POST)
    public ModelAndView registerUser(@Valid User user, @RequestParam(defaultValue = "false") boolean poulain, @RequestParam(defaultValue = "false") boolean mentor, BindingResult bindingResult, ModelMap modelMap) {
    public ModelAndView registerUser(@ModelAttribute("user") User user, @RequestParam(defaultValue = "false") boolean poulain, @RequestParam(defaultValue = "false") boolean mentor, BindingResult bindingResult, ModelMap modelMap) {
        ModelAndView modelAndView = new ModelAndView();

        //userSignupValidator.validate(o, errors);
        userRegisterValidator.validate(user, bindingResult);
        
        if(bindingResult.hasErrors()) { 
            modelAndView.addObject("registerMessage", "Registration failed: correct the fields !");
            modelMap.addAttribute("bindingResult", bindingResult);
            System.out.println("here");
        }
        else { // Saving the users
            System.out.println("gere");
            if (poulain) {
                userService.save(user, "poulain");
            }
            if(mentor){
                userService.save(user, "mentor");
            }
            //securityService.autoLogin(user.getEmail(), user.getPassword());
            return new ModelAndView("redirect:" + "/");

            return new ModelAndView("redirect:" + "/login");
        }

        modelAndView.addObject("user", new User());
+5 −13
Original line number Diff line number Diff line
@@ -12,26 +12,18 @@

    <body style="background-color:#ededed;">
        <header th:insert="fragments/nav.html :: nav"> </header>
        <div class="container-fluid">
            <div class="row col-lg-4 col-lg-offset-4" style="margin-top: 80px;background-color:#fff;padding:20px;border:solid 1px #ddd;">
                <span th:utext="${registerMessage}"></span>
                <div class="alert alert-success alert-dismissible col-sm-12"
                    th:if="${(bindingResult != null && bindingResult.getAllErrors() != null)}">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <ul th:each="data : ${bindingResult.getAllErrors()}">
                        <li th:text="${data.getObjectName() + ' :: ' + data.getDefaultMessage()}"></li>
                    </ul>
                </div>
        <div class="container">
            <div class="row">
                <form th:action="@{/login}" method="POST" class="form-signin">
                    <h3 class="form-signin-heading" th:text="Login"></h3>
                    <br/>
                    <div class="card-panel red lighten-4 red-text text-darken-4" th:if="${param.error}">
                        <p>Email or Password is invalid</p>
                    </div>
                    <input type="text" id="email" name="email" th:placeholder="Email" class="form-control" required /> 
                    <br/>
                    <input type="password" th:placeholder="Password" id="password" name="password" class="form-control" required />
                    <br/>
                    <div align="center" th:if="${param.error}">
                        <p style="font-size: 20; color: #FF1C19;">Email or Password is invalid.</p>
                    </div>
                    <button class="btn btn-lg btn-primary btn-block" name="Submit" value="Login" type="Submit" th:text="Login" style="margin-right:10px;"></button>
                </form>
                <br>
+14 −15
Original line number Diff line number Diff line
@@ -24,24 +24,32 @@

        <header th:insert="fragments/nav.html :: nav"> </header>

        <div class="container-fluid" style="margin-top: 30px;">

            <div class="row col-lg-4 col-lg-offset-4" style="margin-top: 40px; background-color: #fff; padding: 20px; border: solid 1px #ddd;">
        <div class="container">
            <div class="row">
                <form autocomplete="off" action="#" th:action="@{/register}" th:object="${user}" method="post" class="form-signin" role="form">
                    <h3 class="form-signin-heading">Registration Form</h3>
                    <div class="card-panel red lighten-4 red-text text-darken-4" th:if="${(bindingResult != null && bindingResult.getAllErrors() != null)}">
                        <span th:utext="${registerMessage}"></span>
                        <ul th:each="data : ${bindingResult.getAllErrors()}">
                            <li th:text="${data.getDefaultMessage()}"></li>
                        </ul>
                    </div>
                    <div class="form-group">
                        <div class="">
                            <input type="text" th:field="*{username}" placeholder="Username" class="form-control" required />
                            <label for="username">Username</label>
                            <input type="text" th:field="*{username}" id="username" name="username" placeholder="Username" class="form-control" pattern=".{4,16}" required title="4 to 16 characters" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="">
                            <input type="text" th:field="*{email}" placeholder="Email" class="form-control" required />
                            <label for="email">Email</label>
                            <input type="email" th:field="*{email}" id="email" name="email" placeholder="Email" class="form-control" required />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="">
                            <input type="password" th:field="*{password}" placeholder="Password" class="form-control" required />
                            <label for="password">Password</label>
                            <input type="password" th:field="*{password}" id="password" name="password" placeholder="Password" class="form-control" pattern=".{5,32}" required title="5 to 32 characters" />
                        </div>
                    </div>
                    <div class="form-check">
@@ -61,15 +69,6 @@
                            <button type="submit" class="btn btn-primary btn-lg btn-block" id="submitBtn">Register User</button>
                        </div>
                    </div>
                    <span th:utext="${registerMessage}"></span>
                    <div class="alert alert-success alert-dismissible col-sm-12"
                        th:if="${(bindingResult != null && bindingResult.getAllErrors() != null)}">
                        <button type="button" class="close" data-dismiss="alert"
                            aria-hidden="true">×</button>
                        <ul th:each="data : ${bindingResult.getAllErrors()}">
                            <li th:text="${data.getObjectName() + ' :: ' + data.getDefaultMessage()}"></li>
                        </ul>
                    </div>
                </form>
            </div>
        </div>