✨ Introducing new features. 支持个人级别数据权限

This commit is contained in:
lbw committed 2022-11-04 16:03:09 +08:00
1 parent c7aaa31db8
commit d56dfc4a7b
11 files changed
+62 -26

No files matched your search

@@ -35,12 +35,22 @@ public class DataScope extends HashMap {
/**
* 限制范围的字段名称
*/
private String scopeName = "dept_id";
private String scopeDeptName = "dept_id";
/**
* 本人权限范围字段
*/
private String scopeUserName = "create_by";
/**
* 具体的数据范围
*/
private List<Long> deptIds = new ArrayList<>();
private List<Long> deptList = new ArrayList<>();
/**
* 具体查询的用户数据权限范围
*/
private String username;
/**
* 是否只查询本部门
@@ -63,13 +73,8 @@ public class DataScope extends HashMap {
return new DataScope();
}
public DataScope scopeName(String scopeName) {
this.scopeName = scopeName;
return this;
}
public DataScope deptIds(List<Long> deptIds) {
this.deptIds = deptIds;
this.deptList = deptIds;
return this;
}
@@ -17,8 +17,6 @@
package com.pig4cloud.pigx.common.data.datascope;
import java.util.List;
/**
* @author lengleng
* @date 2019-09-07
@@ -29,9 +27,9 @@ public interface DataScopeHandle {
/**
* 计算用户数据权限
* @param deptList 部门ID,如果为空表示没有任何数据权限。
* @param dataScope 数据权限设置
* @return 返回true表示无需进行数据过滤处理,返回false表示需要进行数据过滤
*/
Boolean calcScope(List<Long> deptList);
Boolean calcScope(DataScope dataScope);
}
@@ -2,6 +2,7 @@ package com.pig4cloud.pigx.common.data.datascope;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import lombok.Setter;
import org.apache.ibatis.executor.Executor;
@@ -36,24 +37,28 @@ public class DataScopeInnerInterceptor implements DataScopeInterceptor {
return;
}
String scopeName = dataScope.getScopeName();
List<Long> deptIds = dataScope.getDeptIds();
// 优先获取赋值数据
if (CollUtil.isEmpty(deptIds) && dataScopeHandle.calcScope(deptIds)) {
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope", dataScope.getFunc().getType(),
originalSql);
mpBs.sql(originalSql);
// 返回true 不拦截直接返回原始 SQL
if (dataScopeHandle.calcScope(dataScope)) {
return;
}
if (deptIds.isEmpty()) {
List<Long> deptIds = dataScope.getDeptList();
// 1.无数据权限限制,则直接返回 0 条数据
if (CollUtil.isEmpty(deptIds) && StrUtil.isBlank(dataScope.getUsername())) {
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2",
dataScope.getFunc().getType(), originalSql);
}
// 2.如果为本人权限则走下面
else if (StrUtil.isNotBlank(dataScope.getUsername())) {
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s'",
dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername());
}
// 3.都没有,则是其他权限,走下面
else {
String join = CollectionUtil.join(deptIds, ",");
originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)",
dataScope.getFunc().getType(), originalSql, scopeName, join);
dataScope.getFunc().getType(), originalSql, dataScope.getScopeDeptName(), join);
}
mpBs.sql(originalSql);
@@ -48,7 +48,12 @@ public enum DataScopeTypeEnum {
/**
* 本级
*/
OWN_LEVEL(3, "本级");
OWN_LEVEL(3, "本级"),
/**
* 本人
*/
SELF_LEVEL(4, "本人");
/**
* 类型
@@ -48,16 +48,19 @@ public class PigxDefaultDatascopeHandle implements DataScopeHandle {
/**
* 计算用户数据权限
* @param deptList
* @param dataScope 数据权限范围
* @return
*/
@Override
public Boolean calcScope(List<Long> deptList) {
public Boolean calcScope(DataScope dataScope) {
PigxUser user = SecurityUtils.getUser();
List<String> roleIdList = user.getAuthorities().stream().map(GrantedAuthority::getAuthority)
.filter(authority -> authority.startsWith(SecurityConstants.ROLE))
.map(authority -> authority.split(StrUtil.UNDERLINE)[1]).collect(Collectors.toList());
// 当前用户的角色为空
List<Long> deptList = dataScope.getDeptList();
// 当前用户的角色为空 , 返回false
if (CollectionUtil.isEmpty(roleIdList)) {
return false;
}
@@ -98,6 +101,11 @@ public class PigxDefaultDatascopeHandle implements DataScopeHandle {
if (DataScopeTypeEnum.OWN_LEVEL.getType() == dsType) {
deptList.add(user.getDeptId());
}
// 只查询本人
if (DataScopeTypeEnum.SELF_LEVEL.getType() == dsType) {
dataScope.setUsername(user.getUsername());
}
return false;
}