Quellcode durchsuchen

优化功能权限

4.0
FLYPHT vor 2 Jahren
Ursprung
Commit
4be822f190
7 geänderte Dateien mit 283 neuen und 36 gelöschten Zeilen
  1. +9
    -4
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/design/form/ModelFormCache.java
  2. +9
    -0
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/design/form/ModelFormHelper.java
  3. +19
    -0
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanContent.java
  4. +121
    -3
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanHelper.java
  5. +55
    -19
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanItemProvider.java
  6. +69
    -9
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/role/RoleHelper.java
  7. +1
    -1
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/role/RoleRightContent.java

+ 9
- 4
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/design/form/ModelFormCache.java Datei anzeigen

@@ -7,10 +7,7 @@ import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* Created by Akmm at 2022/1/12 18:34
@@ -102,4 +99,12 @@ public class ModelFormCache extends AbstractEntityCache<ModelForm> {
public ModelForm getByPageName(String pageName) {
return getByKey(mn, pageName.toUpperCase());
}
// 获取包含本页面的模块下的所有页面
public Set<ModelForm> getIncPage(long pageId){
ModelForm form = get(pageId);
if(form==null || form.getParent()<=0L){
return null;
}
return getListByModule(form.getParent());
}
}

+ 9
- 0
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/design/form/ModelFormHelper.java Datei anzeigen

@@ -21,6 +21,8 @@ import cc.smtweb.system.bpm.web.design.db.ModelCatalogCache;
import cc.smtweb.system.bpm.web.design.db.ModelProjectCache;
import cc.smtweb.system.bpm.web.design.form.define.*;
import cc.smtweb.system.bpm.web.engine.flow.entity.Comment;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanHelper;
import cc.smtweb.system.bpm.web.sys.user.role.RoleHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;

