Blame view

src/main/java/fr/plil/sio/web/mvc/NewUserController.java 2.04 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
2afad654   Julien Iguchi-Cartigny   deal with userForm
5
  import org.springframework.web.bind.annotation.ModelAttribute;
aa193b2b   Julien Iguchi-Cartigny   First version
6
7
8
9
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
10
11
  import javax.annotation.Resource;
  
aa193b2b   Julien Iguchi-Cartigny   First version
12
13
14
15
  @Controller
  public class NewUserController {
  
      @Resource
8efec875   Julien Iguchi-Cartigny   switch to spring ...
16
17
      private UserRepository userRepository;
  
aa193b2b   Julien Iguchi-Cartigny   First version
18
      @Resource
2afad654   Julien Iguchi-Cartigny   deal with userForm
19
20
21
      private UserService userService;
  
      @Resource
aa193b2b   Julien Iguchi-Cartigny   First version
22
      private UserSession userSession;
8efec875   Julien Iguchi-Cartigny   switch to spring ...
23
  
aa193b2b   Julien Iguchi-Cartigny   First version
24
      @Resource
0399d385   Julien Iguchi-Cartigny   Adding user form
25
      private UserFormValidator userFormValidator;
aa193b2b   Julien Iguchi-Cartigny   First version
26
27
28
29
30
31
32
  
      @RequestMapping(value = {"/newUser"}, method = RequestMethod.GET)
      public ModelAndView getNewUserForm() {
          return new ModelAndView("newUser", "user", new User());
      }
  
      @RequestMapping(value = {"/newUser"}, method = RequestMethod.POST)
2afad654   Julien Iguchi-Cartigny   deal with userForm
33
34
      public String postNewUser(@ModelAttribute("userForm") UserForm user,
                                BindingResult result) {
aa193b2b   Julien Iguchi-Cartigny   First version
35
36
37
38
39
  
          if (!userSession.getUsername().equals("admin")) {
              result.rejectValue("username", "new.user.only.admin");
          }
  
0399d385   Julien Iguchi-Cartigny   Adding user form
40
          userFormValidator.validate(user, result);
aa193b2b   Julien Iguchi-Cartigny   First version
41
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
42
          boolean present = (userRepository.findByUsername(user.getUsername()) != null);
aa193b2b   Julien Iguchi-Cartigny   First version
43
44
45
46
47
48
49
50
51
  
          if (present) {
              result.rejectValue("username", "new.user.form.present");
          }
  
          if(result.hasErrors()) {
              return "newUser";
          }
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
52
          userService.createUser(user.getUsername(), user.getPassword());
aa193b2b   Julien Iguchi-Cartigny   First version
53
54
55
56
  
          return "redirect:/";
      }
  
8efec875   Julien Iguchi-Cartigny   switch to spring ...
57
58
      public void setUserRepository(UserRepository userRepository) {
          this.userRepository = userRepository;
aa193b2b   Julien Iguchi-Cartigny   First version
59
60
61
62
63
64
      }
  
      public void setUserSession(UserSession userSession) {
          this.userSession = userSession;
      }
  
0399d385   Julien Iguchi-Cartigny   Adding user form
65
66
      public void setUserFormValidator(UserFormValidator userFormValidator) {
          this.userFormValidator = userFormValidator;
aa193b2b   Julien Iguchi-Cartigny   First version
67
      }
2afad654   Julien Iguchi-Cartigny   deal with userForm
68
69
70
71
  
      public void setUserService(UserService userService) {
          this.userService = userService;
      }
aa193b2b   Julien Iguchi-Cartigny   First version
72
  }