UserController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.genersoft.iot.vmp.vmanager.user;
  2. import cn.dev33.satoken.annotation.SaIgnore;
  3. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  4. import com.genersoft.iot.vmp.conf.security.SecurityUtils;
  5. import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
  6. import com.genersoft.iot.vmp.conf.security.saToken.SaAdminCheckRole;
  7. import com.genersoft.iot.vmp.service.IAdminService;
  8. import com.genersoft.iot.vmp.service.IRoleService;
  9. import com.genersoft.iot.vmp.storager.dao.dto.AdminAccount;
  10. import com.genersoft.iot.vmp.storager.dao.dto.Role;
  11. import com.genersoft.iot.vmp.utils.DateUtil;
  12. import com.genersoft.iot.vmp.utils.StpAdminUtil;
  13. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  14. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  15. import com.github.pagehelper.PageInfo;
  16. import io.swagger.v3.oas.annotations.Operation;
  17. import io.swagger.v3.oas.annotations.Parameter;
  18. import io.swagger.v3.oas.annotations.tags.Tag;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.security.authentication.AuthenticationManager;
  23. import org.springframework.util.DigestUtils;
  24. import org.springframework.util.ObjectUtils;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.security.sasl.AuthenticationException;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.util.List;
  30. @Tag(name = "用户管理")
  31. @CrossOrigin(origins = "*")
  32. @RestController
  33. @SaAdminCheckRole("admin")
  34. @RequestMapping("/api/user")
  35. public class UserController {
  36. private Logger logger = LoggerFactory.getLogger(UserController.class);
  37. @Autowired
  38. private AuthenticationManager authenticationManager;
  39. @Autowired
  40. private IAdminService userService;
  41. @Autowired
  42. private IRoleService roleService;
  43. // @GetMapping("/login")
  44. @PostMapping("/login")
  45. @SaIgnore
  46. @Operation(summary = "登录", description = "登录成功后返回AccessToken, 可以从返回值获取到也可以从响应头中获取到," +
  47. "后续的请求需要添加请求头 'access-token'或者放在参数里")
  48. @Parameter(name = "username", description = "用户名", required = true)
  49. @Parameter(name = "password", description = "密码(32位md5加密)", required = true)
  50. public WVPResult login(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password) {
  51. AdminAccount adminAccount = userService.getUser(username, password);
  52. if (adminAccount == null) {
  53. return WVPResult.fail(ErrorCode.ERROR100);
  54. }
  55. StpAdminUtil.login(adminAccount.getId());
  56. return WVPResult.success();
  57. }
  58. @GetMapping("/logout")
  59. @Operation(summary = "注销账户")
  60. public WVPResult<Boolean> logout() {
  61. StpAdminUtil.logout();
  62. return WVPResult.success(true);
  63. }
  64. @GetMapping("/default")
  65. @SaIgnore
  66. @Operation(summary = "获取是否为新平台", description = "获取是否为新平台")
  67. public WVPResult<Boolean> isDefault() {
  68. int accountSize = userService.getUserCount();
  69. if (accountSize > 0) {
  70. return WVPResult.success(false);
  71. }
  72. return WVPResult.success(true);
  73. }
  74. // 注册管理员
  75. @PostMapping("/register")
  76. @SaIgnore
  77. @Operation(summary = "注册管理员账户", description = "注册管理员账户")
  78. @Parameter(name = "username", description = "用户名", required = true)
  79. @Parameter(name = "password", description = "密码(32位md5加密)", required = true)
  80. public WVPResult register(@RequestParam String username, @RequestParam String password) {
  81. int accountSize = userService.getUserCount();
  82. if (accountSize > 0) {
  83. logger.warn("在已经拥有一个账户的情况下尝试注册管理员");
  84. return WVPResult.fail(ErrorCode.ERROR100);
  85. }
  86. if (!userService.registerAdmin(username, password)) {
  87. logger.warn("无法注册管理员账户");
  88. return WVPResult.fail(ErrorCode.ERROR100);
  89. }
  90. return WVPResult.success();
  91. }
  92. @PostMapping("/changePassword")
  93. @Operation(summary = "修改密码")
  94. @Parameter(name = "username", description = "用户名", required = true)
  95. @Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
  96. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  97. public WVPResult changePassword(@RequestParam String oldPassword, @RequestParam String password) {
  98. logger.info("[用户管理] 修改密码");
  99. // 获取当前登录用户id
  100. String adminId = StpAdminUtil.getLoginId().toString();
  101. AdminAccount adminAccount = userService.getUserById(adminId);
  102. String username = adminAccount.getUsername();
  103. if (oldPassword != adminAccount.getPassword()) {
  104. return WVPResult.fail(ErrorCode.ERROR403, "原密码错误");
  105. }
  106. String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
  107. //
  108. logger.info("[用户管理] 修改密码,用户id:" + adminAccount.getId() + ",用户名:" + username);
  109. boolean result = userService.changePassword(adminAccount.getId(), passwordMd5);
  110. if (!result) {
  111. return WVPResult.fail(ErrorCode.ERROR100, "修改密码失败");
  112. }
  113. return WVPResult.success();
  114. }
  115. @PostMapping("/add")
  116. @Operation(summary = "添加用户")
  117. @Parameter(name = "username", description = "用户名", required = true)
  118. @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
  119. public void add(@RequestParam String username, @RequestParam String password) {
  120. if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
  121. throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
  122. }
  123. // 获取当前登录用户id
  124. String accountId = StpAdminUtil.getLoginId().toString();
  125. logger.info("[用户管理] 添加用户,当前用户id:" + accountId);
  126. AdminAccount adminAccount = new AdminAccount();
  127. adminAccount.setUsername(username);
  128. adminAccount.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
  129. //新增用户的pushKey的生成规则为md5(时间戳+用户名)
  130. adminAccount.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis() + password).getBytes()));
  131. adminAccount.setCreateTime(DateUtil.getNow());
  132. adminAccount.setUpdateTime(DateUtil.getNow());
  133. int addResult = userService.addUser(adminAccount);
  134. if (addResult <= 0) {
  135. throw new ControllerException(ErrorCode.ERROR100);
  136. }
  137. }
  138. @DeleteMapping("/delete")
  139. @Operation(summary = "删除用户")
  140. @Parameter(name = "id", description = "用户Id", required = true)
  141. public WVPResult<String> delete(@RequestParam Integer id) {
  142. // 获取当前登录用户id
  143. if (!StpAdminUtil.hasRole("primary")) {
  144. return WVPResult.fail(ErrorCode.ERROR403, "无权限操作");
  145. }
  146. int deleteResult = userService.deleteUser(id);
  147. if (deleteResult <= 0) {
  148. return WVPResult.fail(ErrorCode.ERROR404, "该用户不存在");
  149. }
  150. return WVPResult.success("");
  151. }
  152. @GetMapping("/all")
  153. @Operation(summary = "查询用户")
  154. public WVPResult<List<AdminAccount>> all() {
  155. // 获取当前登录用户id
  156. if (!StpAdminUtil.hasRole("primary")) {
  157. return WVPResult.fail(ErrorCode.ERROR403, "无权限操作");
  158. }
  159. List<AdminAccount> adminAccount = userService.getAllUsers();
  160. return WVPResult.success(adminAccount);
  161. }
  162. /**
  163. * 分页查询用户
  164. *
  165. * @param page 当前页
  166. * @param count 每页查询数量
  167. * @return 分页用户列表
  168. */
  169. @GetMapping("/users")
  170. @Operation(summary = "分页查询用户")
  171. @Parameter(name = "page", description = "当前页", required = true)
  172. @Parameter(name = "count", description = "每页查询数量", required = true)
  173. public PageInfo<AdminAccount> users(int page, int count) {
  174. return userService.getUsers(page, count);
  175. }
  176. @RequestMapping("/changePushKey")
  177. @Operation(summary = "修改pushkey")
  178. @Parameter(name = "pushKey", description = "新的pushKey", required = true)
  179. public WVPResult changePushKey(@RequestParam String pushKey) {
  180. // 获取当前登录用户id
  181. String accountId = StpAdminUtil.getLoginId().toString();
  182. logger.info("[用户管理] 修改pushKey,当前用户id:" + accountId);
  183. int resetPushKeyResult = userService.changePushKey(accountId, pushKey);
  184. if (resetPushKeyResult <= 0) {
  185. return WVPResult.fail(ErrorCode.ERROR100);
  186. }
  187. return WVPResult.success();
  188. }
  189. @PostMapping("/changePasswordForAdmin")
  190. @Operation(summary = "管理员修改普通用户密码")
  191. @Parameter(name = "userId", description = "用户id", required = true)
  192. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  193. public WVPResult<String> changePasswordForAdmin(@RequestParam String userId, @RequestParam String password) {
  194. // 获取当前登录用户id
  195. // 获取当前登录用户id
  196. String adminId = StpAdminUtil.getLoginId().toString();
  197. AdminAccount adminAccount = userService.getUserById(adminId);
  198. AdminAccount changeAdminAccount = userService.getUserById(userId);
  199. String _passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
  200. boolean result = false;
  201. if (changeAdminAccount == null) {
  202. return WVPResult.fail(ErrorCode.ERROR404, "要更改的用户不存在");
  203. }
  204. if (StpAdminUtil.hasRole("primary")) {
  205. result = userService.changePassword(userId, _passwordMd5);
  206. }
  207. if (!result) {
  208. return WVPResult.fail(ErrorCode.ERROR100, "修改用户密码失败");
  209. }
  210. return WVPResult.success();
  211. }
  212. }