From ecd14d6757eb12e66d7668eff509c10875abf855 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期一, 22 八月 2022 15:03:18 +0800 Subject: [PATCH] Merge pull request #578 from urmyfaith/patch-1 --- src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java | 188 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 136 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java index de40c2c..d628d57 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java @@ -2,27 +2,29 @@ import com.genersoft.iot.vmp.conf.security.SecurityUtils; import com.genersoft.iot.vmp.conf.security.dto.LoginUser; +import com.genersoft.iot.vmp.service.IRoleService; import com.genersoft.iot.vmp.service.IUserService; +import com.genersoft.iot.vmp.storager.dao.dto.Role; import com.genersoft.iot.vmp.storager.dao.dto.User; +import com.genersoft.iot.vmp.utils.DateUtil; import com.genersoft.iot.vmp.vmanager.bean.WVPResult; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; +import com.github.pagehelper.PageInfo; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.util.DigestUtils; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import javax.security.sasl.AuthenticationException; -import javax.xml.crypto.Data; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; -@Api(tags = "鐢ㄦ埛绠$悊") +@Tag(name = "鐢ㄦ埛绠$悊") @CrossOrigin @RestController @RequestMapping("/api/user") @@ -34,39 +36,46 @@ @Autowired private IUserService userService; - private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + @Autowired + private IRoleService roleService; - @ApiOperation("鐧诲綍") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "瀵嗙爜锛�32浣峬d5鍔犲瘑锛�", dataTypeClass = String.class), - }) @GetMapping("/login") - public String login(@RequestParam String username, @RequestParam String password){ - LoginUser user; + @Operation(summary = "鐧诲綍") + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "password", description = "瀵嗙爜锛�32浣峬d5鍔犲瘑锛�", required = true) + public WVPResult<LoginUser> login(@RequestParam String username, @RequestParam String password){ + LoginUser user = null; + WVPResult<LoginUser> result = new WVPResult<>(); try { user = SecurityUtils.login(username, password, authenticationManager); } catch (AuthenticationException e) { e.printStackTrace(); - return "fail"; + result.setCode(-1); + result.setMsg("fail"); } if (user != null) { - return "success"; + result.setCode(0); + result.setMsg("success"); + result.setData(user); }else { - return "fail"; + result.setCode(-1); + result.setMsg("fail"); } + return result; } - @ApiOperation("淇敼瀵嗙爜") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "oldpassword", required = true, value = "鏃у瘑鐮侊紙宸瞞d5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - }) @PostMapping("/changePassword") + @Operation(summary = "淇敼瀵嗙爜") + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "oldpassword", description = "鏃у瘑鐮侊紙宸瞞d5鍔犲瘑鐨勫瘑鐮侊級", required = true) + @Parameter(name = "password", description = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", required = true) public String changePassword(@RequestParam String oldPassword, @RequestParam String password){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id - String username = SecurityUtils.getUserInfo().getUsername(); + LoginUser userInfo = SecurityUtils.getUserInfo(); + if (userInfo== null) { + return "fail"; + } + String username = userInfo.getUsername(); LoginUser user = null; try { user = SecurityUtils.login(username, oldPassword, authenticationManager); @@ -84,58 +93,75 @@ } - @ApiOperation("娣诲姞鐢ㄦ埛") - @ApiImplicitParams({ - @ApiImplicitParam(name = "username", required = true, value = "鐢ㄦ埛鍚�", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", required = true, value = "瀵嗙爜锛堟湭md5鍔犲瘑鐨勫瘑鐮侊級", dataTypeClass = String.class), - @ApiImplicitParam(name = "roleId", required = true, value = "瑙掕壊ID", dataTypeClass = String.class), - }) @PostMapping("/add") + @Operation(summary = "鍋滄瑙嗛鍥炴斁") + @Parameter(name = "username", description = "鐢ㄦ埛鍚�", required = true) + @Parameter(name = "password", description = "瀵嗙爜锛堟湭md5鍔犲瘑鐨勫瘑鐮侊級", required = true) + @Parameter(name = "roleId", description = "瑙掕壊ID", required = true) public ResponseEntity<WVPResult<Integer>> add(@RequestParam String username, @RequestParam String password, - @RequestParam int roleId){ + @RequestParam Integer roleId){ + WVPResult<Integer> result = new WVPResult<>(); + if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || roleId == null) { + result.setCode(-1); + result.setMsg("鍙傛暟涓嶅彲涓虹┖"); + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + } // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id - int currenRoleId = SecurityUtils.getUserInfo().getRoleId(); - if (currenRoleId != 0) { - // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 - return new ResponseEntity<>(null, HttpStatus.FORBIDDEN); + int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); + if (currenRoleId != 1) { + // 鍙敤瑙掕壊id涓�1鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 + result.setCode(-1); + result.setMsg("鐢ㄦ埛鏃犳潈闄�"); + return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); } User user = new User(); user.setUsername(username); user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes())); - user.setRoleId(roleId); - user.setCreateTime(format.format(System.currentTimeMillis())); - user.setUpdateTime(format.format(System.currentTimeMillis())); + //鏂板鐢ㄦ埛鐨刾ushKey鐨勭敓鎴愯鍒欎负md5(鏃堕棿鎴�+鐢ㄦ埛鍚�) + user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis()+password).getBytes())); + Role role = roleService.getRoleById(roleId); + + if (role == null) { + result.setCode(-1); + result.setMsg("roleId is not found"); + // 瑙掕壊涓嶅瓨鍦� + return new ResponseEntity<>(result, HttpStatus.OK); + } + user.setRole(role); + user.setCreateTime(DateUtil.getNow()); + user.setUpdateTime(DateUtil.getNow()); int addResult = userService.addUser(user); - WVPResult<Integer> result = new WVPResult<>(); + + result.setCode(addResult > 0 ? 0 : -1); result.setMsg(addResult > 0 ? "success" : "fail"); result.setData(addResult); return new ResponseEntity<>(result, HttpStatus.OK); } - @ApiOperation("鍒犻櫎鐢ㄦ埛") - @ApiImplicitParams({ - @ApiImplicitParam(name = "id", required = true, value = "鐢ㄦ埛Id", dataTypeClass = Integer.class), - }) - @DeleteMapping("/delete") + @DeleteMapping("/鍒犻櫎鐢ㄦ埛") + @Operation(summary = "鍋滄瑙嗛鍥炴斁") + @Parameter(name = "id", description = "鐢ㄦ埛Id", required = true) public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id - int currenRoleId = SecurityUtils.getUserInfo().getRoleId(); - if (currenRoleId != 0) { + int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); + WVPResult<String> result = new WVPResult<>(); + if (currenRoleId != 1) { // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 - return new ResponseEntity<>(null, HttpStatus.FORBIDDEN); + result.setCode(-1); + result.setMsg("鐢ㄦ埛鏃犳潈闄�"); + return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); } int deleteResult = userService.deleteUser(id); - WVPResult<String> result = new WVPResult<>(); + result.setCode(deleteResult>0? 0 : -1); result.setMsg(deleteResult>0? "success" : "fail"); return new ResponseEntity<>(result, HttpStatus.OK); } - @ApiOperation("鏌ヨ鐢ㄦ埛") - @ApiImplicitParams({}) @GetMapping("/all") + @Operation(summary = "鏌ヨ鐢ㄦ埛") public ResponseEntity<WVPResult<List<User>>> all(){ // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id List<User> allUsers = userService.getAllUsers(); @@ -145,4 +171,62 @@ result.setData(allUsers); return new ResponseEntity<>(result, HttpStatus.OK); } + + /** + * 鍒嗛〉鏌ヨ鐢ㄦ埛 + * + * @param page 褰撳墠椤� + * @param count 姣忛〉鏌ヨ鏁伴噺 + * @return 鍒嗛〉鐢ㄦ埛鍒楄〃 + */ + @GetMapping("/users") + @Operation(summary = "鍒嗛〉鏌ヨ鐢ㄦ埛") + @Parameter(name = "page", description = "褰撳墠椤�", required = true) + @Parameter(name = "count", description = "姣忛〉鏌ヨ鏁伴噺", required = true) + public PageInfo<User> users(int page, int count) { + return userService.getUsers(page, count); + } + + @RequestMapping("/changePushKey") + @Operation(summary = "淇敼pushkey") + @Parameter(name = "userId", description = "鐢ㄦ埛Id", required = true) + @Parameter(name = "pushKey", description = "鏂扮殑pushKey", required = true) + public ResponseEntity<WVPResult<String>> changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) { + // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id + int currenRoleId = SecurityUtils.getUserInfo().getRole().getId(); + WVPResult<String> result = new WVPResult<>(); + if (currenRoleId != 1) { + // 鍙敤瑙掕壊id涓�0鎵嶅彲浠ュ垹闄ゅ拰娣诲姞鐢ㄦ埛 + result.setCode(-1); + result.setMsg("鐢ㄦ埛鏃犳潈闄�"); + return new ResponseEntity<>(result, HttpStatus.FORBIDDEN); + } + int resetPushKeyResult = userService.changePushKey(userId,pushKey); + + result.setCode(resetPushKeyResult > 0 ? 0 : -1); + result.setMsg(resetPushKeyResult > 0 ? "success" : "fail"); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @PostMapping("/changePasswordForAdmin") + @Operation(summary = "绠$悊鍛樹慨鏀规櫘閫氱敤鎴峰瘑鐮�") + @Parameter(name = "adminId", description = "绠$悊鍛榠d", required = true) + @Parameter(name = "userId", description = "鐢ㄦ埛id", required = true) + @Parameter(name = "password", description = "鏂板瘑鐮侊紙鏈猰d5鍔犲瘑鐨勫瘑鐮侊級", required = true) + public String changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) { + // 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛id + LoginUser userInfo = SecurityUtils.getUserInfo(); + if (userInfo == null) { + return "fail"; + } + Role role = userInfo.getRole(); + if (role != null && role.getId() == 1) { + boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes())); + if (result) { + return "success"; + } + } + + return "fail"; + } } -- Gitblit v1.8.0