疯狂的狮子li
2021-05-29 b7bd67b4cb9be191a2af45b02dd45d8cfba2efc3
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
@@ -3,7 +3,6 @@
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
@@ -19,6 +18,7 @@
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Map;
/**
 * 数据过滤处理
@@ -68,6 +68,7 @@
    @Before("dataScopePointCut()")
    public void doBefore(JoinPoint point) throws Throwable
    {
      clearDataScope(point);
        handleDataScope(point);
    }
@@ -144,13 +145,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 +164,30 @@
        }
        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))
      {
         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) {
            // 方法未找到 不处理
         }
      }
   }
}