MyDataPermissionHandler.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.xjrsoft.common.handler;
  2. import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
  3. import com.xjrsoft.common.annotation.DataPermission;
  4. import com.xjrsoft.module.authority.entity.DataAuth;
  5. import com.xjrsoft.module.authority.service.IDataAuthService;
  6. import net.sf.jsqlparser.expression.Expression;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Lazy;
  9. import org.springframework.stereotype.Component;
  10. import java.lang.reflect.Method;
  11. /**
  12. * @Description 自定义数据权限处理
  13. * @Author: tzx
  14. * @Date: 2023/2/6 16:54
  15. */
  16. public class MyDataPermissionHandler implements DataPermissionHandler {
  17. @Override
  18. public Expression getSqlSegment(Expression where, String mappedStatementId) {
  19. try {
  20. Class<?> clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf(".")));
  21. DataPermission mapperAnnotation = clazz.getAnnotation(DataPermission.class);
  22. //如果mapper 没有数据权限注解 不需要设置
  23. if(mapperAnnotation == null){
  24. return where;
  25. }
  26. // String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1);
  27. // Method[] methods = clazz.getDeclaredMethods();
  28. // for (Method method : methods) {
  29. // if (!methodName.equals(method.getName())) {
  30. // continue;
  31. // }
  32. // // 获取自定义注解,无此注解则不控制数据权限
  33. // DataPermission annotation = method.getAnnotation(DataPermission.class);
  34. // if (annotation == null) {
  35. // continue;
  36. // }
  37. //
  38. // }
  39. } catch (ClassNotFoundException e) {
  40. e.printStackTrace();
  41. }
  42. return where;
  43. }
  44. }