Переглянути джерело

新增教研分析单点登录

wangliang 3 роки тому
батько
коміт
3ae38a6f73

+ 6 - 1
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/cache/RedisKeyHelper.java → teachcloud-common/src/main/java/com/qmth/teachcloud/common/cache/RedisKeyHelper.java

@@ -1,4 +1,4 @@
-package com.qmth.teachcloud.report.business.cache;
+package com.qmth.teachcloud.common.cache;
 
 /**
  * @Description: redis cache helper
@@ -11,6 +11,7 @@ public class RedisKeyHelper {
 
     private static String thirdUserAuthFix = "third_user_auth:";
     private static String thirdUserAuthReturnUrlFix = "third_user_auth_return_url:";
+    private static String thirdUserAuthRoleFix = "third_user_auth_role:";
 
     public static String thirdUserAuth(String key) {
         return thirdUserAuthFix + key;
@@ -19,4 +20,8 @@ public class RedisKeyHelper {
     public static String thirdUserAuthReturnUrl(String key) {
         return thirdUserAuthReturnUrlFix + key;
     }
+
+    public static String thirdUserAuthRole(String key) {
+        return thirdUserAuthRoleFix + key;
+    }
 }

+ 15 - 1
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/cache/ThirdUserAuthCacheUtil.java → teachcloud-common/src/main/java/com/qmth/teachcloud/common/cache/ThirdUserAuthCacheUtil.java

@@ -1,10 +1,12 @@
-package com.qmth.teachcloud.report.business.cache;
+package com.qmth.teachcloud.common.cache;
 
 import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
 import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
+import com.qmth.teachcloud.common.enums.RoleTypeEnum;
 import com.qmth.teachcloud.common.util.RedisUtil;
 
+import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -40,4 +42,16 @@ public class ThirdUserAuthCacheUtil {
     public static void deleteAuthReturnUrl(String key) {
         redisUtil.delete(RedisKeyHelper.thirdUserAuthReturnUrl(key));
     }
+
+    public static void setAuthRole(String key, RoleTypeEnum role) {
+        redisUtil.set(RedisKeyHelper.thirdUserAuthRole(key), role);
+    }
+
+    public static RoleTypeEnum getAuthRole(String key) {
+        return Objects.nonNull(redisUtil.get(RedisKeyHelper.thirdUserAuthRole(key))) ? RoleTypeEnum.valueOf((String) redisUtil.get(RedisKeyHelper.thirdUserAuthRole(key))) : null;
+    }
+
+    public static void deleteAuthRole(String key) {
+        redisUtil.delete(RedisKeyHelper.thirdUserAuthRole(key));
+    }
 }

+ 11 - 2
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/TeachcloudCommonServiceImpl.java

@@ -16,6 +16,7 @@ import com.qmth.teachcloud.common.bean.dto.MenuPrivilegeDto;
 import com.qmth.teachcloud.common.bean.dto.OrgDto;
 import com.qmth.teachcloud.common.bean.dto.PrivilegeCacheDto;
 import com.qmth.teachcloud.common.bean.result.*;
+import com.qmth.teachcloud.common.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
@@ -110,10 +111,18 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
     @Override
     public MenuResult getUserMenu(Long userId) {
         SysUser sysUser = commonCacheService.userCache(userId);
+        RoleTypeEnum role = ThirdUserAuthCacheUtil.getAuthRole(String.valueOf(userId));
         List<SysUserRole> sysUserRoleList = commonCacheService.userRolePrivilegeCache(userId);
         List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
-        for (SysUserRole s : sysUserRoleList) {
-            sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(s.getRoleId()));
+        if (Objects.isNull(role)) {
+            for (SysUserRole s : sysUserRoleList) {
+                sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(s.getRoleId()));
+            }
+        } else {
+            QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
+            sysRoleQueryWrapper.lambda().eq(SysRole::getType, role);
+            SysRole sysRole = sysRoleService.getOne(sysRoleQueryWrapper);
+            sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(sysRole.getId()));
         }
         Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
         QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();

+ 4 - 1
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/ReportCommonServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.gson.Gson;
 import com.itextpdf.text.pdf.PdfReader;
 import com.qmth.boot.api.exception.ApiException;
+import com.qmth.teachcloud.common.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicAttachment;
@@ -18,7 +19,6 @@ import com.qmth.teachcloud.common.service.SysUserService;
 import com.qmth.teachcloud.common.service.TBSessionService;
 import com.qmth.teachcloud.common.util.*;
 import com.qmth.teachcloud.report.business.bean.result.*;
-import com.qmth.teachcloud.report.business.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.report.business.entity.*;
 import com.qmth.teachcloud.report.business.enums.*;
 import com.qmth.teachcloud.report.business.service.*;
@@ -660,12 +660,15 @@ public class ReportCommonServiceImpl implements ReportCommonService {
             redisUtil.deleteUserSession(tbSession.getId());
             commonCacheService.removeUserCache(sysUser.getId());
             commonCacheService.removeUserAuthCache(sysUser.getId());
+            commonCacheService.removeUserMenuCache(sysUser.getId());
+            commonCacheService.removeUserRolePrivilegeCache(sysUser.getId());
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
         } finally {
             if (Objects.nonNull(sysUser)) {
                 HttpServletResponse response = ServletUtil.getResponse();
                 String redirectURL = ThirdUserAuthCacheUtil.getAuthReturnUrl(String.valueOf(sysUser.getId()));
+                ThirdUserAuthCacheUtil.deleteAuthRole(String.valueOf(sysUser.getId()));
                 ThirdUserAuthCacheUtil.deleteAuthReturnUrl(String.valueOf(sysUser.getId()));
                 response.setHeader("Access-Control-Allow-Origin", "*");
                 response.sendRedirect(redirectURL);

+ 9 - 1
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/SsoApiController.java

@@ -7,6 +7,7 @@ import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.teachcloud.common.SignatureEntityTest;
 import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
 import com.qmth.teachcloud.common.bean.result.LoginResult;
+import com.qmth.teachcloud.common.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.*;
@@ -15,7 +16,6 @@ import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.enums.RoleTypeEnum;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.*;
-import com.qmth.teachcloud.report.business.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.report.business.service.ReportCommonService;
 import io.swagger.annotations.*;
 import org.apache.commons.lang3.StringUtils;
@@ -75,6 +75,9 @@ public class SsoApiController {
     @Resource
     ReportCommonService reportCommonService;
 
+    @Resource
+    CommonCacheService commonCacheService;
+
     @ApiOperation(value = "单点登录接口")
     @ApiResponses({@ApiResponse(code = 200, message = "单点登录接口", response = Object.class)})
     @RequestMapping(value = "/login", method = RequestMethod.POST)
@@ -150,6 +153,7 @@ public class SsoApiController {
         String pattern = "{0}{1}{2}";
         String code = URLEncoder.encode(MessageFormat.format(pattern, userAuthenticationDto.getUid(), SignatureEntityTest.FIELD_JOINER, SignatureEntityTest.encrypt(userAuthenticationDto.getCode())), SystemConstant.CHARSET_NAME);
 
+        ThirdUserAuthCacheUtil.setAuthRole(String.valueOf(sysUser.getId()), role);
         ThirdUserAuthCacheUtil.setAuthReturnUrl(String.valueOf(sysUser.getId()), returnUrl);
 
         HttpServletResponse response = ServletUtil.getResponse();
@@ -184,6 +188,10 @@ public class SsoApiController {
         }
         LoginResult loginResult = teachcloudCommonService.login(sysUser.getPassword(), sysUser, appSource);
         ThirdUserAuthCacheUtil.deleteAuthCode(values[0]);
+        commonCacheService.removeUserCache(sysUser.getId());
+        commonCacheService.removeUserAuthCache(sysUser.getId());
+        commonCacheService.removeUserMenuCache(sysUser.getId());
+        commonCacheService.removeUserRolePrivilegeCache(sysUser.getId());
         return ResultUtil.ok(loginResult);
     }
 

+ 2 - 0
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/SysController.java

@@ -174,6 +174,8 @@ public class SysController {
         redisUtil.deleteUserSession(tbSession.getId());
         commonCacheService.removeUserCache(sysUser.getId());
         commonCacheService.removeUserAuthCache(sysUser.getId());
+        commonCacheService.removeUserMenuCache(sysUser.getId());
+        commonCacheService.removeUserRolePrivilegeCache(sysUser.getId());
         return ResultUtil.ok(new EditResult(sysUser.getId()));
     }
 

+ 1 - 1
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/WudaOpenApiController.java

@@ -8,6 +8,7 @@ import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.teachcloud.common.SignatureEntityTest;
 import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
 import com.qmth.teachcloud.common.bean.result.LoginResult;
+import com.qmth.teachcloud.common.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.entity.BasicSchool;
@@ -20,7 +21,6 @@ import com.qmth.teachcloud.common.service.TeachcloudCommonService;
 import com.qmth.teachcloud.common.util.JacksonUtil;
 import com.qmth.teachcloud.common.util.Result;
 import com.qmth.teachcloud.common.util.ResultUtil;
-import com.qmth.teachcloud.report.business.cache.ThirdUserAuthCacheUtil;
 import com.qmth.teachcloud.report.business.service.ReportCommonService;
 import com.qmth.teachcloud.report.business.utils.EncrypAES;
 import io.swagger.annotations.*;