Bläddra i källkod

v3.1.0功能代码整合

wangliang 3 år sedan
förälder
incheckning
04c0e3d603

+ 3 - 3
teachcloud-common-api/src/main/java/com/qmth/teachcloud/common/api/api/SysRoleController.java

@@ -76,7 +76,7 @@ public class SysRoleController {
         if (bindingResult.hasErrors()) {
             return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
         }
-        return ResultUtil.ok(sysRoleService.saveRoleNew(role));
+        return ResultUtil.ok(sysRoleService.saveRoleReportNew(role));
     }
 
     /**
@@ -88,7 +88,7 @@ public class SysRoleController {
     @ApiOperation(value = "启用/禁用")
     @RequestMapping(value = "/enable", method = RequestMethod.POST)
     public Result enable(@RequestBody SysRole role) throws NoSuchAlgorithmException {
-        return ResultUtil.ok(sysRoleService.enable(role));
+        return ResultUtil.ok(sysRoleService.enableReport(role));
     }
 
     /**
@@ -100,7 +100,7 @@ public class SysRoleController {
     @ApiOperation(value = "删除")
     @RequestMapping(value = "/remove", method = RequestMethod.POST)
     public Result remove(Long id) {
-        return ResultUtil.ok(sysRoleService.remove(id));
+        return ResultUtil.ok(sysRoleService.removeReport(id));
     }
 
     /**

+ 6 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/SysRoleService.java

@@ -30,10 +30,16 @@ public interface SysRoleService extends IService<SysRole> {
      */
     boolean saveRoleNew(SysRole role);
 
+    boolean saveRoleReportNew(SysRole role);
+
     boolean enable(SysRole role) throws NoSuchAlgorithmException;
 
+    boolean enableReport(SysRole role) throws NoSuchAlgorithmException;
+
     boolean remove(Long id);
 
+    boolean removeReport(Long id);
+
     List<Long> getUserRoles(Long userId);
 
     List<SysRole> listRolesByUserId(Long id);

+ 94 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/SysRoleServiceImpl.java

@@ -201,6 +201,67 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
         return true;
     }
 
+    @Transactional
+    @Override
+    public boolean saveRoleReportNew(SysRole role) {
+        try {
+            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
+            Long schoolId = Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null;
+            if (Objects.isNull(schoolId) && Objects.isNull(role.getSchoolId())) {
+                role.setSchoolId(sysUser.getSchoolId());
+            } else if (Objects.nonNull(schoolId)) {
+                role.setSchoolId(schoolId);
+            }
+            if (Objects.nonNull(role.getId())) {//编辑
+                List<SysRolePrivilege> sysRolePrivilegeList = commonCacheService.rolePrivilegeCache(role.getId());
+                QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
+                sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, role.getPrivilegeIds());
+                List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
+                Set<String> relatedSet = sysPrivilegeList.stream().filter(s -> Objects.nonNull(s.getRelated())).map(s -> s.getRelated()).collect(Collectors.toSet());
+                Set<Long> finalRelatedSet = new HashSet<>(Arrays.asList(role.getPrivilegeIds()));
+                for (String s : relatedSet) {
+                    if (s.contains(",")) {
+                        String[] arrays = s.split(",");
+                        for (int i = 0; i < arrays.length; i++) {
+                            finalRelatedSet.add(Long.parseLong(arrays[i].trim()));
+                        }
+                    } else {
+                        finalRelatedSet.add(Long.parseLong(s));
+                    }
+                }
+                List<Long> relatedList = new ArrayList<>(finalRelatedSet);
+                int count = (int) sysRolePrivilegeList.stream().filter(s -> relatedList.contains(s.getPrivilegeId())).count();
+                role.updateInfo(sysUser.getId());
+                sysRoleService.updateById(role);
+                commonCacheService.updateRoleCache(role.getId());
+                if (count != sysRolePrivilegeList.size() || count != finalRelatedSet.size()) {
+                    // 删除权限前先更新涉及特殊权限用户 -> 需要重新同步
+                    sysRolePrivilegeService.removeByRoleId(role.getId());
+                    sysRolePrivilegeService.saveBatch(role);//角色权限
+                    commonCacheService.updateRolePrivilegeCache(role.getId());
+                    //绑定该角色的用户都需要清除鉴权缓存
+                    List<SysUserRole> sysUserRoleList = sysUserRoleService.listByRoleId(role.getId());
+                    commonService.removeUserInfoBatch(sysUserRoleList.stream().map(s -> s.getUserId()).collect(Collectors.toList()), true);
+                }
+            } else {
+                role.insertInfo(sysUser.getId());
+                sysRoleService.save(role);
+                sysRolePrivilegeService.saveBatch(role);//角色权限
+            }
+        } catch (Exception e) {
+            if (e instanceof DuplicateKeyException) {
+                String errorColumn = e.getCause().toString();
+                String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
+                throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
+            } else if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
+        return true;
+    }
+
     @Override
     public boolean enable(SysRole role) throws NoSuchAlgorithmException {
         UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
@@ -223,6 +284,23 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
         return true;
     }
 
+    @Override
+    public boolean enableReport(SysRole role) throws NoSuchAlgorithmException {
+        UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.lambda().set(SysRole::getEnable, role.getEnable()).eq(SysRole::getId, role.getId());
+        this.update(updateWrapper);
+
+        commonCacheService.updateRoleCache(role.getId());
+        //如果状态为禁用,需要踢下线重新登录
+        if (!role.getEnable()) {
+            List<SysUserRole> sysUserRoleList = sysUserRoleService.listByRoleId(role.getId());
+            for (SysUserRole s : sysUserRoleList) {
+                commonService.removeUserInfo(s.getUserId(), true);
+            }
+        }
+        return true;
+    }
+
     @Transactional
     @Override
     public boolean remove(Long id) {
@@ -244,6 +322,22 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
         return true;
     }
 
+    @Override
+    public boolean removeReport(Long id) {
+        // 判断绑定用户
+        List<SysUserRole> userRoleList = sysUserRoleService.listByRoleId(id);
+        if (userRoleList != null && userRoleList.size() > 0) {
+            throw ExceptionResultEnum.ERROR.exception("用户绑定,不能删除");
+        }
+        //删除权限
+        sysRolePrivilegeService.removeByRoleId(id);
+        this.removeById(id);
+
+        commonCacheService.removeRoleCache(id);
+        commonCacheService.removeRolePrivilegeCache(id);
+        return true;
+    }
+
     @Override
     public List<Long> getUserRoles(Long userId) {
         QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();

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

@@ -116,7 +116,10 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
         List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
         if (Objects.isNull(role)) {
             for (SysUserRole s : sysUserRoleList) {
-                sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(s.getRoleId()));
+                SysRole sysRole = commonCacheService.roleCache(s.getRoleId());
+                if (Objects.nonNull(sysRole) && sysRole.getEnable()) {
+                    sysRolePrivilegeList.addAll(commonCacheService.rolePrivilegeCache(s.getRoleId()));
+                }
             }
         } else {
             QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();