瀏覽代碼

增加删除角色的判断

xiewj 2 年之前
父節點
當前提交
90de47ac51

+ 1 - 0
src/main/java/com/fdkankan/site/common/ResultCode.java

@@ -13,6 +13,7 @@ public enum ResultCode {
     FD_ERROR(4009,"四维登录失败"),
     DEL_MINE(4010,"删除自己"),
     DEL_DEF_ROLE(4011,"默认角色不允许"),
+    DEL_ROLE(4012,"角色已被成员绑定,请更换成员角色后进行删除"),
 
 
     PROJECT_CREATER_NOT_EXIST(5001,"项目创建人不存在"),

+ 2 - 1
src/main/java/com/fdkankan/site/controller/RoleController.java

@@ -98,7 +98,8 @@ public class RoleController extends BaseController {
     @PostMapping("/del")
     public ResultData del(@RequestBody RoleParam param){
         VUtils.isTure(param.getRoleId() == null).throwMessage(ResultCode.PARAM_MISS);
-        roleService.delRole(param.getRoleId());
+        VUtils.isTure(param.getProjectId() == null).throwMessage(ResultCode.PARAM_MISS);
+        roleService.delRole(param.getRoleId(),param.getProjectId());
         return ResultData.ok();
     }
 }

+ 119 - 0
src/main/java/com/fdkankan/site/interceptor/QueryStringEscapeInterceptor.java

@@ -0,0 +1,119 @@
+package com.fdkankan.site.interceptor;
+
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.fdkankan.site.util.EscapeUtil;
+import com.github.yulichang.query.MPJLambdaQueryWrapper;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
+import org.apache.ibatis.cache.CacheKey;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.*;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+
+@Component
+@Intercepts(
+        { @Signature(type = Executor.class, method = "query", args =
+                { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }),
+                @Signature(type = Executor.class, method = "query", args =
+                        { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }) })
+public class QueryStringEscapeInterceptor implements Interceptor
+{
+
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        // 拦截sql
+        Object[] args = invocation.getArgs();
+        MappedStatement statement = (MappedStatement)args[0];
+        Object parameterObject = args[1];
+        BoundSql boundSql = statement.getBoundSql(parameterObject);
+        String sql = boundSql.getSql();
+        // 处理特殊字符
+        modifyLikeSql(sql, parameterObject, boundSql);
+        // 返回
+        return invocation.proceed();
+    }
+
+    @Override
+    public Object plugin(Object target) {
+        return Plugin.wrap(target, this);
+    }
+
+    @Override
+    public void setProperties(Properties properties) {
+
+    }
+
+    @SuppressWarnings("unchecked")
+    public static String modifyLikeSql(String sql, Object parameterObject, BoundSql boundSql) {
+        if (parameterObject instanceof HashMap) {
+        } else {
+            return sql;
+        }
+        if (!sql.toLowerCase().contains(" like ") || !sql.toLowerCase().contains("?")) {
+            return sql;
+        }
+        // 获取关键字的个数(去重)
+        String[] strList = sql.split("\\?");
+        Set<String> keyNames = new HashSet<>();
+        for (int i = 0; i < strList.length; i++) {
+            if (strList[i].toLowerCase().contains(" like ")) {
+                String keyName = boundSql.getParameterMappings().get(i).getProperty();
+                keyNames.add(keyName);
+            }
+        }
+        // 对关键字进行特殊字符“清洗”,如果有特殊字符的,在特殊字符前添加转义字符(\)
+        for (String keyName : keyNames) {
+            HashMap parameter = (HashMap)parameterObject;
+            if (keyName.contains("ew.paramNameValuePairs.") && sql.toLowerCase().contains(" like ?")) {
+                // 第一种情况:在业务层进行条件构造产生的模糊查询关键字
+                if (parameter.get("ew") instanceof QueryWrapper){
+                    QueryWrapper wrapper = (QueryWrapper)parameter.get("ew");
+                    parameter = (HashMap)wrapper.getParamNameValuePairs();
+                }
+                else if (parameter.get("ew") instanceof LambdaQueryWrapper){
+                    LambdaQueryWrapper lambdaQueryWrapper = (LambdaQueryWrapper)parameter.get("ew");
+                    parameter = (HashMap)lambdaQueryWrapper.getParamNameValuePairs();
+                }
+                else if (parameter.get("ew") instanceof MPJLambdaWrapper){
+                    MPJLambdaWrapper mpjLambdaWrapper = (MPJLambdaWrapper)parameter.get("ew");
+                    parameter = (HashMap)mpjLambdaWrapper.getParamNameValuePairs();
+                }
+                String[] keyList = keyName.split("\\.");
+                // ew.paramNameValuePairs.MPGENVAL1,截取字符串之后,获取第三个,即为参数名
+                Object a = parameter.get(keyList[2]);
+                if (a instanceof String && (a.toString().contains("_") || a.toString().contains("\\") || a.toString()
+                        .contains("%"))) {
+                    parameter.put(keyList[2],
+                            "%" + EscapeUtil.escapeChar(a.toString().substring(1, a.toString().length() - 1)) + "%");
+                }
+            } else if (!keyName.contains("ew.paramNameValuePairs.") && sql.toLowerCase().contains(" like ?")) {
+                // 第二种情况:未使用条件构造器,但是在service层进行了查询关键字与模糊查询符`%`手动拼接
+                Object a = parameter.get(keyName);
+                if (a instanceof String && (a.toString().contains("_") || a.toString().contains("\\") || a.toString()
+                        .contains("%"))) {
+                    parameter.put(keyName,
+                            "%" + EscapeUtil.escapeChar(a.toString().substring(1, a.toString().length() - 1)) + "%");
+                }
+            } else {
+                // 第三种情况:在Mapper类的注解SQL中进行了模糊查询的拼接
+                Object a = parameter.get("param1");
+                if (a instanceof String && (a.toString().contains("_") || a.toString().contains("\\") || a.toString()
+                        .contains("%"))) {
+                    parameter.put(keyName, EscapeUtil.escapeChar(a.toString()));
+                }
+            }
+        }
+        return sql;
+    }
+}

