package com.vci.web.util;
|
|
import com.vci.corba.common.PLException;
|
import com.vci.corba.framework.data.FunctionInfo;
|
import com.vci.corba.framework.data.RoleInfo;
|
import com.vci.corba.framework.data.RoleRightInfo;
|
import com.vci.starter.web.pagemodel.SessionInfo;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@Slf4j
|
@Component
|
public class RightControlUtil {
|
|
@Resource
|
private PlatformClientUtil platformClientUtil;
|
|
// add by xchao 2012.09.20 统一归整管理员、开发者用户判断
|
// 以便将来有可能修改管理员、开发者用户,对于判断依然有效
|
private static String userAdminEnum = "user.admin";
|
|
private String userNameAdmin = null;
|
|
private static String userDeveloperEnum = "user.developer";
|
|
private String userNameDeveloper = null;
|
|
private String userNameRoot = null;
|
|
private static String userRootEnum = "user.rooter";
|
|
public boolean isAdmin(String userName){
|
return userName.equals(getUserNameAdmin(userAdminEnum));
|
}
|
|
public boolean isDeveloper(String userName){
|
return userName.equals(getUserNameDeveloper(userDeveloperEnum));
|
}
|
|
public boolean isRoot(String userName){
|
return userName.equals(getUserNameRoot(userRootEnum));
|
}
|
|
/**
|
* 是否为管理员、开发者用户
|
* @param userName 登录用户名
|
* @return 判断结果
|
*/
|
public boolean isAdminOrDeveloperOrRoot(String userName){
|
return isAdmin(userName) || isDeveloper(userName)||isRoot(userName);
|
}
|
|
/**
|
* 获取配置的管理员
|
* @param key 配置key
|
* @return 管理员账号
|
*/
|
public String getUserNameAdmin(String key) {
|
if (userNameAdmin == null) {
|
synchronized (RightControlUtil.class) {
|
if (userNameAdmin == null) {
|
try {
|
userNameAdmin = platformClientUtil.getFrameworkService().getConfigValue(key);
|
}catch (PLException e){
|
log.error(e.code, e.messages);
|
}
|
}
|
}
|
}
|
return userNameAdmin;
|
}
|
|
/**
|
* 获取配置的开发人员
|
* @param key 配置key
|
* @return 开发人员账号
|
*/
|
public String getUserNameDeveloper(String key) {
|
if (userNameDeveloper == null) {
|
synchronized (RightControlUtil.class) {
|
if (userNameDeveloper == null) {
|
try {
|
userNameDeveloper = platformClientUtil.getFrameworkService().getConfigValue(key);
|
}catch (PLException e){
|
log.error(e.code, e.messages);
|
}
|
}
|
}
|
}
|
return userNameDeveloper;
|
}
|
|
/**
|
* 获取配置的管理员
|
* @param key 配置key
|
* @return 管理员账号
|
*/
|
public String getUserNameRoot(String key) {
|
if (userNameRoot == null) {
|
synchronized (RightControlUtil.class) {
|
if (userNameRoot == null) {
|
try {
|
userNameRoot = platformClientUtil.getFrameworkService().getConfigValue(key);
|
}catch (PLException e){
|
log.error(e.code, e.messages);
|
}
|
}
|
}
|
}
|
return userNameRoot;
|
}
|
|
/**
|
* 判断当前是否为三元用户
|
* @return
|
*/
|
public boolean isThreeAdminCurUser() {
|
SessionInfo sessionInfo = WebUtil.getCurrentUserSessionInfoNotException();
|
try {
|
RoleInfo[] roles = platformClientUtil.getFrameworkService().fetchRoleInfoByUserId(sessionInfo.getUserOid());
|
for (RoleInfo role : roles) {
|
if (role.name.equalsIgnoreCase("系统管理员")){
|
return true;
|
}
|
else if (role.name.equalsIgnoreCase("安全管理员")){
|
return true;
|
}
|
else if (role.name.equalsIgnoreCase("审计管理员")){
|
return true;
|
}
|
}
|
} catch (PLException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
/**
|
* 获取当前用户的权限
|
* @param userName
|
* @return
|
*/
|
public RoleRightInfo[] getRoleRightByUserName(String userName){
|
RoleRightInfo[] roleRightByUserName = new RoleRightInfo[0];
|
try {
|
roleRightByUserName = platformClientUtil.getFrameworkService().getRoleRightByUserName(userName);
|
} catch (PLException e) {
|
e.printStackTrace();
|
log.error(e.code,e.messages);
|
}
|
return roleRightByUserName;
|
}
|
|
/**
|
* 获取当前用户具有权限的所有模块
|
* @param parentId, 父模块id
|
* @param userName, 用户名
|
* @param userRoleRights, 当前用户具有的权限
|
* @return
|
*/
|
public Map<String, List<FunctionInfo>> getAllChildrenFunctionsByUserName(String parentId, String userName, RoleRightInfo[] userRoleRights) {
|
Map<String, List<RoleRightInfo>> mapRight = new LinkedHashMap<String, List<RoleRightInfo>>();
|
for (int i = 0; i < userRoleRights.length; i++) {
|
RoleRightInfo right = userRoleRights[i];
|
|
List<RoleRightInfo> lstRight = null;
|
if (mapRight.containsKey(right.funcId)) {
|
lstRight = mapRight.get(right.funcId);
|
} else {
|
lstRight = new ArrayList<>();
|
}
|
lstRight.add(right);
|
mapRight.put(right.funcId, lstRight);
|
}
|
|
FunctionInfo[] AllFunction = getFunctionsByParentId(parentId, userName);
|
Map<String, List<FunctionInfo>> map = new LinkedHashMap<String, List<FunctionInfo>>();
|
boolean isAllShow = isDeveloper(userName) || isAdmin(userName) || !isFunctionSwithOn();
|
boolean isHasRight = false;
|
FunctionInfo func;
|
for (int i = 0; i < AllFunction.length; i++) {
|
func = AllFunction[i];
|
//System.out.println("=== FUNC:ID=" + func.getId()+ " ParentID=" + func.getParentId() + "; Name=" + func.getName());
|
isHasRight = false;
|
if (isAllShow) {
|
isHasRight = true;
|
} else {
|
List<RoleRightInfo> lstRight = null;
|
if (mapRight.containsKey(func.id)) {
|
lstRight = mapRight.get(func.id);
|
//System.out.println(" === 有授权信息");
|
}
|
|
if (lstRight == null)
|
isHasRight = false;
|
else{
|
if (lstRight.size() > 0)
|
isHasRight = true;
|
}
|
}
|
if (!isHasRight) {
|
continue;
|
}
|
String cparentId = func.parentId;
|
|
//System.out.println("####==== ParentId=" + cparentId + "; Func=" + func.getName());
|
List<FunctionInfo> lstFunc = null;
|
if (map.containsKey(cparentId)) {
|
lstFunc = map.get(cparentId);
|
} else {
|
lstFunc = new ArrayList<FunctionInfo>();
|
}
|
lstFunc.add(func);
|
map.put(cparentId, lstFunc);
|
}
|
return map;
|
}
|
|
public FunctionInfo[] getFunctionsByParentId(String parentId, String userName) {
|
FunctionInfo[] funcObjs = null;
|
try {
|
if(isDeveloper(userName)){
|
funcObjs = platformClientUtil.getFrameworkService().getChildrenFunctionsByParentId(parentId, true);
|
} else {
|
funcObjs = platformClientUtil.getFrameworkService().getChildrenFunctionsByParentId(parentId, false);
|
}
|
} catch (PLException e) {
|
e.printStackTrace();
|
log.error(e.code, e.messages);
|
}
|
return funcObjs;
|
}
|
|
/**
|
* 根据父主键和用户以及角色信息来返回菜单数据
|
* @param parentId
|
* @param userName
|
* @param userRoleRights
|
* @return
|
*/
|
public List<FunctionInfo> getMenusByPIdAndPermission(String parentId, String userName,RoleRightInfo[] userRoleRights) {
|
Map<String, List<RoleRightInfo>> mapRight = new LinkedHashMap<String, List<RoleRightInfo>>();
|
for (int i = 0; i < userRoleRights.length; i++) {
|
RoleRightInfo right = userRoleRights[i];
|
|
List<RoleRightInfo> lstRight = null;
|
if (mapRight.containsKey(right.funcId)) {
|
lstRight = mapRight.get(right.funcId);
|
} else {
|
lstRight = new ArrayList<>();
|
}
|
lstRight.add(right);
|
mapRight.put(right.funcId, lstRight);
|
}
|
FunctionInfo[] functionInfos = this.getFunctionsByParentId(parentId, userName);
|
boolean isAllShow = isDeveloper(userName) || isAdmin(userName) || !isFunctionSwithOn();
|
|
List<FunctionInfo> functionInfoList = Arrays.stream(functionInfos).filter(menu -> {
|
// 只返回有效且是菜单的节点
|
if ((menu.isValid && menu.functionType == 0) && (mapRight.containsKey(menu.id) || isAllShow)) {
|
return true;
|
}
|
return false;
|
}).collect(Collectors.toList());
|
return functionInfoList;
|
}
|
|
/**
|
* 判断功能权限是否开启
|
* @return
|
*/
|
public boolean isFunctionSwithOn() {
|
String functionRightSwith = null;
|
try {
|
functionRightSwith = platformClientUtil.getFrameworkService().getConfigValue("function.right.swith");
|
} catch (PLException e) {
|
e.printStackTrace();
|
log.error(e.code, e.messages);
|
}
|
if(functionRightSwith != null && "on".equalsIgnoreCase(functionRightSwith)){
|
return true;
|
}
|
|
return false;
|
}
|
|
}
|