From eda9bafd772be4692a2a76c975d5d17f5275dbfc Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期五, 27 五月 2022 18:22:23 +0800 Subject: [PATCH] Merge pull request #497 from TristingChen/fixed--getAllSsrc --- src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java | 60 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 44 insertions(+), 16 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 6e4c416..152122d 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,8 +2,11 @@ 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; @@ -14,10 +17,10 @@ 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 java.text.SimpleDateFormat; import java.util.List; @Api(tags = "鐢ㄦ埛绠$悊") @@ -32,7 +35,8 @@ @Autowired private IUserService userService; - private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + @Autowired + private IRoleService roleService; @ApiOperation("鐧诲綍") @ApiImplicitParams({ @@ -70,7 +74,11 @@ @PostMapping("/changePassword") 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); @@ -97,21 +105,38 @@ @PostMapping("/add") 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())); + + 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); @@ -125,13 +150,16 @@ @DeleteMapping("/delete") 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); -- Gitblit v1.8.0