package com.vci.server.framework.delegate; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.lang3.StringUtils; import com.vci.common.log.LogType; import com.vci.common.log.ServerWithLog4j; import com.vci.common.utility.ObjectUtility; import com.vci.corba.common.VCIError; import com.vci.corba.framework.data.RoleRightInfo; import com.vci.corba.common.data.UserEntityInfo; import com.vci.corba.framework.data.UserInfo; import com.vci.server.base.delegate.BaseDelegate; import com.vci.server.framework.funcmng.function.Function; import com.vci.server.framework.funcmng.function.FunctionService; import com.vci.server.framework.right.roleRight.RoleRight; import com.vci.server.framework.right.roleRight.RoleRightService; import com.vci.server.framework.systemConfig.log.LogRecordUtil; import com.vci.server.framework.systemConfig.stafforgmanage.role.Role; import com.vci.server.framework.systemConfig.stafforgmanage.role.RoleService; public class RoleRightDelegate extends BaseDelegate { private RoleRightService service = null; public RoleRightDelegate(UserEntityInfo userEntityInfo) { super(userEntityInfo); service = new RoleRightService(userEntity); } public RoleRightDelegate() { service = new RoleRightService(); } /** * 保存权限 * * @param roleRightInfos * @param userName * @return * @throws VCIError */ public boolean saveRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError { boolean res = true; if (roleRightInfos == null || roleRightInfos.length == 0) { throw new VCIError("没有任何的权限,不能执行授权", new String[0]); } RoleRight[] roleRights = null; try { RoleService roleService = new RoleService(); FunctionService functionService = new FunctionService(); // UserInfo userInfo = new // RightManagementDelegate().fetchUserInfoByName(userName); short rightType = 0; if (roleRightInfos.length > 0) rightType = roleRightInfos[0].rightType; roleRights = new RoleRight[roleRightInfos.length]; List functionOids = new ArrayList(); for (int i = 0; i < roleRightInfos.length; i++) { roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]); roleRights[i].setId(ObjectUtility.getNewObjectID36()); if (i < 20) functionOids.add(roleRights[i].getFuncId()); //roleRights[i].setRightType(rightType); } Role role = roleService.selectRole(roleId); if (roleRights.length > 0) { res = service.saveRoleRight(roleRights, roleId, rightType); try { // 看看顶层的模块 String logInfo = ""; if (functionOids != null && functionOids.size() > 0) { List functions = functionService.listRootFunctionByOids(functionOids); StringBuilder sb = new StringBuilder(); for (int i = 0; i < functions.size(); i++) { Function func = functions.get(i); sb.append(func.getAliasName()).append(","); if (i > 20) { break; } } sb.append("等共计" + roleRightInfos.length + "个权限"); logInfo = "操作的角色信息为:" + role.getLogInfo() + ",权限包括:" + sb.toString(); } else { logInfo = "操作的角色信息为:" + role.getLogInfo(); } if (res) LogRecordUtil.writeLog(userEntity, "授权", "成功", logInfo, LogType.GrantPrivileges, role.getId()); else LogRecordUtil.writeLog(userEntity, "授权", "失败", logInfo, LogType.GrantPrivileges, role.getId()); } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); } } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); LogRecordUtil.writeLog(userEntity, "授权", "功能模块授权失败!", e.getMessage(), LogType.GrantPrivileges, roleId); throw new VCIError("0", new String[] { e.getMessage() }); } return res; } private int getFunctionType(RoleRight[] roleRights) throws VCIError { List funcOidList = new ArrayList(); for (RoleRight rr : roleRights) { funcOidList.add(rr.getFuncId()); } FunctionService functionService = new FunctionService(); List functions = functionService.batchGetFunctionObjectByOid(funcOidList); if (functions.size() == 0) { // 去UI里面看看, return 1; } else if (roleRights.length != functions.size()) { return 2; } else { return 0; } } public boolean removeRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError { boolean res = true; RoleRight[] roleRights = null; try { RoleService roleService = new RoleService(); roleRights = new RoleRight[roleRightInfos.length]; for (int i = 0; i < roleRightInfos.length; i++) { roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]); roleRights[i].setId(ObjectUtility.getNewObjectID36()); // roleRights[i].setRightType(rightType); } Role role = roleService.selectRole(roleId); // 平台没有清除权限的接口,,weidy@2018-10-12添加 if (roleRights == null || roleRights.length == 0) { } else { res = service.removeRoleRight(roleRights, roleId); if (res) LogRecordUtil.writeLog(userEntity, "授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId()); else LogRecordUtil.writeLog(userEntity, "授权", "失败", role.getLogInfo(), LogType.GrantPrivileges, role.getId()); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); // LogRecordUtil.writeLog(userEntity, "授权", // "功能模块授权失败!",LogType.GrantPrivileges); throw new VCIError("0", new String[0]); } return res; } /** * 添加授权 * * @param roleRightInfos * @param roleId * @param userName * @return * @throws VCIError */ public boolean addRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError { boolean res = true; RoleRight[] roleRights = null; try { RoleService roleService = new RoleService(); // FunctionService functionService = new FunctionService(); // UserInfo userInfo = new // RightManagementDelegate().fetchUserInfoByName(userName); int rightType = 0; if (roleRightInfos.length > 0) rightType = roleRightInfos[0].rightType; /***** 当类型为2时,系统是普通用户,rightType = 2 ***/ // if(userInfo.userType == 2) { // rightType = userInfo.userType; // } else { // rightType = userInfo.userType + 1; // } roleRights = new RoleRight[roleRightInfos.length]; for (int i = 0; i < roleRightInfos.length; i++) { roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]); roleRights[i].setId(ObjectUtility.getNewObjectID36()); // roleRights[i].setRightType(rightType); } Role role = roleService.selectRole(roleId); res = service.reAddRoleRight(roleRights, roleId, rightType); LogRecordUtil.writeLog(userEntity, "增加授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId()); } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); // LogRecordUtil.writeLog(userEntity, "授权", // "功能模块授权失败!",LogType.GrantPrivileges); throw new VCIError("0", new String[0]); } return res; } public boolean addRoleRightWithNoCheck(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError { boolean res = true; RoleRight[] roleRights = null; try { RoleService roleService = new RoleService(); // UserInfo userInfo = new RightManagementDelegate().fetchUserInfoByName(userName); int rightType = 0; if (roleRightInfos.length > 0) rightType = roleRightInfos[0].rightType; /***** 当类型为2时,系统是普通用户,rightType = 2 ***/ // if(userInfo.userType == 2) { // rightType = userInfo.userType; // } else { // rightType = userInfo.userType + 1; // } roleRights = new RoleRight[roleRightInfos.length]; for (int i = 0; i < roleRightInfos.length; i++) { roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]); roleRights[i].setId(ObjectUtility.getNewObjectID36()); // roleRights[i].setRightType(rightType); } Role role = roleService.selectRole(roleId); res = service.reAddRoleRightWithNoCheck(roleRights, rightType); if (res) LogRecordUtil.writeLog(userEntity, "增加授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId()); else LogRecordUtil.writeLog(userEntity, "增加授权", "失败", role.getLogInfo(), LogType.GrantPrivileges, role.getId()); } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); // LogRecordUtil.writeLog(userEntity, "授权", // "功能模块授权失败!",LogType.GrantPrivileges); throw new VCIError("0", new String[0]); } return res; } /** * 获取权限 * * @param userName * @return * @throws VCIError */ public RoleRightInfo[] getRoleRightList(String roleId, String userName) throws VCIError { RoleRightInfo[] roleRightInfos = null; try { // UserInfo userInfo = new RightManagementDelegate().fetchUserInfoByName(userName); /***** 当类型为2时,系统是普通用户,rightType = 2 ***/ int rightType = 0; // if(userInfo.userType == 2) { // rightType = userInfo.userType; // } else { // rightType = userInfo.userType + 1; // } // int rightType = userInfo.userType + 1; List ls = service.getRoleRightList(roleId, rightType); roleRightInfos = new RoleRightInfo[ls.size()]; for (int i = 0; i < ls.size(); i++) { roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i)); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new VCIError("0", new String[0]); } return roleRightInfos; } /** * *

* Description:根据授权角色的类型获取权限 *

* * @author sunbo * @time 2013-2-21 * @param rightType * @return * @throws VCIError */ public RoleRightInfo[] getRoleRightListByType(final String[] rightType) throws VCIError { RoleRightInfo[] roleRightInfos = null; try { List ls = service.getRoleRightListByType(rightType); roleRightInfos = new RoleRightInfo[ls.size()]; for (int i = 0; i < ls.size(); i++) { roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i)); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new VCIError("0", new String[0]); } return roleRightInfos; } /** * 获取用户对当前模块的权限 * * @param funcId * @param userName * @return * @throws VCIError */ public RoleRightInfo[] getRoleRightByModule(String funcId, String userName) throws VCIError { RoleRightInfo[] roleRightInfos = null; try { List ls = service.getRoleRightByModule(funcId, userName); roleRightInfos = new RoleRightInfo[ls.size()]; for (int i = 0; i < ls.size(); i++) { roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i)); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new VCIError("120601", new String[0]); } return roleRightInfos; } public RoleRightInfo[] getRoleRightByUserName(String userName) throws VCIError { RoleRightInfo[] roleRightInfos = null; try { List ls = service.getRoleRightByUserName(userName); roleRightInfos = new RoleRightInfo[ls.size()]; for (int i = 0; i < ls.size(); i++) { roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i)); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new VCIError("0", new String[0]); } return roleRightInfos; } public RoleRightInfo[] getFunctionRoleRightByUserName(String userName) throws VCIError { RoleRightInfo[] roleRightInfos = null; try { List ls = service.getFunctionRoleRightByUserName(userName); roleRightInfos = new RoleRightInfo[ls.size()]; for (int i = 0; i < ls.size(); i++) { roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i)); } } catch (Exception e) { //e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new VCIError("0", new String[0]); } return roleRightInfos; } private RoleRightInfo changeRoleRightEntityToInfo(RoleRight roleRight) { RoleRightInfo info = new RoleRightInfo(); info.id = roleRight.getId() == null ? "" : roleRight.getId(); info.roleId = roleRight.getRoleId() == null ? "" : roleRight.getRoleId(); info.rightValue = roleRight.getRightValue(); info.rightType = roleRight.getRightType(); info.funcId = roleRight.getFuncId() == null ? "" : roleRight.getFuncId(); return info; } private RoleRight changeRoleRightInfoToEntity(RoleRightInfo roleRightInfo) { RoleRight entity = new RoleRight(); entity.setId(roleRightInfo.id); entity.setFuncId(roleRightInfo.funcId); entity.setRoleId(roleRightInfo.roleId); entity.setRightType(roleRightInfo.rightType); entity.setRightValue(roleRightInfo.rightValue); entity.setCreateUser(roleRightInfo.createUser); entity.setCreateTime(new Date(roleRightInfo.createTime)); entity.setModifyTime(new Date(roleRightInfo.modifyTime)); entity.setModifyUser(roleRightInfo.modifyUser); entity.setLicensor(roleRightInfo.licensor); return entity; } public boolean clearRoleRight(String roleId, String userName, int functionType) { return service.clearRoleRight(roleId, functionType); } }