Blame view

src/main/java/fr/plil/sio/web/mvc/NewUserController.java 1.38 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
322c9502   Julien Cartigny   Ad Spring securit...
3
  import org.springframework.security.access.prepost.PreAuthorize;
aa193b2b   Julien Iguchi-Cartigny   First version
4
5
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
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
  import javax.annotation.Resource;
322c9502   Julien Cartigny   Ad Spring securit...
11
  import javax.validation.Valid;
2070653e   Julien Iguchi-Cartigny   Switching to spri...
12
  
aa193b2b   Julien Iguchi-Cartigny   First version
13
  @Controller
322c9502   Julien Cartigny   Ad Spring securit...
14
  @RequestMapping(value = {"/newUser"})
aa193b2b   Julien Iguchi-Cartigny   First version
15
16
17
  public class NewUserController {
  
      @Resource
2afad654   Julien Iguchi-Cartigny   deal with userForm
18
19
      private UserService userService;
  
322c9502   Julien Cartigny   Ad Spring securit...
20
      @RequestMapping(method = RequestMethod.GET)
aa193b2b   Julien Iguchi-Cartigny   First version
21
      public ModelAndView getNewUserForm() {
322c9502   Julien Cartigny   Ad Spring securit...
22
          return new ModelAndView("newUser", "userForm", new UserForm());
aa193b2b   Julien Iguchi-Cartigny   First version
23
24
      }
  
322c9502   Julien Cartigny   Ad Spring securit...
25
26
27
      @PreAuthorize("hasRole('ROLE_ADMIN')")
      @RequestMapping(method = RequestMethod.POST)
      public String postNewUser(@Valid UserForm userForm, BindingResult result) {
aa193b2b   Julien Iguchi-Cartigny   First version
28
  
322c9502   Julien Cartigny   Ad Spring securit...
29
          boolean present = (userService.findByUsername(userForm.getUsername()) != null);
aa193b2b   Julien Iguchi-Cartigny   First version
30
31
32
33
34
35
36
37
38
  
          if (present) {
              result.rejectValue("username", "new.user.form.present");
          }
  
          if(result.hasErrors()) {
              return "newUser";
          }
  
322c9502   Julien Cartigny   Ad Spring securit...
39
          userService.createUser(userForm.getUsername(), userForm.getPassword());
aa193b2b   Julien Iguchi-Cartigny   First version
40
41
42
43
  
          return "redirect:/";
      }
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
44
45
46
      public void setUserService(UserService userService) {
          this.userService = userService;
      }
aa193b2b   Julien Iguchi-Cartigny   First version
47
  }