DataScopeAspect.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.bizmatics.service.aop;
  2. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  3. import com.bizmatics.common.mvc.utils.ServletUtils;
  4. import com.bizmatics.common.spring.util.SpringContextUtils;
  5. import com.bizmatics.model.base.BaseEntity;
  6. import com.bizmatics.model.system.SysRole;
  7. import com.bizmatics.model.system.SysUser;
  8. import com.bizmatics.service.system.impl.TokenService;
  9. import com.bizmatics.service.config.security.LoginUser;
  10. import org.aspectj.lang.JoinPoint;
  11. import org.aspectj.lang.Signature;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.annotation.Before;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. import org.aspectj.lang.reflect.MethodSignature;
  16. import org.springframework.stereotype.Component;
  17. import java.lang.reflect.Method;
  18. import java.util.Objects;
  19. /**
  20. * 数据过滤处理
  21. *
  22. * @author ruoyi
  23. */
  24. @Aspect
  25. @Component
  26. public class DataScopeAspect
  27. {
  28. /**
  29. * 全部数据权限
  30. */
  31. public static final String DATA_SCOPE_ALL = "1";
  32. /**
  33. * 自定数据权限
  34. */
  35. public static final String DATA_SCOPE_CUSTOM = "2";
  36. /**
  37. * 部门数据权限
  38. */
  39. public static final String DATA_SCOPE_DEPT = "3";
  40. /**
  41. * 部门及以下数据权限
  42. */
  43. public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
  44. /**
  45. * 仅本人数据权限
  46. */
  47. public static final String DATA_SCOPE_SELF = "5";
  48. /**
  49. * 数据权限过滤关键字
  50. */
  51. public static final String DATA_SCOPE = "dataScope";
  52. // 配置织入点
  53. @Pointcut("@annotation(com.bizmatics.service.aop.DataScope)")
  54. public void dataScopePointCut()
  55. {
  56. }
  57. @Before("dataScopePointCut()")
  58. public void doBefore(JoinPoint point) throws Throwable
  59. {
  60. clearDataScope(point);
  61. handleDataScope(point);
  62. }
  63. protected void handleDataScope(final JoinPoint joinPoint)
  64. {
  65. // 获得注解
  66. DataScope controllerDataScope = getAnnotationLog(joinPoint);
  67. if (controllerDataScope == null)
  68. {
  69. return;
  70. }
  71. // 获取当前的用户
  72. LoginUser loginUser = SpringContextUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
  73. if (Objects.nonNull(loginUser))
  74. {
  75. SysUser currentUser = loginUser.getUser();
  76. // 如果是超级管理员,则不过滤数据
  77. if (Objects.nonNull(currentUser) && !currentUser.isAdmin())
  78. {
  79. dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
  80. controllerDataScope.userAlias());
  81. }
  82. }
  83. }
  84. /**
  85. * 数据范围过滤
  86. *
  87. * @param joinPoint 切点
  88. * @param user 用户
  89. * @param userAlias 别名
  90. */
  91. public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias)
  92. {
  93. StringBuilder sqlString = new StringBuilder();
  94. for (SysRole role : user.getRoles())
  95. {
  96. String dataScope = role.getDataScope();
  97. if (DATA_SCOPE_ALL.equals(dataScope))
  98. {
  99. sqlString = new StringBuilder();
  100. break;
  101. }
  102. else if (DATA_SCOPE_CUSTOM.equals(dataScope))
  103. {
  104. sqlString.append(StringUtils.format(
  105. " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
  106. role.getRoleId()));
  107. }
  108. else if (DATA_SCOPE_DEPT.equals(dataScope))
  109. {
  110. sqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId()));
  111. }
  112. else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
  113. {
  114. sqlString.append(StringUtils.format(
  115. " OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
  116. deptAlias, user.getDeptId(), user.getDeptId()));
  117. }
  118. else if (DATA_SCOPE_SELF.equals(dataScope))
  119. {
  120. if (StringUtils.isNotBlank(userAlias))
  121. {
  122. sqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, user.getUserId()));
  123. }
  124. else
  125. {
  126. // 数据权限为仅本人且没有userAlias别名不查询任何数据
  127. sqlString.append(" OR 1=0 ");
  128. }
  129. }
  130. }
  131. if (StringUtils.isNotBlank(sqlString.toString()))
  132. {
  133. Object params = joinPoint.getArgs()[0];
  134. if (Objects.nonNull(params) && params instanceof BaseEntity)
  135. {
  136. BaseEntity baseEntity = (BaseEntity) params;
  137. baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
  138. }
  139. }
  140. }
  141. /**
  142. * 是否存在注解,如果存在就获取
  143. */
  144. private DataScope getAnnotationLog(JoinPoint joinPoint)
  145. {
  146. Signature signature = joinPoint.getSignature();
  147. MethodSignature methodSignature = (MethodSignature) signature;
  148. Method method = methodSignature.getMethod();
  149. if (method != null)
  150. {
  151. return method.getAnnotation(DataScope.class);
  152. }
  153. return null;
  154. }
  155. /**
  156. * 拼接权限sql前先清空params.dataScope参数防止注入
  157. */
  158. private void clearDataScope(final JoinPoint joinPoint)
  159. {
  160. Object params = joinPoint.getArgs()[0];
  161. if (Objects.nonNull(params) && params instanceof BaseEntity)
  162. {
  163. BaseEntity baseEntity = (BaseEntity) params;
  164. baseEntity.getParams().put(DATA_SCOPE, "");
  165. }
  166. }
  167. }