package com.vci.server.framework.right.roleRight; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.hibernate.HibernateException; import com.vci.common.objects.UserEntity; import com.vci.corba.common.VCIError; import com.vci.corba.common.data.VCIInvocationInfo; import com.vci.server.base.persistence.dao.BaseService; import com.vci.server.base.persistence.dao.HibernateCallback; import com.vci.server.base.persistence.dao.HibernateSessionFactory; import com.vci.server.base.persistence.dao.HibernateTemplate; /** * 角色功能模块权限配置 * * @author xf 2012-5-20 */ public class RoleRightService extends BaseService { public RoleRightService() { } public RoleRightService(UserEntity userEntity) { super(userEntity); } /** * 清除权限 * * @author weidy@2018-10-12 * @param roleId 角色的主键 * @param rightType 权限类型 * @return */ public boolean clearRoleRight(final String roleId, final int rightType) { return (Boolean) new HibernateTemplate().run(new HibernateCallback() { @Override public Object execute() throws HibernateException, SQLException, VCIError { RoleRightDaoImpl impl = new RoleRightDaoImpl(); if (userEntity == null) { userEntity = new UserEntity(); VCIInvocationInfo vcii = HibernateSessionFactory.getVciSessionInfo(); userEntity.setIp(vcii.clientIPInfo); userEntity.setModule("功能模块授权"); userEntity.setUserName(vcii.userName); } String hql = "delete RoleRight where roleId = ? and rightType=?"; impl.createQuery(hql, new Object[] { roleId.trim(), (short)rightType }); return true; } }); } /** * 保存角色功能模块权限 * * @return */ public boolean saveRoleRight(final RoleRight[] roleRights, final String roleId, final int rightType) { return (Boolean) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException, SQLException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); /** 先删除后保存 **/ // 注释根据rightType删除角色权限,避免管理员角色和普通用户角色相互影响 String hql = "delete RoleRight where rightType = ? and roleId = ? "; Object[] values = new Object[2]; values[0] = (short)rightType; values[1] = roleId; impl.createQuery(hql, values); // long s = System.currentTimeMillis(); batchSaveRoleRight(roleRights, roleId, rightType); // long t = System.currentTimeMillis(); // System.out.println(roleRights.length + " " + (t - s)); return true; } }); } public boolean removeRoleRight(final RoleRight[] roleRights, final String roleId) { return (Boolean) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException, SQLException { String hql = "delete RoleRight where funcId = ? and roleId = ?"; RoleRightDaoImpl impl = new RoleRightDaoImpl(); for (int i = 0; i < roleRights.length; i++) { // 根据角色和功能funcId删除授权 RoleRight roleRight = roleRights[i]; impl.createQuery(hql, new Object[] { roleRight.getFuncId(), roleRight.getRoleId() }); } return true; } }); } private void batchSaveRoleRight(RoleRight[] roleRights, String roleId, int rightType) throws HibernateException, SQLException { String sql = "insert into PLROLERIGHT (PLROLEOID, PLFUNCOID, PLRIGHTTYPE, PLRIGHTVALUE, PLCREATEUSER, " + "PLCREATETIME, PLUPDATEUSER, PLUPDATETIME, PLLICENSORS, PLOID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; PreparedStatement pst = null; try { pst = HibernateSessionFactory.getSessionConnection().prepareStatement(sql);// HibernateSessionFactory.getSession().connection().prepareStatement(sql); int batchSize = 200; // if (StringUtils.isBlank(roleId)) { // if (roleRights != null && roleRights.length > 0) { // roleId = roleRights[0].getRoleId(); // } // } // RoleRightDaoImpl impl = new RoleRightDaoImpl(); // String hql = "delete RoleRight where roleId = ? and rightType=?"; // // if (StringUtils.isNotBlank(roleId) && StringUtils.isNotBlank(hql)) {// 有传递角色才删除,因为有时候追加的情况 // impl.createQuery(hql, new Object[] { roleId, rightType }); // } Set roleOidHasFunctionOid = new HashSet(); for (int i = 0; i < roleRights.length; i++) { // 根据角色和功能funcId删除授权 RoleRight roleRight = roleRights[i]; // 增加权限 String roleOidFuncOid = roleRight.getFuncId(); if (roleOidHasFunctionOid.contains(roleOidFuncOid)) { continue; } else { roleOidHasFunctionOid.add(roleOidFuncOid); prepareCreateBOPsmt(pst, roleRight); pst.addBatch(); // 是否达到批量执行阀值 if ((i + 1) % batchSize == 0) { // 达到则执行 pst.executeBatch(); } } } pst.executeBatch(); } finally { if (pst != null) { pst.close(); } } } // private boolean judgeHasExitRight(RoleRight roleRight) throws VCIError { // boolean flag = false; // String oid = roleRight.getId(); // String funcId = roleRight.getFuncId(); // // String judgeSql = "select count(ploid) from PLROLERIGHT r " + // "where r.plroleoid = '" + oid + "' " + // "and r.plfuncoid = '" + funcId + "'"; // // String[][] result = new QTServiceImpl().queryBySqlWithoutKey(judgeSql); // if (result != null && result.length > 0) { // int sum = Integer.parseInt(result[0][0]); // if (sum == 0) { // // } else { // // } // } // } private void prepareCreateBOPsmt(PreparedStatement pst, RoleRight roleRight) throws SQLException { pst.setString(1, roleRight.getRoleId()); pst.setString(2, roleRight.getFuncId()); pst.setShort(3, roleRight.getRightType()); pst.setLong(4, roleRight.getRightValue()); pst.setString(5, roleRight.getCreateUser()); pst.setTimestamp(6, new Timestamp(roleRight.getCreateTime().getTime())); pst.setString(7, roleRight.getModifyUser()); pst.setTimestamp(8, new Timestamp(roleRight.getModifyTime().getTime())); pst.setString(9, roleRight.getLicensor()); pst.setString(10, roleRight.getId()); } /** * 增加授权专用:不改变原有授权,只是添加新增授权;如果已存在,不作处理,没有存的入库添加 * * @param roleRights * @param roleId * @param rightType * @return */ public boolean reAddRoleRight(final RoleRight[] roleRights, final String roleId, final int rightType) { return (Boolean) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException, SQLException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); /** 获取所选角色已有的模块授权 **/ String hql = "from RoleRight where rightType = ? and roleId = ? "; Object[] values = new Object[2]; values[0] = (short)rightType; values[1] = roleId; List myRoleRights = impl.findEntites(hql, values); List funcIds = new ArrayList(); if (myRoleRights != null) { for (RoleRight roleRight : myRoleRights) { funcIds.add(roleRight.getFuncId()); } } List roleRightList = new ArrayList(); for (RoleRight right : roleRights) { if (!funcIds.contains(right.getFuncId())) { roleRightList.add(right); } } batchSaveRoleRight(roleRightList.toArray(new RoleRight[roleRightList.size()]), null, rightType); return true; } }); } public boolean reAddRoleRightWithNoCheck(final RoleRight[] roleRights, final int rightType) { return (Boolean) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException, SQLException { batchSaveRoleRight(roleRights, null, rightType); return true; } }); } /** * 获取角色功能模块权限树 * * @return */ @SuppressWarnings("unchecked") public List getRoleRightList(final String roleId, final int rightType) { return (List) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); String hql = ""; if (rightType == 0) { hql = "from RoleRight where roleId = ? "; Object[] values = new Object[1]; values[0] = roleId; return impl.findEntites(hql, values); } else { hql = "from RoleRight where rightType = ? and roleId = ? "; Object[] values = new Object[2]; values[0] = (short)rightType; values[1] = roleId; return impl.findEntites(hql, values); } } }); } /** * *

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

* * @author sunbo * @time 2013-2-21 * @param rightType * @return */ public List getRoleRightListByType(final String[] rightType) { return (List) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); String hql = "select * from PLROLERIGHT r where r.PLRIGHTTYPE in ('" + rightType[0] + "' , '" + rightType[1] + "')"; // Object[] values = new Object[rightType.length]; // for(int i=0;i getRoleRightByModule(final String funcId, final String userName) { return (List) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); StringBuffer sql = new StringBuffer(); sql.append("select {t.*} from plroleright t where t.plroleoid in ("); sql.append(" select u.plroleuid from pluserrole u where u.pluseruid in ("); sql.append(" select pluid from pluser where plusername = '").append(userName).append("'"); sql.append(" )"); sql.append(") and t.plfuncoid = '").append(funcId).append("'"); return impl.findEntites(sql.toString(), new Object[0], "t", RoleRight.class); } }); } /** * 获取当前用户的所有权限 * * @param userName * @return */ @SuppressWarnings("unchecked") public List getRoleRightByUserName(final String userName) { return (List) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); StringBuffer sql = new StringBuffer(); sql.append(" select {r.*} from plroleright r where r.plroleoid in ("); sql.append(" select u.plroleuid from pluserrole u where u.pluseruid in ("); sql.append(" select pluid from pluser where plusername = '").append(userName).append("'"); sql.append(" )"); sql.append(" ) "); return impl.findEntites(sql.toString(), new Object[0], "r", RoleRight.class); } }); } @SuppressWarnings("unchecked") public List getFunctionRoleRightByUserName(final String userName) { return (List) new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { RoleRightDaoImpl impl = new RoleRightDaoImpl(); StringBuffer sql = new StringBuffer(); sql.append( " select {r.*} from plroleright r where r.plfuncoid in (select ploid from plfunction) and r.plroleoid in ("); sql.append(" select u.plroleuid from pluserrole u where u.pluseruid in ("); sql.append(" select pluid from pluser where plusername = '").append(userName).append("'"); sql.append(" )"); sql.append(" ) "); return impl.findEntites(sql.toString(), new Object[0], "r", RoleRight.class); } }); } }