package stopheracleum.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 stopheracleum.model.User;
import stopheracleum.security.SecurityService;
import stopheracleum.service.UserService;
import stopheracleum.validator.UserValidator;

/**
 * Controller for {@link stopheracleum.model.User}'s pages.
 */

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(Model model) {
        model.addAttribute("userForm", new User());

        return "registration";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
        userValidator.validate(userForm, bindingResult);

        if (bindingResult.hasErrors()) {
            return "registration";
        }

        userService.save(userForm);

        securityService.autoLogin(userForm.getUsername(), userForm.getConfirmPassword());

        return "redirect:/welcome";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String error, String logout) {
        if (error != null) {
            model.addAttribute("error", "Username or password is incorrect.");
        }

        if (logout != null) {
            model.addAttribute("message", "Logged out successfully.");
        }

        return "login";
    }

    @RequestMapping(value = {"/welcome"}, method = RequestMethod.GET)
    public String welcome(Model model) {
        return "welcome";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String admin(Model model) {
        return "admin";
    }
}

@@ -91,16 +91,226 @@
 owired;%0A
+import org.springframework.security.authentication.AnonymousAuthenticationToken;%0Aimport org.springframework.security.core.Authentication;%0Aimport org.springframework.security.core.context.SecurityContextHolder;%0A
 import o
@@ -347,24 +347,24 @@
 Controller;%0A
-
 import org.s
@@ -1192,32 +1192,226 @@
 (Model model) %7B%0A
+%0A        Authentication auth = SecurityContextHolder.getContext().getAuthentication();%0A        if (!(auth instanceof AnonymousAuthenticationToken)) %7B%0A            return %22redirect:/%22;%0A        %7D%0A%0A
         model.ad
@@ -1976,23 +1976,19 @@
 direct:/
-welcome
+map
 %22;%0A    %7D
@@ -2047,32 +2047,32 @@
 uestMethod.GET)%0A
-
     public Strin
@@ -2119,24 +2119,218 @@
 g logout) %7B%0A
+%0A        Authentication auth = SecurityContextHolder.getContext().getAuthentication();%0A        if (!(auth instanceof AnonymousAuthenticationToken)) %7B%0A            return %22redirect:/%22;%0A        %7D%0A%0A
         if (

Add redirect from login and registration pages for logged in users