Blame view

src/main/java/fr/plil/sio/web/mvc/NewUserController.java 1.61 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;
5ab9e3a3   jcartign   Add support for d...
6
7
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.annotation.InitBinder;
aa193b2b   Julien Iguchi-Cartigny   First version
8
9
10
11
  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...
12
  import javax.annotation.Resource;
322c9502   Julien Cartigny   Ad Spring securit...
13
  import javax.validation.Valid;
2070653e   Julien Iguchi-Cartigny   Switching to spri...
14
  
aa193b2b   Julien Iguchi-Cartigny   First version
15
  @Controller
322c9502   Julien Cartigny   Ad Spring securit...
16
  @RequestMapping(value = {"/newUser"})
aa193b2b   Julien Iguchi-Cartigny   First version
17
18
19
  public class NewUserController {
  
      @Resource
2afad654   Julien Iguchi-Cartigny   deal with userForm
20
21
      private UserService userService;
  
5ab9e3a3   jcartign   Add support for d...
22
23
24
25
26
      @InitBinder
      protected void initBinder(WebDataBinder binder) {
          binder.addValidators(new PasswordValidator());
      }
  
322c9502   Julien Cartigny   Ad Spring securit...
27
      @RequestMapping(method = RequestMethod.GET)
aa193b2b   Julien Iguchi-Cartigny   First version
28
      public ModelAndView getNewUserForm() {
322c9502   Julien Cartigny   Ad Spring securit...
29
          return new ModelAndView("newUser", "userForm", new UserForm());
aa193b2b   Julien Iguchi-Cartigny   First version
30
31
      }
  
322c9502   Julien Cartigny   Ad Spring securit...
32
33
34
      @PreAuthorize("hasRole('ROLE_ADMIN')")
      @RequestMapping(method = RequestMethod.POST)
      public String postNewUser(@Valid UserForm userForm, BindingResult result) {
aa193b2b   Julien Iguchi-Cartigny   First version
35
  
322c9502   Julien Cartigny   Ad Spring securit...
36
          boolean present = (userService.findByUsername(userForm.getUsername()) != null);
aa193b2b   Julien Iguchi-Cartigny   First version
37
38
39
40
41
42
43
44
45
  
          if (present) {
              result.rejectValue("username", "new.user.form.present");
          }
  
          if(result.hasErrors()) {
              return "newUser";
          }
  
322c9502   Julien Cartigny   Ad Spring securit...
46
          userService.createUser(userForm.getUsername(), userForm.getPassword());
aa193b2b   Julien Iguchi-Cartigny   First version
47
48
49
50
  
          return "redirect:/";
      }
  
2afad654   Julien Iguchi-Cartigny   deal with userForm
51
52
53
      public void setUserService(UserService userService) {
          this.userService = userService;
      }
4abe2e3d   Julien Cartigny   Add @Transactionn...
54
  }