+ 1 - 1
src/main/java/com/fdkankan/site/service/IRoleService.java

@@ -20,7 +20,7 @@ public interface IRoleService extends IService<Role> {
 
     Object pageList(RoleSearchParam param);
 
-    void delRole(Integer roleId);
+    void delRole(Integer roleId,Integer projectId);
 
     void delRoleByProjectId(Integer projectId);
 

+ 4 - 0
src/main/java/com/fdkankan/site/service/IUserRoleService.java

@@ -1,10 +1,13 @@
 package com.fdkankan.site.service;
 
+import com.amazonaws.services.dynamodbv2.xspec.L;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.fdkankan.site.entity.Marking;
 import com.fdkankan.site.entity.UserRole;
 import com.fdkankan.site.request.MarkingSearchParam;
 
+import java.util.List;
+
 /**
  * <p>
  *  服务类
@@ -20,5 +23,6 @@ public interface IUserRoleService extends IService<UserRole> {
 
 
     UserRole findByUserIdAndPorId(Integer userId,Integer projectId);
+    List<UserRole> findByRoleIdAndPorId(Integer userId, Integer projectId);
 
 }

+ 8 - 5
src/main/java/com/fdkankan/site/service/impl/RoleServiceImpl.java

@@ -13,10 +13,7 @@ import com.fdkankan.site.entity.dto.RoleDTO;
 import com.fdkankan.site.entity.vo.RoleSelect;
 import com.fdkankan.site.mapper.IRoleMapper;
 import com.fdkankan.site.request.RoleSearchParam;
-import com.fdkankan.site.service.IMenuService;
-import com.fdkankan.site.service.IProjectService;
-import com.fdkankan.site.service.IRoleMenuService;
-import com.fdkankan.site.service.IRoleService;
+import com.fdkankan.site.service.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -31,6 +28,9 @@ public class RoleServiceImpl extends ServiceImpl<IRoleMapper, Role> implements I
     IRoleMenuService roleMenuService;
     @Autowired
     IMenuService menuService;
+    @Autowired
+    IUserRoleService userRoleService;
+
     @Override
     public Object pageList(RoleSearchParam param) {
         VUtils.isTure(param.getProjectId() == null).throwMessage(ResultCode.PARAM_MISS);
@@ -47,7 +47,10 @@ public class RoleServiceImpl extends ServiceImpl<IRoleMapper, Role> implements I
     }
 
     @Override
-    public void delRole(Integer roleId) {
+    public void delRole(Integer roleId,Integer projectId) {
+        List<UserRole> userRoles = userRoleService.findByRoleIdAndPorId(roleId, projectId);
+        VUtils.isTure(userRoles.size()>0).throwMessage(ResultCode.DEL_ROLE);
+
         LambdaUpdateWrapper<Role> wrapper=new LambdaUpdateWrapper<>();
         wrapper.eq(Role::getRoleId,roleId);
         int delete = baseMapper.delete(wrapper);

+ 10 - 0
src/main/java/com/fdkankan/site/service/impl/UserRoleServiceImpl.java

@@ -9,6 +9,8 @@ import com.fdkankan.site.mapper.IUserRoleMapper;
 import com.fdkankan.site.service.IUserRoleService;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 @Service
 public class UserRoleServiceImpl extends ServiceImpl<IUserRoleMapper, UserRole> implements IUserRoleService {
 
@@ -33,4 +35,12 @@ public class UserRoleServiceImpl extends ServiceImpl<IUserRoleMapper, UserRole>
         wrapper.eq(UserRole::getProjectId,projectId);
         return getOne(wrapper);
     }
+
+    @Override
+    public List<UserRole> findByRoleIdAndPorId(Integer roleId, Integer projectId) {
+        LambdaQueryWrapper<UserRole> wrapper=new LambdaQueryWrapper<>();
+        wrapper.eq(UserRole::getRoleId,roleId);
+        wrapper.eq(UserRole::getProjectId,projectId);
+        return list(wrapper);
+    }
 }

+ 1 - 0
src/main/java/com/fdkankan/site/util/EscapeUtil.java

@@ -9,6 +9,7 @@ public class EscapeUtil {
             before = before.replaceAll("\\\\", "\\\\\\\\");
             before = before.replaceAll("_", "\\\\_");
             before = before.replaceAll("%", "\\\\%");
+            before = before.replaceAll("'", "\\\\'");
         }
         return before ;
     }