引言
权限管理是企业级应用的核心功能之一,OneCode 3.0通过esdright
模块构建了一套灵活可扩展的权限引擎,支持组件、模块、流程等多维度的权限控制。本文将深入剖析其实现架构、核心组件及设计思想。
总体架构设计
OneCode权限引擎采用分层设计,通过类型化定义、树形结构展示和服务化处理构建完整权限体系:
1. 权限类型体系
通过RightType
枚举定义三大核心权限类型,采用枚举扩展模式支持未来功能扩展:
public enum RightType implements TreeItem {
comright("组件权限", "bpmfont bpmgongzuoliuzhutiguizeweihuguanli", true, false, false, AttributeNavService.class),
moduleright("模块权限", "bpmfont bpmgongzuoliuxitongpeizhi", true, false, false, MenuFormulaService.class),
bpmright("流程授权", "spafont spa-icon-app", true, false, false, BPMTreeRightIndex.class);
// ... 类型定义与绑定类实现
}
每种权限类型绑定对应的处理类,实现职责分离。
2. 权限树结构设计
采用树形结构展示权限层级关系,核心实现包括:
- BPMRightTree:流程权限树节点,关联流程定义ID和活动定义ID
- ComRightTree:组件权限树节点,支持属性类型和公式类型双重分类
- ModuleRightTree:模块权限树节点,关联模块定义信息
树节点通过@ChildTreeAnnotation
实现动态加载,如组件权限树绑定AttributeNavService
实现子节点加载:
@ChildTreeAnnotation(bindClass = AttributeNavService.class)
public ComRightTree(Attributetype attributetype, String currentClassName) {
this.caption = attributetype.getName();
this.currentClassName = currentClassName;
this.id = attributetype.getType();
this.imageClass = attributetype.getImageClass();
this.attributetype = attributetype;
}
核心服务实现
权限引擎通过多个服务类实现权限的管理、验证和控制逻辑:
1. 权限入口控制器
RightIndex
作为权限管理的入口控制器,提供三大权限类型的访问端点:
@Controller
@RequestMapping("/esd/right/")
@MethodChinaName(cname = "权限管理")
public class RightIndex {
@MethodChinaName(cname = "组件授权")
@RequestMapping(method = RequestMethod.POST, value = "ComRight")
public TreeListResultModel<List<ComRightTree>> getComRight(String id, String projectName, String currentClassName) { ... }
@MethodChinaName(cname = "模块授权")
@RequestMapping(method = RequestMethod.POST, value = "ModuleRight")
public TreeListResultModel<List<ModuleRightTree>> getModuleRight(String id, String projectName, String className) { ... }
@MethodChinaName(cname = "流程授权")
@RequestMapping(method = RequestMethod.POST, value = "BpmRight")
public TreeListResultModel<List<BPMRightTree>> getBpmRight(String id, String projectName, String className) { ... }
}
通过@RequestMapping
定义REST接口,使用TreePageUtil
构建树形返回结果。
2. 模块权限服务
ModuleFormulaService
实现模块权限的核心逻辑,包括表达式CRUD和权限验证:
- 表达式管理:支持权限表达式的添加、编辑、删除和查询
- 树形展示:通过
getFormulaTree
方法提供权限选择树 - 权限验证:通过ESDClient与后端交互验证权限
关键实现代码:
@RequestMapping(method = RequestMethod.POST, value = "Index")
public ListResultModel<List<ModuleInstGridView>> getFormulaInstList(String projectName) {
ListResultModel<List<ModuleInstGridView>> resultModel = new ListResultModel<>();
try {
Project config = ESDFacrory.getAdminESDClient().getProjectByName(projectName);
List<FormulaInst> list = config.getFormulas();
List<FormulaInst> instList = new ArrayList<>();
for (FormulaInst inst : list) {
if (inst.getFormulaType() != null && inst.getFormulaType().equals(FormulaType.ModuleRight)) {
instList.add(inst);
}
}
resultModel = PageUtil.getDefaultPageList(instList, ModuleInstGridView.class);
} catch (JDSException e) {
e.printStackTrace();
}
return resultModel;
}
3. 动作权限服务
ActionFormulaService
专注于组件动作级别的权限控制,实现细粒度的操作权限管理:
@RequestMapping(method = RequestMethod.POST, value = "Index")
public ListResultModel<List<ModuleActionGridView>> getFormulaInstList(String projectName, String currentClassName) {
// 从模块配置中筛选动作权限表达式
ProjectVersion version = ESDFacrory.getAdminESDClient().getProjectVersionByName(projectName);
EUModule euModule = ESDFacrory.getAdminESDClient().getModule(currentClassName, version.getVersionName());
List<ModuleFormulaInst> list = euModule.getComponent().getFormulas();
// ... 筛选与转换逻辑
}
组织权限集成
权限引擎与组织架构深度集成,通过org
包下的服务类实现基于组织的权限控制:
1. 流程权限集成
BPMTreeRightIndex
实现流程节点级别的权限控制,关联流程定义和活动定义:
@RequestMapping(method = RequestMethod.POST, value = "BPMRightNav")
public TreeListResultModel<List<BPMRightTree>> getOrgRightNav(String activityDefId, String processDefId, String projectName) {
return TreePageUtil.getTreeList(Arrays.asList(BPMRightFormulaType.values()), BPMRightTree.class);
}
2. 阅办人授权
ReadFormulaService
实现文档阅办权限的精细化控制,支持阅办人表达式的管理:
@RequestMapping(method = RequestMethod.POST, value = "Index")
public ListResultModel<List<ReadGridView>> getFormulaInstList(String projectId) {
// 筛选阅办人权限表达式
List<FormulaInst> instList = new ArrayList<>();
for (FormulaInst inst : list) {
if (inst != null && inst.getFormulaType() != null && inst.getFormulaType().equals(FormulaType.ReaderSelectedID)) {
instList.add(inst);
}
}
// ...
}
属性权限控制
AttributeNavService
实现基于属性的权限控制,支持不同属性类型的权限差异化管理:
@RequestMapping(method = RequestMethod.POST, value = "FormulaList")
public ListResultModel<List<PageRightGridView>> getFormulaList(FormulaType formulaType, String projectName, String currentClassName) {
// 根据公式类型筛选权限表达式
List<FormulaInst> instList = new ArrayList<>();
for (FormulaInst inst : list) {
if (inst != null && inst.getFormulaType() != null && inst.getFormulaType().equals(formulaType)) {
instList.add(inst);
}
}
// ...
}
权限表达式引擎
权限引擎的核心在于表达式处理,通过公式定义实现灵活的权限规则:
- 表达式类型:通过
FormulaType
枚举定义多种权限表达式类型 - 表达式管理:支持表达式的添加、编辑、删除和查询
- 表达式执行:通过
ESDFacrory.getAdminESDClient()
与后端交互执行权限验证
关键代码示例(添加表达式):
@RequestMapping(value = {"addFormula"}, method = {RequestMethod.GET, RequestMethod.POST})
public @ResponseBody ResultModel<Boolean> addFormulaInst(String SelectFormulaTree, String projectName) {
try {
Project config = ESDFacrory.getAdminESDClient().getProjectByName(projectName);
String[] formulaIdArr = StringUtility.split(SelectFormulaTree, ";");
for (String id : formulaIdArr) {
ParticipantSelect select = FormulaFactory.getInstance(ESDFacrory.getAdminESDClient().getSpace()).getFormulaById(id);
FormulaInst inst = new FormulaInst();
inst.setExpression(select.getFormula());
inst.setFormulaType(select.getFormulaType());
inst.setParticipantSelectId(select.getParticipantSelectId());
inst.setName(select.getSelectName());
inst.setFormulaInstId(UUID.randomUUID().toString());
ESDFacrory.getAdminESDClient().updateFormulaConfig(config.getId(),inst);
}
} catch (JDSException e) {
e.printStackTrace();
}
return resultModel;
}
与OneCode核心设计的契合
权限引擎深度融合OneCode的两大核心设计理念:
注解驱动开发:大量使用自定义注解实现权限元数据定义
@MethodChinaName
:定义权限中文名称@NavTreeViewAnnotation
:声明树形导航视图@APIEventAnnotation
:绑定API事件处理
元数据驱动开发:通过
FormulaInst
等元数据对象描述权限规则,实现配置化权限管理
总结与展望
OneCode 3.0权限引擎通过类型化、层次化、服务化的设计,构建了灵活强大的权限管理体系。核心优势包括:
- 多维度权限控制:支持组件、模块、流程等多层面权限管理
- 细粒度权限定义:从功能到数据的精细化权限控制
- 灵活的表达式引擎:通过公式定义实现复杂权限规则
- 与组织架构深度集成:基于组织的权限分配与控制
未来可进一步增强AI能力,如基于用户行为推荐权限配置、智能检测权限冲突等,提升权限管理的智能化水平。