定义注解

  1. /**
  2. * 数据权限过滤注解
  3. */
  4. @Target(ElementType.METHOD)
  5. @Retention(RetentionPolicy.RUNTIME)
  6. @Documented
  7. public @interface CompanyDateScope {
  8. }

切面实现

  1. package com.ruoyi.framework.aspectj;
  2. import com.ruoyi.common.utils.StringUtils;
  3. import com.ruoyi.common.utils.security.ShiroUtils;
  4. import com.ruoyi.framework.aspectj.lang.annotation.CompanyDateScope;
  5. import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
  6. import com.ruoyi.framework.web.domain.BaseEntity;
  7. import com.ruoyi.project.system.user.domain.User;
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.Signature;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.aspectj.lang.reflect.MethodSignature;
  14. import org.springframework.stereotype.Component;
  15. import java.lang.reflect.Method;
  16. @Aspect
  17. @Component
  18. public class CompanyDataScopeAspect {
  19. /**
  20. * 数据权限过滤关键字
  21. */
  22. public static final String DATA_SCOPE = "dataScope";
  23. /**
  24. * @param joinPoint
  25. * @param companyId
  26. */
  27. public static void dataScopeFilter(JoinPoint joinPoint, Long companyId) {
  28. String filterSql = StringUtils.format("and company_id = {}", companyId);
  29. Object params = joinPoint.getArgs()[0];
  30. if (StringUtils.isNotNull(params) && params instanceof BaseEntity) {
  31. BaseEntity baseEntity = (BaseEntity) params;
  32. baseEntity.getParams().put(DATA_SCOPE, filterSql);
  33. }
  34. }
  35. // 配置织入点
  36. @Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.CompanyDateScope)")
  37. public void dataScopePointCut() { }
  38. @Before("dataScopePointCut()")
  39. public void doBefore(JoinPoint point) throws Throwable {
  40. handleDataScope(point);
  41. }
  42. protected void handleDataScope(final JoinPoint joinPoint) {
  43. // 获得注解
  44. CompanyDateScope controllerDataScope = getAnnotationLog(joinPoint);
  45. if (controllerDataScope == null) {
  46. return;
  47. }
  48. // 获取当前的用户
  49. User currentUser = ShiroUtils.getSysUser();
  50. // 如果公司id大于0说明要过滤数据
  51. if (currentUser != null && currentUser.getCompanyId() != 0) {
  52. dataScopeFilter(joinPoint, currentUser.getCompanyId());
  53. }
  54. }
  55. /**
  56. * 是否存在注解,如果存在就获取
  57. */
  58. private CompanyDateScope getAnnotationLog(JoinPoint joinPoint) {
  59. Signature signature = joinPoint.getSignature();
  60. MethodSignature methodSignature = (MethodSignature) signature;
  61. Method method = methodSignature.getMethod();
  62. if (method != null) {
  63. return method.getAnnotation(CompanyDateScope.class);
  64. }
  65. return null;
  66. }
  67. }

在 mapper 方法上增加注解

  1. /**
  2. * 产品Mapper接口
  3. */
  4. public interface ProductMapper {
  5. ...
  6. @CompanyDateScope
  7. List<ProductVO> selectProductList(ProductQO product);
  8. ...
  9. }

在 where 最后面增加过滤数据的 sql 语句

  1. ...
  2. <select id="selectProductList" parameterType="com.ruoyi.project.api.domain.qo.ProductQO" resultMap="ProductResult">
  3. <include refid="selectProductVo"/>
  4. <where>
  5. <if test="name != null and name != ''">and name like concat('%', #{name}, '%')</if>
  6. ${params.dataScope}
  7. </where>
  8. </select>
  9. ...