瀏覽代碼

新增教研分析单点登录

wangliang 3 年之前
父節點
當前提交
ab1842abfe

+ 7 - 12
teachcloud-common/src/main/java/com/qmth/teachcloud/common/cache/ThirdUserAuthCacheUtil.java

@@ -3,10 +3,9 @@ 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.service.CommonCacheService;
 import com.qmth.teachcloud.common.util.RedisUtil;
 
-import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -43,15 +42,11 @@ public class ThirdUserAuthCacheUtil {
         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));
+    public static void deleteCache(Long userId) {
+        CommonCacheService commonCacheService = SpringContextHolder.getBean(CommonCacheService.class);
+        commonCacheService.removeUserCache(userId);
+        commonCacheService.removeUserAuthCache(userId);
+        commonCacheService.removeUserMenuCache(userId);
+        commonCacheService.removeUserRolePrivilegeCache(userId);
     }
 }

+ 3 - 2
teachcloud-common/src/main/java/com/qmth/teachcloud/common/entity/SysUser.java

@@ -17,6 +17,7 @@ import org.hibernate.validator.constraints.Length;
 import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.NotNull;
 import java.io.Serializable;
+import java.util.Objects;
 
 /**
  * <p>
@@ -128,7 +129,7 @@ public class SysUser extends BaseEntity implements Serializable {
 
     }
 
-    public SysUser(Long schoolId, String loginName, String realName, String mobileNumber) {
+    public SysUser(Long schoolId, String loginName, String realName, String mobileNumber, Boolean enable) {
         setId(SystemConstant.getDbUuid());
         this.schoolId = schoolId;
         this.loginName = loginName;
@@ -136,7 +137,7 @@ public class SysUser extends BaseEntity implements Serializable {
         this.mobileNumber = mobileNumber;
         this.pwdCount = 1;
         this.password = SystemConstant.DEFAULT_PASSWORD;
-        this.enable = true;
+        this.enable = Objects.isNull(enable) ? true : enable;
     }
 
     @Override

+ 4 - 13
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/TeachcloudCommonServiceImpl.java

@@ -16,7 +16,6 @@ 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;
@@ -111,21 +110,13 @@ 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<>();
-        if (Objects.isNull(role)) {
-            for (SysUserRole s : sysUserRoleList) {
-                SysRole sysRole = commonCacheService.roleCache(s.getRoleId());
-                if (Objects.nonNull(sysRole) && sysRole.getEnable()) {
-                    sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(s.getRoleId()));
-                }
+        for (SysUserRole s : sysUserRoleList) {
+            SysRole sysRole = commonCacheService.roleCache(s.getRoleId());
+            if (Objects.nonNull(sysRole) && sysRole.getEnable()) {
+                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<>();

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

@@ -658,18 +658,14 @@ public class ReportCommonServiceImpl implements ReportCommonService {
         try {
             tbSessionService.removeById(tbSession.getId());
             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()));
+                ThirdUserAuthCacheUtil.deleteCache(sysUser.getId());
                 response.setHeader("Access-Control-Allow-Origin", "*");
                 response.sendRedirect(redirectURL);
             }

+ 11 - 19
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/SsoApiController.java

@@ -104,7 +104,7 @@ public class SsoApiController {
             sysUser = new SysUser(basicSchool.getId(),
                     loginName,
                     Objects.nonNull(realName) ? realName : loginName,
-                    Objects.nonNull(mobileNumber) ? mobileNumber : null);
+                    Objects.nonNull(mobileNumber) ? mobileNumber : null, enable);
         } else {
             sysUser.setSchoolId(basicSchool.getId());
             if (Objects.nonNull(realName)) {
@@ -113,9 +113,9 @@ public class SsoApiController {
             if (Objects.nonNull(mobileNumber)) {
                 sysUser.setMobileNumber(mobileNumber);
             }
-        }
-        if (Objects.nonNull(enable)) {
-            sysUser.setEnable(enable);
+            if (Objects.nonNull(enable)) {
+                sysUser.setEnable(enable);
+            }
         }
 
         //查询学院id
@@ -136,24 +136,19 @@ public class SsoApiController {
         Optional.ofNullable(sysRole).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("角色数据为空"));
 
         QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
-        sysUserRoleQueryWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getId())
-                .eq(SysUserRole::getRoleId, sysRole.getId());
-        SysUserRole sysUserRole = sysUserRoleService.getOne(sysUserRoleQueryWrapper);
-        if (Objects.isNull(sysUserRole)) {
-            sysUserRole = new SysUserRole(sysUser.getId(), sysRole.getId());
-            sysUserRoleService.save(sysUserRole);
-        } else {
-            if (!sysUser.getEnable()) {
-                sysUserRoleService.removeById(sysUserRole);
-            }
+        sysUserRoleQueryWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getId());
+        int count = sysUserRoleService.count(sysUserRoleQueryWrapper);
+        if (count > 0) {
+            sysUserRoleService.remove(sysUserRoleQueryWrapper);
         }
+        SysUserRole sysUserRole = new SysUserRole(sysUser.getId(), sysRole.getId());
+        sysUserRoleService.save(sysUserRole);
 
         UserAuthenticationDto userAuthenticationDto = new UserAuthenticationDto(String.valueOf(sysUser.getId()), SystemConstant.getUuid());
         ThirdUserAuthCacheUtil.setAuthCode(userAuthenticationDto);
         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();
@@ -188,10 +183,7 @@ 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());
+        ThirdUserAuthCacheUtil.deleteCache(sysUser.getId());
         return ResultUtil.ok(loginResult);
     }
 

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

@@ -7,7 +7,6 @@ import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.api.exception.ApiException;
 import com.qmth.teachcloud.common.bean.auth.AuthBean;
 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.*;
@@ -153,12 +152,6 @@ public class SysController {
                 }
             }
         }
-        if (Objects.nonNull(ThirdUserAuthCacheUtil.getAuthRole(String.valueOf(sysUser.getId())))) {
-            commonCacheService.removeUserCache(sysUser.getId());
-            commonCacheService.removeUserAuthCache(sysUser.getId());
-            commonCacheService.removeUserMenuCache(sysUser.getId());
-            commonCacheService.removeUserRolePrivilegeCache(sysUser.getId());
-        }
         return ResultUtil.ok(teachcloudCommonService.login(login.getPassword(), sysUser, AppSourceEnum.SYSTEM));
     }