业务场景
公司给员工对哪些模块有访问权限,这个时候就需要在员工访问模块表的时候做数据过滤,
解决方案
我们可以通过平台提供的数据过滤占位符解决这个问题,新建一条数据行权限,过滤语句条件是占位符,再编写占位符的解析逻辑
1.初始化权限基础数据
package pro.shushi.pamirs.demo.core.init;
import com.google.common.collect.Lists;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.auth.api.constants.AuthConstants;
import pro.shushi.pamirs.auth.api.enmu.AuthGroupTypeEnum;
import pro.shushi.pamirs.auth.api.enmu.PermissionDataSourceEnum;
import pro.shushi.pamirs.auth.api.enmu.PermissionTypeEnum;
import pro.shushi.pamirs.auth.api.model.AuthGroup;
import pro.shushi.pamirs.auth.api.model.AuthRole;
import pro.shushi.pamirs.auth.api.model.ResourcePermission;
import pro.shushi.pamirs.boot.base.model.UeModule;
import pro.shushi.pamirs.boot.common.api.command.AppLifecycleCommand;
import pro.shushi.pamirs.boot.common.api.init.InstallDataInit;
import pro.shushi.pamirs.boot.common.api.init.UpgradeDataInit;
import pro.shushi.pamirs.demo.api.DemoModule;
import pro.shushi.pamirs.demo.core.placeholder.EmployeeModulePlaceholder;
import pro.shushi.pamirs.framework.common.utils.ObjectUtils;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.domain.module.ModuleDefinition;
import java.util.Collections;
import java.util.List;
@Slf4j
@Component
@Order(0)
public class DemoModuleBizInit implements InstallDataInit, UpgradeDataInit {
@Override
public List<String> modules() {
return Collections.singletonList(DemoModule.MODULE_MODULE);
}
@Override
public int priority() {
return 0;
}
@Override
public boolean init(AppLifecycleCommand command, String version) {
this.initAuth();
return true;
}
@Override
public boolean upgrade(AppLifecycleCommand command, String version, String existVersion) {
this.initAuth();
return true;
}
private void initAuth() {
AuthGroup authGroup = new AuthGroup();
authGroup.setName("测试权限组")
.setDisplayName("测试权限组")
.setType(AuthGroupTypeEnum.RUNTIME)
.setActive(true);
authGroup.createOrUpdate();
AuthRole authRole = new AuthRole();
authRole.setCode("TEST_ROLE_1")
.setName("测试角色")
.setRoleTypeCode(AuthConstants.ROLE_SYSTEM_TYPE_CODE)
.setPermissionDataSource(PermissionDataSourceEnum.CUSTOM)
.setActive(true);
authRole.createOrUpdate();
authRole.setGroups(Lists.newArrayList(authGroup));
authRole.fieldSave(AuthRole::getGroups);
ResourcePermission authPermission = new ResourcePermission();
authPermission.setName("测试模块权限过滤")
.setDomainExp(EmployeeModulePlaceholder.PLACEHOLDER)
.setModel(ModuleDefinition.MODEL_MODEL)
.setPermRead(true)
.setPermRun(true)
.setPermissionType(PermissionTypeEnum.ROW)
.setPermissionDataSource(PermissionDataSourceEnum.CUSTOM)
.setCanShow(true)
.setActive(true);
ResourcePermission authPermission2 = ObjectUtils.clone(authPermission);
authPermission2.setName("测试ue模块权限过滤").setModel(UeModule.MODEL_MODEL);
authGroup.setPermissions(Lists.newArrayList(authPermission, authPermission2));
authGroup.fieldSave(AuthGroup::getPermissions);
}
}
这里演示的
module
表比较特殊,需要同时设置ModuleDefinition
和UeModule
这2个模型做数据过滤
2.编写占位符拦截替换逻辑
package pro.shushi.pamirs.demo.core.placeholder;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.user.api.AbstractPlaceHolderParser;
@Component
public class EmployeeModulePlaceholder extends AbstractPlaceHolderParser {
public static final String PLACEHOLDER = "${employeeModulePlaceholder}";
protected String value() {
// TODO 可以根据业务数据动态拼写rsql条件,这里只是示例,请改成自己的查询条件
return "module != 'hidden_module'";
}
public Integer priority() {
return -this.namespace().length();
}
public Boolean active() {
return true;
}
public String namespace() {
return EmployeeModulePlaceholder.PLACEHOLDER;
}
}
3.在"用户管理"应用内给用户绑定新建的角色
4.如权限未生效,可在"权限"应用的角色管理处刷新权限
如何通过sql语句查看配置是否生效?
修改启动工程内的application-dev.xml配置开启sql日志,将日志级别从error改成debug后重启应用
logging:
level:
root: info
pro.shushi.pamirs.framework.connectors.data.mapper.GenericMapper: debug # mybatis sql日志
通过gql工具发起请求,以上面的逻辑示例
{
moduleQuery {
queryPage(
page: { size: 200, currentPage: 1 }
queryWrapper: { }
) {
content {
module
displayName
}
}
}
}
查看效果
Oinone社区 作者:nation原创文章,如若转载,请注明出处:https://doc.oinone.top/backend/4737.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验