Procházet zdrojové kódy

权限框架移植 2022/10/26

lwhhszx před 2 roky
rodič
revize
6141bb36f5

+ 1 - 1
RMS/src/main/java/cn/cslg/report/common/config/InnerInterceptor/LizzMybatisIntercepts.java

@@ -1,4 +1,4 @@
-package cn.cslg.pas.common.config.InnerInterceptor;
+package cn.cslg.report.common.config.InnerInterceptor;
 
 import cn.cslg.report.common.model.DataSource;
 import cn.cslg.report.common.model.vo.PersonnelVO;

+ 0 - 40
RMS/src/main/java/cn/cslg/report/common/config/StpInterfaceImpl.java

@@ -1,40 +0,0 @@
-package cn.cslg.report.common.config;
-
-import cn.cslg.report.domain.associate.AssoRoleFunctionData;
-import cn.cslg.report.service.associate.RoleFunctionDataService;
-import cn.dev33.satoken.stp.StpInterface;
-import lombok.RequiredArgsConstructor;
-import org.springframework.context.annotation.Lazy;
-import org.springframework.stereotype.Component;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * 自定义权限验证接口扩展
- */
-@Component
-@RequiredArgsConstructor(onConstructor_ = {@Lazy})
-public class StpInterfaceImpl implements StpInterface {
-    private final RoleFunctionDataService roleFunctionDataService;
-
-    /**
-     * 返回一个账号所拥有的权限码集合
-     */
-    @Override
-    public List<String> getPermissionList(Object loginId, String loginType) {
-        //通过用户(人员)ID查询List
-        List<AssoRoleFunctionData> roleFunctionList = roleFunctionDataService.getRoleFunctionData(Integer.parseInt(loginId.toString()));
-        //提取人员List里面的功能 生成权限(功能)List
-        List<String> list = new ArrayList<>();
-        for (AssoRoleFunctionData assoRoleFunctionData : roleFunctionList) {
-            list.add(assoRoleFunctionData.getFunctionPath());
-        }
-        return list;
-    }
-
-    @Override
-    public List<String> getRoleList(Object o, String s) {
-        return null;
-    }
-}

+ 45 - 0
RMS/src/main/java/cn/cslg/report/common/model/DataSource.java

@@ -0,0 +1,45 @@
+package cn.cslg.report.common.model;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * @author 沈永艺
+ * @date 2022-8-12
+ * @description 数据权限类 数据库对应实体
+ */
+
+@Data
+@Accessors(chain = true)
+
+public class DataSource {
+    /**
+     * 数据字典名字
+     */
+    private String dataSourceName;
+
+    /**
+     * 数据字典描述
+     */
+    private String dataSourceDescription;
+
+    /**
+     * 数据字典数据库
+     */
+    private String dataSourceDataBase;
+
+    /**
+     * 数据字典表格
+     */
+    private String dataSourceTable;
+
+    /**
+     * 数据字典字段
+     */
+    private String dataSourceField;
+
+    private  Integer id;
+    private Integer isDelete;
+
+
+}

+ 0 - 23
RMS/src/main/java/cn/cslg/report/common/model/dto/TenantDTO.java

@@ -1,23 +0,0 @@
-package cn.cslg.report.common.model.dto;
-
-import cn.cslg.report.domain.Personnel;
-import lombok.Data;
-import lombok.experimental.Accessors;
-
-import java.util.List;
-
-/**
- * @author 沈永艺
- * @date 2022-9-22
- * @description 返回租户组织结构的工具类
- */
-@Data
-@Accessors(chain = true)
-public class TenantDTO {
-    private Integer tenantId;
-    private String tenantName;
-    private Integer departmentId;
-    private String departmentName;
-    private String departmentPath;
-    private List<Personnel> userList;
-}

+ 5 - 50
RMS/src/main/java/cn/cslg/report/common/model/vo/PersonnelVO.java

@@ -1,9 +1,6 @@
 package cn.cslg.report.common.model.vo;
 
-import cn.cslg.report.common.model.BaseVO;
-import cn.cslg.report.common.model.vo.associate.AssoDepartPosVO;
 import lombok.Data;
-import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 
 import java.util.List;
@@ -16,8 +13,7 @@ import java.util.List;
 
 @Data
 @Accessors(chain = true)