@@ -293,6 +295,13 @@ public class ModelFormHelper {
ret.put("label", form.getTitle());
ret.put("service", service);
ret.put("module", ModelProjectCache.getInstance().getModule(form.getPrjId()));
// 加载权限
long menuId = params.readLong("menuId");
String fullPath = params.readString("fullPath");
long findMenuId = MenuPlanHelper.findMenuId(menuId,form.getId(),fullPath);
Map<String,Object> permission = RoleHelper.getFuncAndFieldRight(us.getUserId(),us.getPartyId(),findMenuId);
ret.put("menuId",findMenuId>0L? findMenuId: menuId);
ret.put("permission",permission);
buildOptsEx(form, ret);
//form不用管
if (pageInfo.form != null) ret.put("form", pageInfo.form);


+ 19
- 0
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanContent.java Datei anzeigen

@@ -4,6 +4,8 @@ import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.system.bpm.web.sys.user.menu.Menu;
import cc.smtweb.system.bpm.web.sys.user.menu.MenuCache;

import java.util.ArrayList;
import java.util.Collections;
@@ -138,6 +140,23 @@ public class MenuPlanContent {
findHome(item.getChildren(),homeList);
}
}
// 查询指定页面
public void findPage(long pageId,List<MenuPlanItem> pageList){
findPage(list,pageList,pageId);
}
// 查询指定页面
private void findPage(List<MenuPlanItem> source,List<MenuPlanItem> pageList,long pageId){
if(CommUtil.isEmpty(source))return;
for(MenuPlanItem item: source){
if(item.getMenu()>0L){
Menu menu = MenuCache.getInstance().get(item.getMenu());
if(menu!=null&& menu.getPageId().equalsIgnoreCase(String.valueOf(pageId))){
pageList.add(item);
}
}
findPage(item.getChildren(),pageList,pageId);
}
}
private MenuPlanItem findById(long id, List<MenuPlanItem> itemList) {
if (CommUtil.isEmpty(itemList)) {
return null;


+ 121
- 3
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanHelper.java Datei anzeigen

@@ -1,5 +1,12 @@
package cc.smtweb.system.bpm.web.sys.user.menuPlan;

import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormCache;

import java.util.*;

/**
* @Author: tanghp
* @Date: 2022-09-26 10:19
@@ -13,9 +20,120 @@ public class MenuPlanHelper {
* @param fullPath 带参数的路径
* @return
*/
public long findMenuId(long menuId,long pageId,String fullPath){
//
public static long findMenuId(long menuId,long pageId,String fullPath){
MenuPlanItemProvider.MenuPlanItem2 mItem = MenuPlanItemProvider.getInstance().getMenuPlanItem(menuId);
if(mItem==null)return 0L;
// 1、如果是本页面的菜单 直接返回菜单ID
if(mItem.getPageId() == pageId){
return menuId;
}
// 获取本菜单对应的模块包含的所有页面
Set<ModelForm> children = ModelFormCache.getInstance().getIncPage(mItem.getPageId());
if(!CommUtil.isEmpty(children)){
for(ModelForm form : children){
//2、本页属于本菜单模块的子页面 也返回该菜单
if(form.getId() == pageId){
return menuId;
}
}
}
// 获取包含本页面的所有的菜单
List<MenuPlanItemProvider.MenuPlanItem2> pItems = findItemIncPage(mItem.getMpId(),pageId);
// 解析fullPath
UrlPath urlPath = new UrlPath(fullPath);
Map<Long, Map<String,Integer>> countMap = new HashMap<>();
for(MenuPlanItemProvider.MenuPlanItem2 item: pItems){
Map<String,Integer> map = new HashMap<>();
map.put("total",item.getParameterNames().size());
map.put("count",0);
item.getParameterNames().forEach(name->{
if(urlPath.getParamNames().contains(name)){
map.put("count",map.get("count")+1);
}
});
countMap.put(item.getId(),map);
// 3.参数总个数和命中个数 和配置一致直接返回
if(map.get("total").intValue() == map.get("count").intValue()&& map.get("count")==urlPath.paramNames.size()){
return item.getId();
}
}
// 记录最大命中数的菜单
int count = 0;
long rtMenuId=0L;
for(Map.Entry<Long,Map<String,Integer>> item: countMap.entrySet()){
// 4、参数命中个数 和 url的参数个数一致 也返回
if(item.getValue().get("count")==urlPath.paramNames.size()){
return item.getKey();
}
// 找最大命中数的菜单
if(count<item.getValue().get("count")){
count = item.getValue().get("count");
rtMenuId = item.getKey();
}
}
// 5.返回最大命中数的菜单
return rtMenuId;
}

/**
* 获取包含指定页面的所有明细
* @param mpId 菜单方案ID
* @param pageId 页面ID
* @return 明细集合
*/
public static List<MenuPlanItemProvider.MenuPlanItem2> findItemIncPage(long mpId, long pageId){
MenuPlan menuPlan = MenuPlanCache.getInstance().get(mpId);
if(menuPlan==null){
return new ArrayList<>();
}
List<MenuPlanItemProvider.MenuPlanItem2> rtList = new ArrayList<>();
List<MenuPlanItem> tempList = new ArrayList<>();
MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
menuPlanContent.findPage(pageId,tempList);
tempList.forEach(item->{
MenuPlanItemProvider.MenuPlanItem2 bean = new MenuPlanItemProvider.MenuPlanItem2();
bean.getData().putAll(item.getData());
bean.setMpId(mpId);
});
return rtList;
}
static class UrlPath{
private String path;
private Set<String> paramNames = new HashSet<>();

public UrlPath(String urlPath) {
String[] array = urlPath.split("\\?");
if(array.length>0){
path = array[0];
}
if(array.length>1){
String[] pArray = array[1].split("&");
for(String p1 : pArray){
String[] param = p1.split("=");
if(param.length>0){
String p = param[0];
if(StringUtil.isNotEmpty(p)){
paramNames.add(p);
}
}
}
}
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Set<String> getParamNames() {
return paramNames;
}

return 0L;
public void setParamNames(Set<String> paramNames) {
this.paramNames = paramNames;
}
}
}

+ 55
- 19
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/menuPlan/MenuPlanItemProvider.java Datei anzeigen

@@ -1,11 +1,10 @@
package cc.smtweb.system.bpm.web.sys.user.menuPlan;

import cc.smtweb.framework.core.cache.SessionCacheFactory;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.sys.user.menu.Menu;
import cc.smtweb.system.bpm.web.sys.user.menu.MenuCache;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* @Author: tanghp
@@ -13,33 +12,70 @@ import java.util.Map;
* @Desc: 菜单明细加载类
*/
public class MenuPlanItemProvider {
private volatile MenuPlanItemProvider provider;
private Map<Long, MenuPlanItem> map = new HashMap<>();
private Map<Long, MenuPlanItem2> map = new HashMap<>();
private MenuPlanItemProvider() {
}
public MenuPlanItemProvider getInstance(){
if (provider == null) {
synchronized (SessionCacheFactory.class) {
if (provider == null) {
provider = new MenuPlanItemProvider();
}
}
}
return provider;

public static MenuPlanItemProvider getInstance(){
return ProviderHolder.provider;
}

// 获取菜单方案明细ID
public MenuPlanItem getMenuPlanItem(long menuId){
MenuPlanItem menuPlanItem = map.get(menuId);
public MenuPlanItem2 getMenuPlanItem(long menuId){
MenuPlanItem2 menuPlanItem = map.get(menuId);
if(menuPlanItem!=null)return menuPlanItem;
Collection<MenuPlan> plans = MenuPlanCache.getInstance().getAll();
if(CommUtil.isEmpty(plans))return null;
for(MenuPlan menuPlan: plans){
menuPlanItem = MenuPlanCache.getInstance().getById(menuPlan.getEntityId(),menuId);
if(menuPlanItem!=null){
MenuPlanItem item = MenuPlanCache.getInstance().getById(menuPlan.getEntityId(),menuId);
if(item!=null){
menuPlanItem = new MenuPlanItem2();
menuPlanItem.getData().putAll(item.getData());
menuPlanItem.setMpId(menuPlan.getEntityId());
map.put(menuId,menuPlanItem);
return menuPlanItem;
}
}
return null;
}

private static final class ProviderHolder {
static final MenuPlanItemProvider provider = new MenuPlanItemProvider();
}

public static class MenuPlanItem2 extends MenuPlanItem{
// 获取菜单方案ID
public long getMpId(){
return getLong("mp_id");
}
public void setMpId(long mp_id){
put("mp_id",mp_id);
}
// 获取页面ID
public long getPageId(){
if(getMenu()>0L){
Menu menu = MenuCache.getInstance().get(getMenu());
if(menu!=null){
return Long.parseLong(menu.getPageId());
}
}
return 0L;
}
// 获取路径的参数name
public Set<String> getParameterNames(){
Set<String> rtSet = new HashSet<>();
if(getMenu()>0L){
Menu menu = MenuCache.getInstance().get(getMenu());
if(menu!=null){
String urlPath = menu.getPath();
rtSet = getNamesByPath(urlPath);
}
}
return rtSet;
}
private Set<String> getNamesByPath(String url){
MenuPlanHelper.UrlPath urlPath = new MenuPlanHelper.UrlPath(url);
return urlPath.getParamNames();
}
}
}

+ 69
- 9
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/role/RoleHelper.java Datei anzeigen

@@ -1,17 +1,16 @@
package cc.smtweb.system.bpm.web.sys.user.role;

import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.sys.user.dataRight.DataRightDefine;
import cc.smtweb.system.bpm.web.sys.user.dataRight.DataRightDefineCache;
import cc.smtweb.system.bpm.web.sys.user.dataRightGroup.DataRightGroup;
import cc.smtweb.system.bpm.web.sys.user.dataRightGroup.DataRightGroupCache;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlan;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanCache;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanContent;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanItem;
import cc.smtweb.system.bpm.web.sys.user.menuPlan.*;
import cc.smtweb.system.bpm.web.sys.user.user.User;
import cc.smtweb.system.bpm.web.sys.user.user.UserCache;
import cc.smtweb.system.bpm.web.sys.user.user.UserRoleCache;

import javax.print.DocFlavor;
import java.util.*;

/**
@@ -134,17 +133,16 @@ public final class RoleHelper {
* 获取用户指定类型的数据权限组
* @param userId 用户ID
* @param partyId 登录的单位
* @param menuId 菜单ID
* @param menuId 菜单明细ID
* @param dsType 数据权限类型编码
* @return 数据权限组集合
*/
public static Set<DataRightGroup> getDataRightGroup(long userId,long partyId,long menuId,String dsType){
Set<Long> mpIds = getMenuPlanIds(userId,partyId);
if(mpIds.size()==0){
MenuPlanItemProvider.MenuPlanItem2 menuPlanItem2 =MenuPlanItemProvider.getInstance().getMenuPlanItem(menuId);
if(menuPlanItem2 ==null){
return new HashSet<>();
}
// 没传菜单方案ID,随机取一个菜单方案
return getDataRightGroup(userId,partyId,menuId,dsType,mpIds.iterator().next());
return getDataRightGroup(userId,partyId,menuId,dsType,menuPlanItem2.getMpId());
}

/**
@@ -179,4 +177,66 @@ public final class RoleHelper {
});
return new HashSet<>(mapGroup.values());
}

/**
* 获取功能权限和字段权限
* @param userId 用户ID
* @param partyId 登录单位ID
* @param menuId 菜单明细ID
* @return
*/
public static Map<String,Object> getFuncAndFieldRight(long userId,long partyId,long menuId){
MenuPlanItemProvider.MenuPlanItem2 menuPlanItem2 =MenuPlanItemProvider.getInstance().getMenuPlanItem(menuId);
if(menuPlanItem2 ==null){
return new HashMap<>();
}
return getFuncAndFieldRight(userId,partyId,menuId,menuPlanItem2.getMpId());
}

/**
* 获取功能权限和字段权限
* @param userId 用户ID
* @param partyId 登录单位ID
* @param menuId 菜单明细ID
* @param menuPlanId 菜单方案ID
* @return
*/
public static Map<String,Object> getFuncAndFieldRight(long userId,long partyId,long menuId,long menuPlanId){
Set<Long> roleIds = UserRoleCache.getInstance().getRoleIdByUP(userId,partyId);
RoleCache roleCache = RoleCache.getInstance();
Set<String> fun = new HashSet<>();
Map<String,Integer> fieldMap = new HashMap<>();
roleIds.forEach((roleId)-> {
Role role = roleCache.get(roleId);
if(role==null || role.getSmpId()!=menuPlanId){
return;
}
RoleRightContent roleRightContent = new RoleRightContent(role.getPrivilege());
RoleRight roleRight = roleRightContent.getRoleRight(menuId);
if(roleRight!=null){
// 功能权限
Set<String> tempFun = roleRight.getRightFunc();
if(!CommUtil.isEmpty(tempFun)){
fun.addAll(tempFun);
}
// 字段权限
Map<String,Integer> tempFieldMap = roleRight.getRightField();
if(tempFieldMap!=null){
tempFieldMap.forEach((key,value)-> {
if(!fieldMap.containsKey(key)){
fieldMap.put(key,value);
}else {
if(fieldMap.get(key)<value){
fieldMap.put(key,value);
}
}
});
}
}
});
Map<String,Object> rtMap = new HashMap<>();
rtMap.put("func",fun);
rtMap.put("field",fieldMap);
return rtMap;
}
}

+ 1
- 1
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/user/role/RoleRightContent.java Datei anzeigen

@@ -115,7 +115,7 @@ public class RoleRightContent {

/**
* 获取菜单的数据权限组ID
* @param menuId 菜单id
* @param menuId 菜单明细id
* @param dataRightId 数据权限定义id
* @return 数据权限组ID
*/


Laden…
Abbrechen
Speichern