1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.sprhib.web; |
20 | |
|
21 | |
import org.apache.shiro.authz.annotation.RequiresPermissions; |
22 | |
import org.apache.shiro.samples.sprhib.model.User; |
23 | |
import org.apache.shiro.samples.sprhib.service.UserService; |
24 | |
import org.springframework.beans.factory.annotation.Autowired; |
25 | |
import org.springframework.stereotype.Controller; |
26 | |
import org.springframework.ui.Model; |
27 | |
import org.springframework.util.Assert; |
28 | |
import org.springframework.validation.BindingResult; |
29 | |
import org.springframework.web.bind.annotation.ModelAttribute; |
30 | |
import org.springframework.web.bind.annotation.RequestMapping; |
31 | |
import org.springframework.web.bind.annotation.RequestMethod; |
32 | |
import org.springframework.web.bind.annotation.RequestParam; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
@Controller |
38 | 0 | public class ManageUsersController { |
39 | |
|
40 | 0 | private EditUserValidator editUserValidator = new EditUserValidator(); |
41 | |
|
42 | |
private UserService userService; |
43 | |
|
44 | |
@Autowired |
45 | |
public void setUserService(UserService userService) { |
46 | 0 | this.userService = userService; |
47 | 0 | } |
48 | |
|
49 | |
@RequestMapping("/manageUsers") |
50 | |
@RequiresPermissions("user:manage") |
51 | |
public void manageUsers(Model model) { |
52 | 0 | model.addAttribute("users", userService.getAllUsers()); |
53 | 0 | } |
54 | |
|
55 | |
@RequestMapping(value="/editUser",method= RequestMethod.GET) |
56 | |
@RequiresPermissions("user:edit") |
57 | |
public String showEditUserForm(Model model, @RequestParam Long userId, @ModelAttribute EditUserCommand command) { |
58 | |
|
59 | 0 | User user = userService.getUser( userId ); |
60 | 0 | command.setUserId(userId); |
61 | 0 | command.setUsername(user.getUsername()); |
62 | 0 | command.setEmail(user.getEmail()); |
63 | 0 | return "editUser"; |
64 | |
} |
65 | |
|
66 | |
@RequestMapping(value="/editUser",method= RequestMethod.POST) |
67 | |
@RequiresPermissions("user:edit") |
68 | |
public String editUser(Model model, @RequestParam Long userId, @ModelAttribute EditUserCommand command, BindingResult errors) { |
69 | 0 | editUserValidator.validate( command, errors ); |
70 | |
|
71 | 0 | if( errors.hasErrors() ) { |
72 | 0 | return "editUser"; |
73 | |
} |
74 | |
|
75 | 0 | User user = userService.getUser( userId ); |
76 | 0 | command.updateUser( user ); |
77 | |
|
78 | 0 | userService.updateUser( user ); |
79 | |
|
80 | 0 | return "redirect:/s/manageUsers"; |
81 | |
} |
82 | |
|
83 | |
@RequestMapping("/deleteUser") |
84 | |
@RequiresPermissions("user:delete") |
85 | |
public String deleteUser(@RequestParam Long userId) { |
86 | 0 | Assert.isTrue( userId != 1, "Cannot delete admin user" ); |
87 | 0 | userService.deleteUser( userId ); |
88 | 0 | return "redirect:/s/manageUsers"; |
89 | |
} |
90 | |
|
91 | |
} |