DataScopeAspect.java 5.3 KB

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