This commit is contained in:
2023-08-14 23:23:36 +08:00
parent 029838fa65
commit b8e29fcb5a
18 changed files with 156 additions and 13 deletions

View File

@@ -0,0 +1,19 @@
package cn.lihongjie.coal.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface OrgScope {
boolean value() default true;
}

View File

@@ -0,0 +1,103 @@
package cn.lihongjie.coal.aop;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.exception.BizException;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.hibernate.Filter;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
@Slf4j
public class OrgScopeAop {
@PersistenceContext
EntityManager entityManager;
private static ThreadLocal<Boolean> orgScope = new ThreadLocal<>();
@SneakyThrows
@Around(value = "@annotation(cn.lihongjie.coal.annotation.OrgScope) || @within(cn.lihongjie.coal.annotation.OrgScope)")
public Object beforeOrgScope(ProceedingJoinPoint pjp) {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
OrgScope annotation = method.getAnnotation(OrgScope.class);
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(OrgScope.class);
}
if (annotation != null) {
Boolean old = orgScope.get();
orgScope.set(annotation.value());
try {
return pjp.proceed();
} finally {
orgScope.set(old);
}
} else {
return pjp.proceed();
}
}
@Before(value = "@annotation(org.springframework.transaction.annotation.Transactional))")
public void beforeTransactionMethod() {
if (orgScope.get() != null) {
Session session = entityManager.unwrap(Session.class);
if (orgScope.get()) {
if (StringUtils.isEmpty(Ctx.currentUser().getOrganizationId())) {
throw new BizException("当前用户未绑定机构, 无法进行机构数据过滤");
}
if (session.getEnabledFilter("orgFilter") == null) {
Filter orgFilter = session.enableFilter("orgFilter");
orgFilter.setParameter("organizationId", Ctx.currentUser().getOrganizationId());
} else {
log.debug("当前session {} orgFilter已经启用, 忽略....", session.toString());
}
} else {
if (session.getEnabledFilter("orgFilter") != null) {
session.disableFilter("orgFilter");
} else {
log.debug("当前session {} orgFilter已经禁用, 忽略....", session.toString());
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
@OrgScope
@Transactional
@RestController
public abstract class BaseController {
}

View File

@@ -19,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@SysLog(module = "配煤")
@Anonymous
public class CoalBlendController {
public class CoalBlendController extends BaseController{
@Autowired
CoalBlendService service;

View File

@@ -16,7 +16,7 @@ import java.util.List;
@RestController
@RequestMapping("/coal")
@Anonymous
public class CoalController {
public class CoalController extends BaseController{
@Autowired
CoalService coalService;

View File

@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@SysLog(module = "煤参数配置")
@Anonymous
public class CoalParameterDefController {
public class CoalParameterDefController extends BaseController{
@Autowired
CoalParameterDefService service;

View File

@@ -19,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@SysLog(module = "洗煤报告表")
@Anonymous
public class CoalWashingDailyAnalysisController {
public class CoalWashingDailyAnalysisController extends BaseController{
@Autowired
CoalWashingDailyAnalysisService service;

View File

@@ -1,5 +1,6 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.service.DepartmentService;
@@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/department")
@RestController
@SysLog(module = "部门管理")
@OrgScope
public class DepartmentController {
@Autowired

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dictionary")
@SysLog(module = "数据字典管理")
public class DictionaryController {
public class DictionaryController extends BaseController{
@Autowired

View File

@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class LoginController {
public class LoginController extends BaseController{
@Autowired
SessionService service;

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/organization")
@RestController
@SysLog(module = "机构管理")
public class OrganizationController {
public class OrganizationController extends BaseController{
@Autowired
OrganizationService service;

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/permission")
@RestController
@SysLog(module = "权限管理")
public class PermissionController {
public class PermissionController extends BaseController{
@Autowired
PermissionService service;

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/resource")
@RestController
@SysLog(module = "资源管理")
public class ResourceController {
public class ResourceController extends BaseController{
@Autowired
ResourceService service;

View File

@@ -1,10 +1,12 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,7 +15,9 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/role")
@RestController
@SysLog(module = "角色管理")
public class RoleController {
@OrgScope
@Transactional
public class RoleController extends BaseController{
@Autowired
RoleService service;

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/script")
@RestController
@SysLog(module = "")
public class ScriptController {
public class ScriptController extends BaseController{
@Autowired
ScriptService service;

View File

@@ -1,5 +1,6 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.service.SupplierService;
@@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/supplier")
@RestController
@SysLog(module = "供应商")
@OrgScope
public class SupplierController {
@Autowired

View File

@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/sysConfig")
@RestController
@SysLog(module = "系统参数")
public class SysConfigController {
public class SysConfigController extends BaseController{
@Autowired
SysConfigService service;

View File

@@ -1,5 +1,6 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.service.UserService;
@@ -13,7 +14,8 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
@SysLog(module = "用户管理")
public class UserController {
@OrgScope
public class UserController extends BaseController{
@Autowired
UserService service;