疯狂的狮子li
2021-05-29 8cc2aee13e5f6e880ced3ba63034bf5bc74dce72
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
@@ -19,6 +19,7 @@
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Map;
/**
 * 数据过滤处理
@@ -68,6 +69,7 @@
    @Before("dataScopePointCut()")
    public void doBefore(JoinPoint point) throws Throwable
    {
      clearDataScope(point);
        handleDataScope(point);
    }
@@ -104,6 +106,10 @@
    {
        StringBuilder sqlString = new StringBuilder();
      // 将 "." 提取出,不写别名为单表查询,写别名为多表查询
      deptAlias = StrUtil.isNotBlank(deptAlias) ? deptAlias + "." : "";
      userAlias = StrUtil.isNotBlank(userAlias) ? userAlias + "." : "";
        for (SysRole role : user.getRoles())
        {
            String dataScope = role.getDataScope();
@@ -115,24 +121,24 @@
            else if (DATA_SCOPE_CUSTOM.equals(dataScope))
            {
                sqlString.append(StrUtil.format(
                        " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
                        " OR {}dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
                        role.getRoleId()));
            }
            else if (DATA_SCOPE_DEPT.equals(dataScope))
            {
                sqlString.append(StrUtil.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
                sqlString.append(StrUtil.format(" OR {}dept_id = {} ", deptAlias, user.getDeptId()));
            }
            else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
            {
                sqlString.append(StrUtil.format(
                        " OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
                        " OR {}dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
                        deptAlias, user.getDeptId(), user.getDeptId()));
            }
            else if (DATA_SCOPE_SELF.equals(dataScope))
            {
                if (StrUtil.isNotBlank(userAlias))
                {
                    sqlString.append(StrUtil.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
                    sqlString.append(StrUtil.format(" OR {}user_id = {} ", userAlias, user.getUserId()));
                }
                else
                {
@@ -144,13 +150,8 @@
        if (StrUtil.isNotBlank(sqlString.toString()))
        {
            Object params = joinPoint.getArgs()[0];
            if (Validator.isNotNull(params) && params instanceof BaseEntity)
            {
                BaseEntity baseEntity = (BaseEntity) params;
                baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
            }
        }
         putDataScope(joinPoint, sqlString.substring(4));
      }
    }
    /**
@@ -168,4 +169,35 @@
        }
        return null;
    }
   /**
    * 拼接权限sql前先清空params.dataScope参数防止注入
    */
   private void clearDataScope(final JoinPoint joinPoint)
   {
      Object params = joinPoint.getArgs()[0];
      if (Validator.isNotNull(params))
      {
         putDataScope(joinPoint, "");
      }
   }
   private static void putDataScope(JoinPoint joinPoint, String sql) {
      Object params = joinPoint.getArgs()[0];
      if (Validator.isNotNull(params))
      {
         if(params instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) params;
            baseEntity.getParams().put(DATA_SCOPE, sql);
         } else {
            try {
               Method getParams = params.getClass().getDeclaredMethod("getParams", null);
               Map<String, Object> invoke = (Map<String, Object>) getParams.invoke(params, null);
               invoke.put(DATA_SCOPE, sql);
            } catch (Exception e) {
               // 方法未找到 不处理
            }
         }
      }
   }
}