✨ 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

+1
View File
@@ -333,6 +333,7 @@ CREATE TABLE `sys_menu` (
`icon` varchar(32) CHARACTER SET utf8mb4 DEFAULT NULL,
`sort_order` int DEFAULT '1' COMMENT '排序值',
`keep_alive` char(1) CHARACTER SET utf8mb4 DEFAULT '0',
`visible` char(1) CHARACTER SET utf8mb4 DEFAULT '1',
`menu_type` char(1) CHARACTER SET utf8mb4 DEFAULT '0',
`create_by` varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT ' ' COMMENT '创建人',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
@@ -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;
}
@@ -64,9 +64,10 @@ public class PigxResourceServerAutoConfiguration implements ApplicationContextAw
* @return OAuth2MethodSecurityExpressionHandler
*/
@Bean
public OAuth2MethodSecurityExpressionHandler expressionHandler(){
public OAuth2MethodSecurityExpressionHandler expressionHandler() {
OAuth2MethodSecurityExpressionHandler oAuth2MethodSecurityExpressionHandler = new OAuth2MethodSecurityExpressionHandler();
oAuth2MethodSecurityExpressionHandler.setApplicationContext(applicationContext);
return oAuth2MethodSecurityExpressionHandler;
}
}
@@ -103,6 +103,12 @@ public class SysMenu extends Model<SysMenu> {
@ApiModelProperty(value = "路由缓冲")
private String keepAlive;
/**
* 菜单显示隐藏控制
*/
@ApiModelProperty(value = "菜单是否显示")
private String visible;
/**
* 创建人
*/
@@ -155,6 +155,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
extra.put("label", menu.getName());
extra.put("sortOrder", menu.getSortOrder());
extra.put("keepAlive", menu.getKeepAlive());
extra.put("visible", menu.getVisible());
node.setExtra(extra);
return node;
};
@@ -31,6 +31,7 @@
<result column="sort_order" property="sortOrder"/>
<result column="menu_type" property="menuType"/>
<result column="keep_alive" property="keepAlive"/>
<result column="visible" property="visible"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="del_flag" property="delFlag"/>
@@ -47,6 +48,7 @@
sys_menu.sort_order,
sys_menu.keep_alive,
sys_menu.menu_type,
sys_menu.visible,
sys_menu.create_time,
sys_menu.update_time,
sys_menu.del_flag
@@ -88,6 +88,7 @@
u.del_flag,
u.lock_flag,
u.tenant_id,
u.create_by,
u.create_time ucreate_time,
u.update_time uupdate_time,
r.role_id,
@@ -113,6 +114,7 @@
u.nickname,
u.name,
u.email,
u.create_by,
u.create_time ucreate_time,
u.update_time uupdate_time,
r.role_id,
@@ -158,6 +160,7 @@
u.wx_openid,
u.qq_openid,
u.dept_id,
u.create_by,
u.create_time ucreate_time,
u.update_time uupdate_time,
u.del_flag,
@@ -197,6 +200,7 @@
u.wx_openid,
u.qq_openid,
u.dept_id,
u.create_by,
u.create_time ucreate_time,
u.update_time uupdate_time,
u.del_flag,