-@EqualsAndHashCode(callSuper = true)
-public class PersonnelVO extends BaseVO {
+public class PersonnelVO {
     /**
      * ID
      */
@@ -54,36 +50,6 @@ public class PersonnelVO extends BaseVO {
     private Integer tenant;
 
     /**
-     * Front:担任职位
-     * Back:
-     */
-    private List<AssoDepartPosVO> positions;
-
-    /**
-     * Front:
-     * Back:用户组IdList
-     */
-    private List<Integer> group;
-
-    /**
-     * Front:
-     * Back:角色IdList
-     */
-    private List<Integer> role;
-
-    /**
-     * Front:
-     * Back:用户组信息List
-     */
-    private List<UserGroupVO> groupList;
-
-    /**
-     * Front:
-     * Back:角色信息List
-     */
-    private List<RoleVO> roleList;
-
-    /**
      * Front:状态
      * Back:人员账号状态(1启用0停用)
      */
@@ -151,6 +117,10 @@ public class PersonnelVO extends BaseVO {
      * 部门职位List
      */
     private List<DP> dpList;
+    /**
+     * 角色类型(是否为管理角色)
+     */
+    private Integer roleType;
 
     /**
      * 部门职位绑定关系
@@ -190,19 +160,4 @@ public class PersonnelVO extends BaseVO {
         private Integer roleId;
     }
 
-    /**
-     * 角色类型(是否为管理角色)
-     */
-    private Integer roleType;
-
-    /**
-     * 部门IDList
-     */
-    private List<Integer> departmentIds;
-
-    /**
-     * 职位IDList
-     */
-    private List<Integer> positionIds;
-
 }

+ 147 - 4
RMS/src/main/java/cn/cslg/report/common/utils/CacheUtils.java

@@ -1,28 +1,171 @@
 package cn.cslg.report.common.utils;
 
 import cn.cslg.report.common.core.base.RedisConf;
+import cn.cslg.report.common.model.dto.CommonData;
+import cn.cslg.report.common.model.dto.ProjectFieldOrderDTO;
+//import cn.cslg.report.common.model.dto.analysis.AnalysisItemResultDTO;
+//import cn.cslg.report.domain.PatentField;
+//import cn.cslg.report.domain.User;
 import cn.cslg.report.common.model.vo.PersonnelVO;
 import cn.dev33.satoken.exception.NotLoginException;
-import com.alibaba.fastjson2.JSONObject;
+import cn.hutool.core.lang.tree.Tree;
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.crypto.SecureUtil;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Component
 public class CacheUtils {
+
     @Resource
     private RedisUtil redisUtil;
 
-    public void setLoginUser(PersonnelVO personnelVO) {
-        redisUtil.set(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + personnelVO.getId(), JsonUtils.objectToJson(personnelVO));
+    public void setCommonData(CommonData commonData) {
+        redisUtil.set(RedisConf.COMMON_DATA, JsonUtils.objectToJson(commonData));
+    }
+
+    public CommonData getCommonData() {
+        String json = redisUtil.get(RedisConf.COMMON_DATA);
+        if (StringUtils.isEmpty(json)) {
+            return null;
+        } else {
+            return JsonUtils.jsonToPojo(json, CommonData.class);
+        }
     }
 
+//    public void setLoginUser(User user) {
+//        redisUtil.set(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + user.getId(), JsonUtils.objectToJson(user));
+//    }
+//
     public PersonnelVO getLoginUser(Object userId) {
         String json = redisUtil.get(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + userId);
         if (StringUtils.isEmpty(json)) {
             throw new NotLoginException("无数据", "user", "");
         } else {
-            return JSONObject.parseObject(json, PersonnelVO.class);
+            return com.alibaba.fastjson2.JSONObject.parseObject(json, PersonnelVO.class);
         }
     }
+
+    public  PersonnelVO getLoginUserPersonnel(Object userId) {
+        String json = redisUtil.get(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + userId);
+        if (StringUtils.isEmpty(json)) {
+            throw new NotLoginException("无数据", "user", "");
+        } else {
+            return com.alibaba.fastjson2.JSONObject.parseObject(json, PersonnelVO.class);
+        }
+    }
+
+    public String getSelectPatentIds(String patentKey) {
+        return redisUtil.get(RedisConf.SELECT_PATENT + RedisConf.SYMBOL_COLON + patentKey);
+    }
+
+    public Integer getSelectPatentNum(String patentKey) {
+        String ids = getSelectPatentIds(patentKey);
+        if (StringUtils.isEmpty(ids)) {
+            return 0;
+        }
+        return StringUtils.changeStringToInteger(ids, ",").size();
+    }
+
+//    public void setAnalysisCountV2(String patentKey, Integer field, Integer expand, String value, String ids) {
+//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
+//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
+//        if (StringUtils.isNotEmpty(value) && StringUtils.isNotEmpty(ids)) {
+//            redisUtil.hPut(key, value, ids);
+//            redisUtil.expire(key, 7200, TimeUnit.SECONDS);
+//        }
+//    }
+//
+//    public Object getAnalysisCountIdsV2(String patentKey, Integer field, Integer expand, String name) {
+//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
+//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
+//        return redisUtil.hGet(key, name);
+//    }
+//
+//    public List<AnalysisItemResultDTO> getAnalysisCountV2(String patentKey, Integer field, Integer expand) {
+//        List<AnalysisItemResultDTO> resultList = new ArrayList<>();
+//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
+//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
+//        Map<Object, Object> result = redisUtil.hGetAll(key);
+//        for (Object name : result.keySet()) {
+//            Object value = result.get(name);
+//            if (StringUtils.isNotNull(value)) {
+//                List<Integer> ids = StringUtils.changeStringToInteger(value.toString(), ",");
+//                AnalysisItemResultDTO analysisItemResultDTO = new AnalysisItemResultDTO();
+//                analysisItemResultDTO.setIds(value.toString());
+//                analysisItemResultDTO.setName(name.toString());
+//                analysisItemResultDTO.setCount(ids.size());
+//                resultList.add(analysisItemResultDTO);
+//            }
+//        }
+//        return resultList;
+//    }
+
+    public List<Tree> setAreaTreeList(List<Tree<Integer>> treeList) {
+        redisUtil.set(RedisConf.COMMON_DATA + RedisConf.SYMBOL_COLON + RedisConf.AREA_LIST, JsonUtils.objectToJson(treeList));
+        return this.getAreaTreeList();
+    }
+
+    public List<Tree> getAreaTreeList() {
+        String json = redisUtil.get(RedisConf.COMMON_DATA + RedisConf.SYMBOL_COLON + RedisConf.AREA_LIST);
+        if (StringUtils.isNotEmpty(json)) {
+            return JsonUtils.jsonToList(json, Tree.class);
+        }
+        return null;
+    }
+
+    public String setSelectPatentIds(List<Integer> patentIds) {
+        String uuid = IdUtil.simpleUUID();
+        redisUtil.setEx(RedisConf.SELECT_PATENT + RedisConf.SYMBOL_COLON + uuid, StringUtils.join(patentIds, ","), 43200, TimeUnit.SECONDS);
+        return uuid;
+    }
+
+    public void deleteUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId) {
+        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
+        redisUtil.delete(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key);
+    }
+
+//    public void setUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId, List<PatentField> defaultField) {
+//        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
+//        redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(defaultField));
+//    }
+
+//    public List<PatentField> getUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId) {
+//        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
+//        String json = redisUtil.get(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key);
+//        if (StringUtils.isNotEmpty(json)) {
+//            return JsonUtils.jsonToList(json, PatentField.class);
+//        }
+//        return new ArrayList<>();
+//    }
+
+    public List<ProjectFieldOrderDTO> getProjectFieldOrder(Integer projectId, Integer userId) {
+        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + userId);
+        String json = redisUtil.get(RedisConf.FIELD_ORDER + RedisConf.SYMBOL_COLON + key);
+        if (StringUtils.isNotEmpty(json)) {
+            return JsonUtils.jsonToList(json, ProjectFieldOrderDTO.class);
+        }
+        return new ArrayList<>();
+    }
+
+    public void setProjectFieldOrder(Integer projectId, Integer userId, List<ProjectFieldOrderDTO> data) {
+        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + userId);
+        redisUtil.set(RedisConf.FIELD_ORDER + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(data));
+    }
+
+    public void setUserImportId(Integer userId, Integer importId) {
+        redisUtil.set(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId, String.valueOf(importId));
+    }
+
+    public String getUserImportId(Integer userId) {
+        return redisUtil.get(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId);
+    }
+
+    public void deleteUserImport(Integer userId) {
+        redisUtil.delete(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId);
+    }
 }

+ 57 - 40
RMS/src/main/java/cn/cslg/report/common/utils/auth/AuthAop.java

@@ -1,19 +1,22 @@
 package cn.cslg.report.common.utils.auth;
 
-
+import cn.cslg.report.common.model.DataSource;
 import cn.cslg.report.common.model.vo.PersonnelVO;
 import cn.cslg.report.common.utils.CacheUtils;
 import cn.cslg.report.common.utils.Response;
-import cn.cslg.report.controller.DataController;
-import cn.cslg.report.domain.DataSource;
-import cn.dev33.satoken.stp.StpUtil;
+import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import io.swagger.v3.oas.annotations.Operation;
-import org.aspectj.lang.JoinPoint;
+import okhttp3.FormBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
 import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.*;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -24,6 +27,7 @@ import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import java.lang.reflect.Method;
 import java.util.List;
+import java.util.Objects;
 
 @Order(2)
 @Aspect
@@ -31,10 +35,11 @@ import java.util.List;
 public class AuthAop {
     @Value("${authorUrl}")
     private String url;
-    @Autowired
-    private DataController dataController;
+
     @Autowired
     private CacheUtils cacheUtils;
+    @Autowired
+    private LoginUtils loginUtils;
 
     /**
      * 定义切点
@@ -44,18 +49,19 @@ public class AuthAop {
 
     }
 
-    @Before("annotationPointcut()")
-    public void beforePointcut(JoinPoint joinPoint) {
-        //此处进入到方法前  可以实现一些业务逻辑
-        //获取目标对象方法参数
-
-    }
-
     /**
      * @param joinPoint 当前执行的方法
      */
     @Around("annotationPointcut()")
     public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
+
+        //获得登录人信息
+        PersonnelVO personnelVO = cacheUtils.getLoginUserPersonnel(loginUtils.getId());
+              if(personnelVO.getState()==0){
+                  return Response.error("登录账号已被禁用,请联系管理员启用");
+              }
+
+
         // 是否通过切面过滤标记
         Boolean isPass = true;
         MethodSignature ms = (MethodSignature) joinPoint.getSignature();
@@ -68,31 +74,53 @@ public class AuthAop {
         String functionId = myAnnotation.FunId();
         //获得执行方法的参数对象
         Object[] args = joinPoint.getArgs();
-        //根据登录人的id以及功能id获得计算逻辑
-        //根据 登录人的Id和功能名获得规则
-        String resBody = dataController.queryDataRule(StpUtil.getLoginIdAsInt(), functionId).toString();
+        //根据登录人的id以及功能id获得规则信息
+        //将登录人的id以及功能id放入requestBody中
+        RequestBody requestBody = new FormBody.Builder()
+                .add("loginId", loginUtils.getId().toString())
+                .add("functionId", functionId)
+                .build();
+        //建立远程连接
+        OkHttpClient okHttpClient = new OkHttpClient();
+        //发送请求
+        Request request = new Request.Builder()
+                .url(url + "/permission/api/data/queryDataRule")
+                .addHeader("Cookie", LoginUtils.getToken())
+                .post(requestBody)
+                .build();
+        //获得请求结果
+        String resBody = Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
         JSONArray jsonArray = JSONArray.parseArray(resBody);
         //如果获得规则的返回值为[-1]则代表登录人没有使用该功能的权限
         if (jsonArray.get(0).toString().equals("-1")) {
             return Response.error("没有" + operAnnotation.summary() + "的功能");
         }
         //如果获得规则的返回值为[0],则直接通过判断
-        else if (jsonArray.size() == 1 && jsonArray.get(0).toString().equals("0")) {
+        else if (jsonArray.size() == 1 && jsonArray.get(0).equals("0")) {
             return joinPoint.proceed();
         }
         // 如果查询结果的size大于0证明有限制逻辑
-        if (jsonArray.size() > 0) {
-
+     if (jsonArray.size() > 0) {
+            RequestBody reBodySource = new FormBody.Builder()
+                    .add("tableName", "local")
+                    .build();
             //处理jsonObject,变为(x==y)&&(z==t)的形式 ,并用js引擎进行boolean判断
-            List<DataSource> resSource = dataController.getDataSourceByTableName("local");
-            JSONArray jsonArray1 = JSON.parseArray(JSON.toJSONString(resSource));
+            //建立连接去获得字典信息
+            OkHttpClient okHttpClientSou = new OkHttpClient();
+         //发送请求
+            Request requestSou = new Request.Builder()
+                    .url(url + "/permission/api/data/getDataSource")
+                    .post(reBodySource)
+                    .addHeader("Cookie", LoginUtils.getToken())
+                    .build();
+            //获得请求返回
+            String resSource = Objects.requireNonNull(okHttpClientSou.newCall(requestSou).execute().body()).string();
+
+            JSONArray jsonArray1 = JSON.parseArray(resSource);
             // 获得字典
             List<DataSource> dataSources = jsonArray1.toJavaList(DataSource.class);
-            //获得登录人信息
-            PersonnelVO personnelVO = cacheUtils.getLoginUser(StpUtil.getLoginIdAsInt());
-
+         //循环遍历将多个规则拼接起来
             StringBuilder sqlStr = new StringBuilder();
-            //循环遍历将多个规则拼接起来
             for (int i = 0; i < jsonArray.size(); i++) {
                 //将数据库里存的规则转换为可识别的判断逻辑
                 String sql = TreeUtils.reCompute(JSONObject.parseObject(jsonArray.get(i).toString()), args, dataSources, personnelVO);
@@ -102,8 +130,8 @@ public class AuthAop {
             ScriptEngineManager manager = new ScriptEngineManager();
             //根据名字获得引擎
             ScriptEngine engine = manager.getEngineByName("javascript");
-            //进行判断,生成判断结果并将判断结果赋给isPass
-            Object result = engine.eval(sqlStr.toString());
+         //进行判断,生成判断结果并将判断结果赋给isPass
+            Object result = engine.eval(sqlStr.toString());//进行判断
             isPass = (Boolean) result;
         }
 
@@ -116,16 +144,5 @@ public class AuthAop {
         return joinPoint.proceed();
     }
 
-    /**
-     * 在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
-     *
-     * @param joinPoint 切点
-     */
-    @AfterReturning("annotationPointcut()")
-    public void doAfterReturning(JoinPoint joinPoint) {
-    }
-
-    private void checkToken(String token) {
-    }
 }
 

+ 3 - 4
RMS/src/main/java/cn/cslg/report/common/utils/auth/TreeUtils.java

@@ -1,7 +1,7 @@
 package cn.cslg.report.common.utils.auth;
 
+import cn.cslg.report.common.model.DataSource;
 import cn.cslg.report.common.model.vo.PersonnelVO;
-import cn.cslg.report.domain.DataSource;
 import com.alibaba.fastjson.JSONObject;
 import org.springframework.stereotype.Component;
 
@@ -37,7 +37,6 @@ public class TreeUtils {
             String field = jsonObject.get("field").toString();
             //获得规则的栏位值,并且要对值进行识别操作
             String value = distinguishFields(jsonObject.get("value").toString(), dataSource, personnelVO);
-            //获得规则的运算符
             if (jsonObject.get("opr").toString().equals("FIND_IN_SET")) {
                 sql = "FIND_IN_SET(" + value + "," + field + ")";
             } else {
@@ -106,7 +105,7 @@ public class TreeUtils {
         else {
             String field = jsonLeft.get("field").toString();
             //获得规则的 value字段,并进行识别操作
-            String value = distinguishFields(jsonLeft.get("value").toString(), dataSource, personnelVO);
+            String value = distinguishFields(jsonLeft.get("value").toString(), dataSource, personnelVO); //没有的话解析字符串拼接成子sql
             if (jsonLeft.get("opr").toString().equals("FIND_IN_SET")) {
                 str1 = "FIND_IN_SET(" + value + "," + field + ")";
             } else {
@@ -203,7 +202,7 @@ public class TreeUtils {
                         Class<?> personClass = personnelVO.getClass();
                         Field dataField = personClass.getDeclaredField(sourceField);
                         dataField.setAccessible(true);
-                        reField = field.replace("local." + sourceField, dataField.get(personnelVO).toString());
+                        reField = reField.replace("local." + sourceField, dataField.get(personnelVO).toString());
                     }
                 }
             }