UserController.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.genersoft.iot.vmp.vmanager.user;
  2. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  3. import com.genersoft.iot.vmp.conf.security.SecurityUtils;
  4. import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
  5. import com.genersoft.iot.vmp.service.IRoleService;
  6. import com.genersoft.iot.vmp.service.IUserService;
  7. import com.genersoft.iot.vmp.storager.dao.dto.Role;
  8. import com.genersoft.iot.vmp.storager.dao.dto.User;
  9. import com.genersoft.iot.vmp.utils.DateUtil;
  10. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  11. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  12. import com.github.pagehelper.PageInfo;
  13. import io.swagger.v3.oas.annotations.Operation;
  14. import io.swagger.v3.oas.annotations.Parameter;
  15. import io.swagger.v3.oas.annotations.tags.Tag;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.http.HttpStatus;
  18. import org.springframework.http.ResponseEntity;
  19. import org.springframework.security.authentication.AuthenticationManager;
  20. import org.springframework.util.DigestUtils;
  21. import org.springframework.util.ObjectUtils;
  22. import org.springframework.util.StringUtils;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.security.sasl.AuthenticationException;
  25. import java.util.List;
  26. @Tag(name = "用户管理")
  27. @CrossOrigin
  28. @RestController
  29. @RequestMapping("/api/user")
  30. public class UserController {
  31. @Autowired
  32. private AuthenticationManager authenticationManager;
  33. @Autowired
  34. private IUserService userService;
  35. @Autowired
  36. private IRoleService roleService;
  37. @GetMapping("/login")
  38. @PostMapping("/login")
  39. @Operation(summary = "登录")
  40. @Parameter(name = "username", description = "用户名", required = true)
  41. @Parameter(name = "password", description = "密码(32位md5加密)", required = true)
  42. public LoginUser login(@RequestParam String username, @RequestParam String password){
  43. LoginUser user = null;
  44. try {
  45. user = SecurityUtils.login(username, password, authenticationManager);
  46. } catch (AuthenticationException e) {
  47. throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
  48. }
  49. if (user == null) {
  50. throw new ControllerException(ErrorCode.ERROR100.getCode(), "用户名或密码错误");
  51. }
  52. return user;
  53. }
  54. @PostMapping("/changePassword")
  55. @Operation(summary = "修改密码")
  56. @Parameter(name = "username", description = "用户名", required = true)
  57. @Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
  58. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  59. public void changePassword(@RequestParam String oldPassword, @RequestParam String password){
  60. // 获取当前登录用户id
  61. LoginUser userInfo = SecurityUtils.getUserInfo();
  62. if (userInfo== null) {
  63. throw new ControllerException(ErrorCode.ERROR100);
  64. }
  65. String username = userInfo.getUsername();
  66. LoginUser user = null;
  67. try {
  68. user = SecurityUtils.login(username, oldPassword, authenticationManager);
  69. if (user == null) {
  70. throw new ControllerException(ErrorCode.ERROR100);
  71. }
  72. int userId = SecurityUtils.getUserId();
  73. boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
  74. if (!result) {
  75. throw new ControllerException(ErrorCode.ERROR100);
  76. }
  77. } catch (AuthenticationException e) {
  78. throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
  79. }
  80. }
  81. @PostMapping("/add")
  82. @Operation(summary = "停止视频回放")
  83. @Parameter(name = "username", description = "用户名", required = true)
  84. @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
  85. @Parameter(name = "roleId", description = "角色ID", required = true)
  86. public void add(@RequestParam String username,
  87. @RequestParam String password,
  88. @RequestParam Integer roleId){
  89. if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
  90. throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
  91. }
  92. // 获取当前登录用户id
  93. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  94. if (currenRoleId != 1) {
  95. // 只用角色id为1才可以删除和添加用户
  96. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  97. }
  98. User user = new User();
  99. user.setUsername(username);
  100. user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
  101. //新增用户的pushKey的生成规则为md5(时间戳+用户名)
  102. user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis()+password).getBytes()));
  103. Role role = roleService.getRoleById(roleId);
  104. if (role == null) {
  105. throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
  106. }
  107. user.setRole(role);
  108. user.setCreateTime(DateUtil.getNow());
  109. user.setUpdateTime(DateUtil.getNow());
  110. int addResult = userService.addUser(user);
  111. if (addResult <= 0) {
  112. throw new ControllerException(ErrorCode.ERROR100);
  113. }
  114. }
  115. @DeleteMapping("/删除用户")
  116. @Operation(summary = "停止视频回放")
  117. @Parameter(name = "id", description = "用户Id", required = true)
  118. public void delete(@RequestParam Integer id){
  119. // 获取当前登录用户id
  120. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  121. if (currenRoleId != 1) {
  122. // 只用角色id为0才可以删除和添加用户
  123. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  124. }
  125. int deleteResult = userService.deleteUser(id);
  126. if (deleteResult <= 0) {
  127. throw new ControllerException(ErrorCode.ERROR100);
  128. }
  129. }
  130. @GetMapping("/all")
  131. @Operation(summary = "查询用户")
  132. public List<User> all(){
  133. // 获取当前登录用户id
  134. return userService.getAllUsers();
  135. }
  136. /**
  137. * 分页查询用户
  138. *
  139. * @param page 当前页
  140. * @param count 每页查询数量
  141. * @return 分页用户列表
  142. */
  143. @GetMapping("/users")
  144. @Operation(summary = "分页查询用户")
  145. @Parameter(name = "page", description = "当前页", required = true)
  146. @Parameter(name = "count", description = "每页查询数量", required = true)
  147. public PageInfo<User> users(int page, int count) {
  148. return userService.getUsers(page, count);
  149. }
  150. @RequestMapping("/changePushKey")
  151. @Operation(summary = "修改pushkey")
  152. @Parameter(name = "userId", description = "用户Id", required = true)
  153. @Parameter(name = "pushKey", description = "新的pushKey", required = true)
  154. public void changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) {
  155. // 获取当前登录用户id
  156. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  157. WVPResult<String> result = new WVPResult<>();
  158. if (currenRoleId != 1) {
  159. // 只用角色id为0才可以删除和添加用户
  160. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  161. }
  162. int resetPushKeyResult = userService.changePushKey(userId,pushKey);
  163. if (resetPushKeyResult <= 0) {
  164. throw new ControllerException(ErrorCode.ERROR100);
  165. }
  166. }
  167. @PostMapping("/changePasswordForAdmin")
  168. @Operation(summary = "管理员修改普通用户密码")
  169. @Parameter(name = "adminId", description = "管理员id", required = true)
  170. @Parameter(name = "userId", description = "用户id", required = true)
  171. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  172. public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
  173. // 获取当前登录用户id
  174. LoginUser userInfo = SecurityUtils.getUserInfo();
  175. if (userInfo == null) {
  176. throw new ControllerException(ErrorCode.ERROR100);
  177. }
  178. Role role = userInfo.getRole();
  179. if (role != null && role.getId() == 1) {
  180. boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
  181. if (!result) {
  182. throw new ControllerException(ErrorCode.ERROR100);
  183. }
  184. }
  185. }
  186. @PostMapping("/userInfo")
  187. @Operation(summary = "管理员修改普通用户密码")
  188. public LoginUser getUserInfo() {
  189. // 获取当前登录用户id
  190. LoginUser userInfo = SecurityUtils.getUserInfo();
  191. if (userInfo == null) {
  192. throw new ControllerException(ErrorCode.ERROR100);
  193. }
  194. return userInfo;
  195. }
  196. }