wangliang 4 жил өмнө
parent
commit
96d83adb45

+ 44 - 27
themis-admin/src/main/java/com/qmth/themis/admin/api/TBUserController.java

@@ -20,14 +20,10 @@ import com.qmth.themis.business.enums.MqTagEnum;
 import com.qmth.themis.business.enums.SystemOperationEnum;
 import com.qmth.themis.business.service.*;
 import com.qmth.themis.business.util.*;
-import com.qmth.themis.common.contanst.Constants;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.enums.Platform;
 import com.qmth.themis.common.enums.Source;
 import com.qmth.themis.common.exception.BusinessException;
-import com.qmth.themis.common.signature.SignatureInfo;
-import com.qmth.themis.common.signature.SignatureType;
-import com.qmth.themis.common.util.Base64Util;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import io.swagger.annotations.*;
@@ -154,7 +150,7 @@ public class TBUserController {
         }
         return userLoginCommon(user);
     }
-    
+
     @ApiOperation(value = "短信验证码登陆接口")
     @RequestMapping(value = "/login/verifyCode", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = TBUser.class)})
@@ -615,19 +611,29 @@ public class TBUserController {
         if (Objects.isNull(mapParameter.get("orgId"))) {
             throw new BusinessException(ExceptionResultEnum.ORG_ID_IS_NULL);
         }
-        TBUser loginUser = (TBUser) ServletUtil.getRequestAccount();
+        boolean cacheClean = false;
+        TBUser loginUser = null;
+        AuthDto authDto = null;
+        if (Objects.nonNull(mapParameter.get("id"))) {
+            loginUser = tbUserService.getById(Long.parseLong(String.valueOf(mapParameter.get("id"))));
+            authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + loginUser.getId());
+        }
+        Gson gson = new Gson();
+        TBUser tbUser = gson.fromJson(gson.toJson(mapParameter), TBUser.class);
+        List<String> roleList = (List<String>) mapParameter.get("roleCode");
+        if (Objects.isNull(roleList) || roleList.size() == 0) {
+            throw new BusinessException("请选择角色");
+        }
+        Set<String> roleSet = new HashSet<>(roleList);
+        if (roleSet.size() > 1) {
+            throw new BusinessException("暂不支持多个角色");
+        }
+        if ((Objects.nonNull(tbUser) && Objects.nonNull(loginUser) && !tbUser.equals(loginUser))
+                || (Objects.nonNull(authDto) && !roleSet.equals(authDto.getRoleCodes()))) {
+            cacheClean = true;
+        }
         Long orgId = Long.parseLong(String.valueOf(mapParameter.get("orgId")));
         try {
-            Gson gson = new Gson();
-            TBUser tbUser = gson.fromJson(gson.toJson(mapParameter), TBUser.class);
-            List<String> roleList = (List<String>) mapParameter.get("roleCode");
-            if (Objects.isNull(roleList) || roleList.size() == 0) {
-                throw new BusinessException("请选择角色");
-            }
-            Set<String> roleSet = new HashSet<>(roleList);
-            if (roleSet.size() > 1) {
-                throw new BusinessException("暂不支持多个角色");
-            }
             if (Objects.isNull(tbUser.getId())) {
                 tbUser.setId(uidUtil.getId());
                 tbUser.setOrgId(orgId);
@@ -654,15 +660,12 @@ public class TBUserController {
             }
             tbUserService.saveOrUpdate(tbUser);
             //清除用户缓存
-            if (Objects.nonNull(roleSet) && roleSet.size() > 0) {
-                AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + tbUser.getId());
-                if (Objects.nonNull(authDto)) {
-                    for (Source s : Source.values()) {
-                        String sessionId = SessionUtil
-                                .digest(tbUser.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()),
-                                        s.name());
-                        redisUtil.deleteUserSession(sessionId);
-                    }
+            if (cacheClean) {
+                for (Source s : Source.values()) {
+                    String sessionId = SessionUtil
+                            .digest(tbUser.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()),
+                                    s.name());
+                    redisUtil.deleteUserSession(sessionId);
                 }
                 redisUtil.deleteUser(tbUser.getId());
                 cacheService.removeAccountCache(tbUser.getId());
@@ -717,7 +720,7 @@ public class TBUserController {
     @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
     public Result enableUser(@ApiJsonObject(name = "enableUser", value = {
             @ApiJsonProperty(key = "id", type = "long", example = "1", description = "用户id"),
-            @ApiJsonProperty(key = "enable", type = "int", example = "1", description = "停用/启用")}) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> user) {
+            @ApiJsonProperty(key = "enable", type = "int", example = "1", description = "停用/启用")}) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> user) throws NoSuchAlgorithmException {
         if (Objects.isNull(user.get("id")) || Objects.equals(user.get("id"), "")) {
             throw new BusinessException(ExceptionResultEnum.USER_ID_IS_NULL);
         }
@@ -733,7 +736,21 @@ public class TBUserController {
         //保存用户
         tbUser.setEnable(enable);
         tbUserService.updateById(tbUser);
-        redisUtil.setUser(tbUser.getId(), tbUser);
+        if (enable == 0) {
+            AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + tbUser.getId());
+            if (Objects.nonNull(authDto)) {
+                for (Source s : Source.values()) {
+                    String sessionId = SessionUtil
+                            .digest(tbUser.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()),
+                                    s.name());
+                    redisUtil.deleteUserSession(sessionId);
+                }
+            }
+            redisUtil.deleteUser(tbUser.getId());
+            cacheService.removeAccountCache(tbUser.getId());
+        } else {
+            redisUtil.setUser(tbUser.getId(), tbUser);
+        }
         return ResultUtil.ok(true);
     }
 

