浏览代码

操作数据时, 检查操作权限

wuweihao 3 年之前
父节点
当前提交
8fe5726447

+ 3 - 0
gis_db/src/main/java/com/gis/db/service/ProjectService.java

@@ -16,4 +16,7 @@ public interface ProjectService extends IService<ProjectEntity> {
     Result saveEntity(ProjectDto param);
 
     Result del(Long id);
+
+    // 检查数据创建者
+    void checkCreatorId(Long creatorId);
 }

+ 10 - 8
gis_db/src/main/java/com/gis/db/service/impl/FieldServiceImpl.java

@@ -1,6 +1,5 @@
 package com.gis.db.service.impl;
 
-import cn.hutool.core.lang.Snowflake;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -25,9 +24,9 @@ import com.gis.db.entity.vo.FieldVo;
 import com.gis.db.mapper.DdlMapper;
 import com.gis.db.mapper.FieldMapper;
 import com.gis.db.service.FieldService;
+import com.gis.db.service.ProjectService;
 import com.gis.db.service.TableService;
 import lombok.extern.slf4j.Slf4j;
-import org.junit.Test;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -57,6 +56,9 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
     @Autowired
     IBaseService baseService;
 
+    @Autowired
+    ProjectService projectService;
+
 
 
     static final String TABLE_KEY = "table:";
@@ -83,16 +85,15 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
             entity.setCreatorId(baseService.getUserId());
             list.add(entity);
             tableId = dto.getTableId();
-//            this.save(entity);
         }
-        TableEntity tableEntity = tableService.getById(tableId);
-        BaseRuntimeException.isNull(tableEntity, ErrorEnum.FAILURE_CODE_3001);
+
+        String tableName = tableService.getTableName(tableId+"");
 
         this.saveBatch(list, 20);
-//        this.saveOrUpdateBatch(list, 20);
+
 
         // 添加字段到表
-        tableService.addField(list, tableEntity.getNameTable());
+        tableService.addField(list, tableName);
 
         // 删除缓存
         String fieldTableKey = FIELD_TABLE_KEY+tableId;
@@ -140,6 +141,8 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
 
         TableEntity tableEntity = tableService.getTableByFieldId(ids.get(0));
         BaseRuntimeException.isNull(tableEntity, null, "该表不存在");
+        // 检查操作权限
+        projectService.checkCreatorId(tableEntity.getCreatorId());
         String tableName = tableEntity.getNameTable();
         Long tableId = tableEntity.getId();
 
@@ -220,7 +223,6 @@ public class FieldServiceImpl extends ServiceImpl<FieldMapper, FieldEntity> impl
     @Override
     public Result updateRecord(RecordDto param) {
         String tableName = tableService.getTableName(param.getTableId());
-
         String id = param.getId();
         BaseRuntimeException.isBlank(id, null, "数据id不能为空");
 

+ 16 - 0
gis_db/src/main/java/com/gis/db/service/impl/ProjectServiceImpl.java

@@ -91,9 +91,12 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, ProjectEntity
         } else {
             entity = getById(id);
             BaseRuntimeException.isNull(entity, ErrorEnum.FAILURE_SYS_2001);
+            // 检查操作权限
+            checkCreatorId(entity.getCreatorId());
         }
         BeanUtils.copyProperties(param, entity);
 
+
         this.saveOrUpdate(entity);
         // 创建appId
         if (flag){
@@ -110,6 +113,10 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, ProjectEntity
             return Result.success();
         }
 
+        // 检查操作权限
+        checkCreatorId(entity.getCreatorId());
+
+
         // 删除逻辑
         List<TableEntity> tables = tableService.findByProjectId(id);
         ArrayList<String> tableNames = new ArrayList<>();
@@ -131,6 +138,15 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, ProjectEntity
         return Result.success();
     }
 
+    @Override
+    public void checkCreatorId(Long creatorId) {
+        Integer isAdmin = baseService.getIsAdmin();
+        if (isAdmin == 0){
+            Long userId = baseService.getUserId();
+            BaseRuntimeException.isTrue(!userId.equals(creatorId), null, "无权限操作");
+        }
+    }
+
     private String createAppId(Long id) {
         try {
             String appId = EncryptUtils.getEncrypt(APP_SALT , id.toString());

+ 7 - 1
gis_db/src/main/java/com/gis/db/service/impl/TableServiceImpl.java

@@ -19,6 +19,7 @@ import com.gis.db.entity.po.TableEntity;
 import com.gis.db.mapper.DdlMapper;
 import com.gis.db.mapper.TableMapper;
 import com.gis.db.service.FieldService;
+import com.gis.db.service.ProjectService;
 import com.gis.db.service.TableService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
@@ -45,6 +46,9 @@ public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> impl
     @Autowired
     IBaseService baseService;
 
+    @Autowired
+    ProjectService projectService;
+
 
     @Override
     public void addField(List<FieldEntity> param, String tableName) {
@@ -75,7 +79,6 @@ public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> impl
         param.setName(underLineCase);
 
         // 检查表名唯一性
-//        BaseRuntimeException.isTrue(isExistByName(param.getName()), ErrorEnum.FAILURE_SYS_2010);
         TableEntity entity = new TableEntity();
         BeanUtils.copyProperties(param, entity);
         entity.setCreatorId(baseService.getUserId());
@@ -155,6 +158,9 @@ public class TableServiceImpl extends ServiceImpl<TableMapper, TableEntity> impl
         String name = entity.getNameTable();
         BaseRuntimeException.isBlank(name, null, "表名为空, 请检查");
 
+        // 检查创建者数据
+        projectService.checkCreatorId(entity.getCreatorId());
+
         return name;
     }