SpDataScopeAspect.java
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.bs.common.aop;
import com.bs.common.annotation.SpDataScope;
import com.bs.common.core.domain.BaseEntity;
import com.bs.common.core.domain.model.LoginUser;
import com.bs.common.utils.SecurityUtils;
import com.bs.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* 服务商数据过滤处理
*
* @author system
*/
@Aspect
@Component
public class SpDataScopeAspect {
/**
* 数据权限过滤关键字
*/
public static final String DATA_SCOPE = "dataScope";
/**
* 驻店人员角色名称
*/
public static final String ROLE_STORE_STAFF = "驻店人员";
/**
* 总部运营角色名称
*/
public static final String ROLE_HQ_OPERATION = "总部运营";
@Before("@annotation(spDataScope)")
public void doBefore(JoinPoint point, SpDataScope spDataScope) throws Throwable {
clearDataScope(point);
handleDataScope(point, spDataScope);
}
protected void handleDataScope(final JoinPoint joinPoint, SpDataScope spDataScope) {
// 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNotNull(loginUser)) {
String spCode = loginUser.getSpCode();
String storeCode = loginUser.getStoreCode();
String spRoleName = loginUser.getSpRoleName();
Long userId = loginUser.getUserId();
// 如果登录信息中的服务商编码和门店编码为空,则无需处理
if (StringUtils.isEmpty(spCode) && StringUtils.isEmpty(storeCode)) {
return;
}
// 如果角色为空,也无需处理
if (StringUtils.isEmpty(spRoleName)) {
return;
}
StringBuilder sqlString = new StringBuilder();
String alias = spDataScope.alias();
if (StringUtils.isNotEmpty(alias)) {
alias = alias + ".";
} else {
alias = "";
}
if (ROLE_STORE_STAFF.equals(spRoleName)) {
// 驻店人员:只能看自己门店,本人所属服务商的自己数据
if (StringUtils.isNotEmpty(storeCode)) {
sqlString.append(StringUtils.format(" OR {}store_code = '{}' AND {}sp_code = '{}' AND {}create_by_id = '{}' ", alias, storeCode, alias, spCode, alias, userId));
}
} else if (ROLE_HQ_OPERATION.equals(spRoleName)) {
// 总部运营:查看服务商关联的门店数据
if (StringUtils.isNotEmpty(spCode)) {
if (null == storeCode || storeCode.isEmpty()) {
sqlString.append(StringUtils.format(" OR {}sp_code = '{}' ", alias, spCode));
} else {
sqlString.append(StringUtils.format(" OR {}store_code = '{}' AND {}sp_code = '{}' ", alias, storeCode, alias, spCode));
}
}
}
if (StringUtils.isNotBlank(sqlString.toString())) {
Object[] args = joinPoint.getArgs();
if (args != null) {
for (Object arg : args) {
if (StringUtils.isNotNull(arg) && arg instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) arg;
baseEntity.getParams().put(DATA_SCOPE, " (" + sqlString.substring(4) + ")");
}
}
}
}
}
}
/**
* 拼接权限sql前先清空params.dataScope参数防止注入
*/
private void clearDataScope(final JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
if (args != null) {
for (Object arg : args) {
if (StringUtils.isNotNull(arg) && arg instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) arg;
baseEntity.getParams().put(DATA_SCOPE, "");
}
}
}
}
}