+ 18 - 0
themis-business/src/main/java/com/qmth/themis/business/entity/TBUser.java

@@ -9,6 +9,8 @@ import com.qmth.themis.business.util.UidUtil;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 
+import java.util.Objects;
+
 /**
  * @Description: 用户
  * @Param:
@@ -116,4 +118,20 @@ public class TBUser extends BaseEntity {
     public void setRemark(String remark) {
         this.remark = remark;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        TBUser tbUser = (TBUser) o;
+        return Objects.equals(loginName, tbUser.loginName) &&
+                Objects.equals(password, tbUser.password) &&
+                Objects.equals(enable, tbUser.enable) &&
+                Objects.equals(orgId, tbUser.orgId);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(loginName, password, enable, orgId);
+    }
 }

+ 3 - 1
themis-business/src/main/java/com/qmth/themis/business/enums/FieldUniqueEnum.java

@@ -31,7 +31,9 @@ public enum FieldUniqueEnum {
     
     t_e_exam_short_code_Idx("考试口令"),
 
-    t_b_org_code_Idx("机构代码");
+    t_b_org_code_Idx("机构代码"),
+
+    org_login_name("登录名");
 
     private String code;
 

+ 4 - 0
themis-common/pom.xml

@@ -78,5 +78,9 @@
             <groupId>com.github.tencentyun</groupId>
             <artifactId>tls-sig-api-v2</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 10 - 0
themis-common/src/main/java/com/qmth/themis/common/exception/GlobalDefultExceptionHandler.java

@@ -1,10 +1,13 @@
 package com.qmth.themis.common.exception;
 
+import com.alibaba.fastjson.JSONObject;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -12,6 +15,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.SQLException;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @Description: 全局异常处理
@@ -44,6 +49,11 @@ public class GlobalDefultExceptionHandler {
             if (e.getMessage().contains("No enum constant com.qmth.themis.common.enums.Platform")) {
                 return ResultUtil.error(ExceptionResultEnum.EXCEPTION_ERROR.getStatusCode(), "暂不支持此平台");
             }
+        } else if (e instanceof MethodArgumentNotValidException) {
+            response.setStatus(ExceptionResultEnum.EXCEPTION_ERROR.getCode());
+            BindingResult bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
+            List<String> errorList = bindingResult.getFieldErrors().stream().map(o -> o.getDefaultMessage()).collect(Collectors.toList());
+            return ResultUtil.error(ExceptionResultEnum.ERROR.getCode(), JSONObject.toJSONString(errorList));
         }
         response.setStatus(ExceptionResultEnum.EXCEPTION_ERROR.getCode());
         //Exception错误