DataScopeAspect.java 5.0 KB

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