|
@@ -7,10 +7,12 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.ims.eval.cache.CacheContext;
|
|
|
import com.ims.eval.entity.DataDictionary;
|
|
|
import com.ims.eval.entity.OrganizationEvaluationRule;
|
|
|
+import com.ims.eval.entity.OrganizationStructure;
|
|
|
import com.ims.eval.entity.custom.PostUser;
|
|
|
import com.ims.eval.entity.dto.response.MyuserResDTO;
|
|
|
import com.ims.eval.entity.dto.result.PagResult;
|
|
|
import com.ims.eval.feign.RemoteServiceBuilder;
|
|
|
+import com.ims.eval.service.IOrganizationStructureService;
|
|
|
import com.ims.eval.service.custom.PostUserService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
@@ -38,6 +40,7 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.sql.Connection;
|
|
@@ -59,6 +62,8 @@ public class PermissionAspect implements Interceptor {
|
|
|
private HttpServletRequest request;
|
|
|
@Autowired
|
|
|
private PostUserService postUserService;
|
|
|
+// @Resource
|
|
|
+// private IOrganizationStructureService organizationStructureService;
|
|
|
|
|
|
//// @Resource
|
|
|
// private IUserService userService;
|
|
@@ -73,14 +78,18 @@ public class PermissionAspect implements Interceptor {
|
|
|
private final static String DEPT_MARK = "BM0001";//部门
|
|
|
private final static String POS_MARK = "GW0001";//岗位
|
|
|
|
|
|
- /** start 以下定义的数据需要和部门表一致 **/
|
|
|
+ /**
|
|
|
+ * start 以下定义的数据需要和部门表一致
|
|
|
+ **/
|
|
|
//全部数据权限
|
|
|
private static final Integer DATA_SCOPE_ALL = 1;
|
|
|
//部门数据权限
|
|
|
private static final Integer DATA_SCOPE_DEPT = 2;
|
|
|
//个人数据权限
|
|
|
private static final Integer DATA_SCOPE_CUSTOM = 3;
|
|
|
- /** end 以下定义的数据需要和部门表一致 **/
|
|
|
+ /**
|
|
|
+ * end 以下定义的数据需要和部门表一致
|
|
|
+ **/
|
|
|
|
|
|
//请求头code
|
|
|
private String code;
|
|
@@ -98,33 +107,34 @@ public class PermissionAspect implements Interceptor {
|
|
|
}
|
|
|
|
|
|
|
|
|
- Map<String,JSONObject> userIdMap = new HashMap<>();
|
|
|
+ Map<String, JSONObject> userIdMap = new HashMap<>();
|
|
|
|
|
|
//前置通知
|
|
|
@Before("pointcut()")
|
|
|
public void beforeMethod(JoinPoint joinPoint) {
|
|
|
- ServletRequestAttributes attributes =
|
|
|
- (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
|
|
|
- code = request.getHeader("code");
|
|
|
- bladeAuth = request.getHeader("Blade-Auth");
|
|
|
+ ServletRequestAttributes attributes =
|
|
|
+ (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
|
|
|
+ code = request.getHeader("code");
|
|
|
+ bladeAuth = request.getHeader("Blade-Auth");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object intercept(Invocation invocation) throws Throwable {
|
|
|
|
|
|
//部门权限
|
|
|
- MyuserResDTO user = getSysUser(code,bladeAuth);
|
|
|
+ MyuserResDTO user = getSysUser(code, bladeAuth);
|
|
|
DataDictionary dept = getSysDept(user);
|
|
|
|
|
|
+
|
|
|
//岗位权限
|
|
|
DataDictionary post = null;
|
|
|
// 比较Scope字段并取最大值
|
|
|
Integer scope = 2;//1:全部;2部门;3个人
|
|
|
String organizationId = "";//组织id
|
|
|
- if(null !=user){
|
|
|
+ if (null != user) {
|
|
|
organizationId = user.getUnitId();
|
|
|
- if(null != dept){
|
|
|
+ if (null != dept) {
|
|
|
scope = dept.getScope();
|
|
|
}
|
|
|
|
|
@@ -132,10 +142,10 @@ public class PermissionAspect implements Interceptor {
|
|
|
post = getSysPost(postUserList);
|
|
|
|
|
|
// 比较Scope字段并取最大值
|
|
|
- if(null != post ){
|
|
|
- if("JCDW".equals(post.getKeyValue())){
|
|
|
+ if (null != post) {
|
|
|
+ if ("JCDW".equals(post.getKeyValue())) {
|
|
|
scope = post.getScope();
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
scope = Integer.min(scope, post.getScope());
|
|
|
}
|
|
|
|
|
@@ -144,13 +154,13 @@ public class PermissionAspect implements Interceptor {
|
|
|
|
|
|
|
|
|
//权限最大值不需要后续处理
|
|
|
- if (scope.equals(DATA_SCOPE_ALL)){
|
|
|
+ if (scope.equals(DATA_SCOPE_ALL)) {
|
|
|
return invocation.proceed();
|
|
|
}
|
|
|
|
|
|
|
|
|
//人员和部门数据为空
|
|
|
- if ( user == null || (dept == null && null == post)) {
|
|
|
+ if (user == null || (dept == null && null == post)) {
|
|
|
// try {
|
|
|
// return invocation.proceed();
|
|
|
// } catch (Exception e) {
|
|
@@ -161,9 +171,12 @@ public class PermissionAspect implements Interceptor {
|
|
|
}
|
|
|
|
|
|
if (scope.equals(DATA_SCOPE_DEPT)) {
|
|
|
+ List<String> childList = CacheContext.getChildList(user.getUnitId());
|
|
|
+ CacheContext.childCompanyId.put(user.getId(),user.getUnitId());
|
|
|
+
|
|
|
String deptId = "";
|
|
|
- if(null != dept){
|
|
|
- deptId = dept.getDataKey();
|
|
|
+ if (null != dept) {
|
|
|
+ deptId = dept.getDataKey();
|
|
|
}
|
|
|
|
|
|
try {
|
|
@@ -193,31 +206,31 @@ public class PermissionAspect implements Interceptor {
|
|
|
String newName = mappedStatement.getId().substring(mappedStatement.getId().lastIndexOf(".") + 1, mappedStatement.getId().length());
|
|
|
Class<?> clazz = Class.forName(newId);
|
|
|
DataPermission dataPermission = null;
|
|
|
- if (!methodNames.containsKey(newId + "-" + newName)){
|
|
|
+ if (!methodNames.containsKey(newId + "-" + newName)) {
|
|
|
for (Method method : clazz.getDeclaredMethods()) {
|
|
|
//方法是否含有DataPermission注解,如果含有注解则将数据结果过滤
|
|
|
if (method.isAnnotationPresent(DataPermission.class)) {
|
|
|
dataPermission = method.getAnnotation(DataPermission.class);
|
|
|
if (dataPermission != null) {
|
|
|
methodNames.put(newId + "-" + method.getName(), dataPermission.isPermission());
|
|
|
- methodNames.put(newId + "-" + method.getName()+"type", dataPermission.type());
|
|
|
- methodNames.put(newId + "-" + method.getName()+"zbtype", dataPermission.zbtype());
|
|
|
- methodNames.put(newId + "-" + method.getName()+"jbtype", dataPermission.jbtype());
|
|
|
- methodNames.put(newId + "-" + method.getName()+"bktype", dataPermission.bktype());
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "type", dataPermission.type());
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "zbtype", dataPermission.zbtype());
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "jbtype", dataPermission.jbtype());
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "bktype", dataPermission.bktype());
|
|
|
}
|
|
|
} else {
|
|
|
methodNames.put(newId + "-" + method.getName(), true);
|
|
|
- methodNames.put(newId + "-" + method.getName()+"type", false);
|
|
|
- methodNames.put(newId + "-" + method.getName()+"zbtype", false);
|
|
|
- methodNames.put(newId + "-" + method.getName()+"jbtype", false);
|
|
|
- methodNames.put(newId + "-" + method.getName()+"bktype", false);
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "type", false);
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "zbtype", false);
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "jbtype", false);
|
|
|
+ methodNames.put(newId + "-" + method.getName() + "bktype", false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//是否开启数据权限
|
|
|
boolean isPermission = true;
|
|
|
- isPermission = null != methodNames.get(newId + "-" + newName) ?methodNames.get(newId + "-" + newName) :false;
|
|
|
+ isPermission = null != methodNames.get(newId + "-" + newName) ? methodNames.get(newId + "-" + newName) : false;
|
|
|
if (isPermission) {
|
|
|
// 获取到原始sql语句
|
|
|
String sql = statementHandler.getBoundSql().getSql();
|
|
@@ -227,15 +240,15 @@ public class PermissionAspect implements Interceptor {
|
|
|
|
|
|
// boolean jbtype = null != methodNames.get(newId + "-" + newName+"jbtype") ?methodNames.get(newId + "-" + newName+"jbtype") :false;
|
|
|
boolean type = null != methodNames.get(newId + "-" + newName + "type") ? methodNames.get(newId + "-" + newName + "type") : false;
|
|
|
- boolean zbtype = null != methodNames.get(newId + "-" + newName+"zbtype") ?methodNames.get(newId + "-" + newName+"zbtype") :false;
|
|
|
- boolean bktype = null != methodNames.get(newId + "-" + newName+"bktype") ?methodNames.get(newId + "-" + newName+"bktype") :false;
|
|
|
+ boolean zbtype = null != methodNames.get(newId + "-" + newName + "zbtype") ? methodNames.get(newId + "-" + newName + "zbtype") : false;
|
|
|
+ boolean bktype = null != methodNames.get(newId + "-" + newName + "bktype") ? methodNames.get(newId + "-" + newName + "bktype") : false;
|
|
|
|
|
|
|
|
|
- System.out.println("==========type="+type+";zbtype"+zbtype+";bktype"+bktype);
|
|
|
+ System.out.println("==========type=" + type + ";zbtype" + zbtype + ";bktype" + bktype);
|
|
|
|
|
|
|
|
|
if (null != post && post.getKeyValue().equals("JCDW")) {
|
|
|
- System.out.println("==========post.getKeyValue()="+post.getKeyValue());
|
|
|
+ System.out.println("==========post.getKeyValue()=" + post.getKeyValue());
|
|
|
if (type) {
|
|
|
sql = getSql(sql, organizationId, "JCDW");
|
|
|
}
|
|
@@ -248,7 +261,7 @@ public class PermissionAspect implements Interceptor {
|
|
|
|
|
|
List<OrganizationEvaluationRule> list = CacheContext.ruleMap.get(organizationId);
|
|
|
StringBuilder bis = new StringBuilder();
|
|
|
- if(null !=list && list.size()>0){
|
|
|
+ if (null != list && list.size() > 0) {
|
|
|
bis.append("(");
|
|
|
for (OrganizationEvaluationRule rule : list) {
|
|
|
rule.getBinSection();
|
|
@@ -258,11 +271,11 @@ public class PermissionAspect implements Interceptor {
|
|
|
}
|
|
|
bis.append(")");
|
|
|
}
|
|
|
- System.out.println("查询条件:"+bis.toString().replace(",)",")"));
|
|
|
- if(bis.toString().length()<=0){
|
|
|
- sql = getSql(sql,"('')", "bktype");
|
|
|
- }else {
|
|
|
- sql = getSql(sql, bis.toString().replace(",)",")"), "bktype");
|
|
|
+ System.out.println("查询条件:" + bis.toString().replace(",)", ")"));
|
|
|
+ if (bis.toString().length() <= 0) {
|
|
|
+ sql = getSql(sql, "('')", "bktype");
|
|
|
+ } else {
|
|
|
+ sql = getSql(sql, bis.toString().replace(",)", ")"), "bktype");
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -292,37 +305,42 @@ public class PermissionAspect implements Interceptor {
|
|
|
* @param sql 原SQL
|
|
|
* @return 新SQL
|
|
|
*/
|
|
|
- private String getSql(String sql, String deptId,String unitFlag) {
|
|
|
+ private String getSql(String sql, String deptId, String unitFlag) {
|
|
|
try {
|
|
|
// 修改原语句
|
|
|
- String condition ="";
|
|
|
+ String condition = "";
|
|
|
Select select = (Select) CCJSqlParserUtil.parse(sql);
|
|
|
- PlainSelect plainSelect = (PlainSelect)select.getSelectBody();
|
|
|
- Table table = (Table)plainSelect.getFromItem();
|
|
|
+ PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
|
|
|
+ Table table = (Table) plainSelect.getFromItem();
|
|
|
if (table.getAlias() != null) {
|
|
|
if (unitFlag.equals("JCDW")) {
|
|
|
|
|
|
- if(!"".equals(deptId.trim())){
|
|
|
+ if (!"".equals(deptId.trim())) {
|
|
|
condition = table.getAlias().getName() + "." + ORGANIZATION_ID + "='" + deptId + "'";
|
|
|
}
|
|
|
|
|
|
} else if (unitFlag.equals("bktype")) {
|
|
|
condition = table.getAlias().getName() + "." + BIN_SECTION + " in " + deptId;
|
|
|
} else {
|
|
|
- condition = "("+table.getAlias().getName() + "." + DEPT_ID + "='" + deptId + "' or "+table.getAlias().getName() + "." + DEPT_ID + "='PUBLIC')";;
|
|
|
+ condition = "(" + table.getAlias().getName() + "." + DEPT_ID + "='" + deptId + "' or " + table.getAlias().getName() + "." + DEPT_ID + "='PUBLIC')";
|
|
|
+ ;
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
condition = table.getAlias().getName() + "." + DEPT_ID + "=''";
|
|
|
}
|
|
|
|
|
|
+ /*if("".equals(condition)){
|
|
|
+ return plainSelect.toString();
|
|
|
+ }*/
|
|
|
+
|
|
|
//取得原SQL的where条件
|
|
|
final Expression expression = plainSelect.getWhere();
|
|
|
//增加新的where条件
|
|
|
final Expression envCondition = CCJSqlParserUtil.parseCondExpression(condition);
|
|
|
//envCondition.toString().contains(envCondition.toString());
|
|
|
System.out.println();
|
|
|
- if(StringUtils.isNotBlank(envCondition.toString()) && !expression.toString().contains(envCondition.toString())){
|
|
|
+ if (StringUtils.isNotBlank(envCondition.toString()) && !expression.toString().contains(envCondition.toString())) {
|
|
|
if (expression == null) {
|
|
|
plainSelect.setWhere(envCondition);
|
|
|
} else {
|
|
@@ -349,52 +367,53 @@ public class PermissionAspect implements Interceptor {
|
|
|
}
|
|
|
|
|
|
@Cacheable(value = "user_code")
|
|
|
- public MyuserResDTO getSysUser(String code,String bladeAuth) {
|
|
|
- if (StringUtils.isBlank(code) || code.toLowerCase().equals("null")){
|
|
|
+ public MyuserResDTO getSysUser(String code, String bladeAuth) {
|
|
|
+ if (StringUtils.isBlank(code) || code.toLowerCase().equals("null")) {
|
|
|
return null;
|
|
|
}
|
|
|
- JSONObject json = postUserService.getSysUser(code,bladeAuth,request);
|
|
|
+ JSONObject json = postUserService.getSysUser(code, bladeAuth, request);
|
|
|
MyuserResDTO user = JSONObject.parseObject(json.getJSONObject("data").toJSONString(), MyuserResDTO.class);
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
- @Cacheable(cacheNames = "user_id",key= "#userId")
|
|
|
+ @Cacheable(cacheNames = "user_id", key = "#userId")
|
|
|
public List<PostUser> getUserPostList(String userId) {
|
|
|
JSONObject postuserjson = null;
|
|
|
- if(null == userIdMap.get(userId)){
|
|
|
- postuserjson = (JSONObject)postUserService.getUserPostList(null,null,userId,request);
|
|
|
- userIdMap.put(userId,postuserjson);
|
|
|
- }else {
|
|
|
+ if (null == userIdMap.get(userId)) {
|
|
|
+ postuserjson = (JSONObject) postUserService.getUserPostList(null, null, userId, request);
|
|
|
+ userIdMap.put(userId, postuserjson);
|
|
|
+ } else {
|
|
|
postuserjson = userIdMap.get(userId);
|
|
|
}
|
|
|
|
|
|
- if(null !=postuserjson){
|
|
|
- PagResult pagResult = JSONObject.parseObject(postuserjson.toJSONString(), PagResult.class);
|
|
|
+ if (null != postuserjson) {
|
|
|
+ PagResult pagResult = JSONObject.parseObject(postuserjson.toJSONString(), PagResult.class);
|
|
|
List<PostUser> list = pagResult.getRecords().toJavaList(PostUser.class);
|
|
|
return list;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- @Cacheable(cacheNames = "user_code_dept",key="#user.id")
|
|
|
- public DataDictionary getSysDept(MyuserResDTO user){
|
|
|
- if (user != null && CacheContext.ddSuperKeyMap.containsKey(DEPT_MARK)){
|
|
|
- Optional<DataDictionary> any = CacheContext.ddSuperKeyMap.get(DEPT_MARK).stream().filter(t -> t.getKeyName().equals(user.getDeptName())).findAny();
|
|
|
- return any.isPresent() ? any.get() : CacheContext.ddMap.get("LSBM");//如果没有匹配上部门,赋予零时部门权限
|
|
|
+ @Cacheable(cacheNames = "user_code_dept", key = "#user.id")
|
|
|
+ public DataDictionary getSysDept(MyuserResDTO user) {
|
|
|
+ if (user != null && CacheContext.ddSuperKeyMap.containsKey(DEPT_MARK)) {
|
|
|
+// Optional<DataDictionary> any = CacheContext.ddSuperKeyMap.get(DEPT_MARK).stream().filter(t -> t.getKeyName().equals(user.getDeptName())).findAny();
|
|
|
+ Optional<DataDictionary> any = CacheContext.ddSuperKeyMap.get(DEPT_MARK).stream().filter(t -> user.getDeptName().contains(t.getKeyName())).findAny();
|
|
|
+ return any.isPresent() ? any.get() : CacheContext.ddMap.get("LSBM");//如果没有匹配上部门,赋予零时部门权限
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Cacheable(value = "user_id_post")
|
|
|
- public DataDictionary getSysPost(List<PostUser> postUsers){
|
|
|
- if (postUsers != null && postUsers.size()>0 && CacheContext.ddSuperKeyMap.containsKey(POS_MARK)){
|
|
|
- List<DataDictionary> list = CacheContext.ddSuperKeyMap.get(POS_MARK).stream()
|
|
|
+ public DataDictionary getSysPost(List<PostUser> postUsers) {
|
|
|
+ if (postUsers != null && postUsers.size() > 0 && CacheContext.ddSuperKeyMap.containsKey(POS_MARK)) {
|
|
|
+ List<DataDictionary> list = CacheContext.ddSuperKeyMap.get(POS_MARK).stream()
|
|
|
.filter(data -> postUsers.stream().anyMatch(user -> user.getPosCode().equals(data.getDataKey()))).collect(Collectors.toList());
|
|
|
- DataDictionary maxScopeData = list.stream().min(Comparator.comparing(DataDictionary::getScope))
|
|
|
+ DataDictionary maxScopeData = list.stream().min(Comparator.comparing(DataDictionary::getScope))
|
|
|
.orElse(null);
|
|
|
return maxScopeData;
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
//没有查到对应的岗位,赋值一般用户岗位
|
|
|
return CacheContext.ddMap.get("61138");
|
|
|
}
|