1.RightsHelper定义
import java.math.BigInteger;
/**
* 权限计算帮助类
*/
public class RightsHelper {
/**
* 利用BigInteger对权限进行2的权的和计算
* @param rights int型权限编码数组
* @return 2的权的和
*/
public static BigInteger sumRights(int[] rights){
BigInteger num = new BigInteger("0");
for(int i=0; i<rights.length; i++){
num = num.setBit(rights[i]);
}
return num;
}
/**
* 利用BigInteger对权限进行2的权的和计算
* @param rights String型权限编码数组
* @return 2的权的和
*/
public static BigInteger sumRights(String[] rights){
BigInteger num = new BigInteger("0");
for(int i=0; i<rights.length; i++){
num = num.setBit(Integer.parseInt(rights[i]));
}
return num;
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(BigInteger sum,int targetRights){
return sum.testBit(targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(String sum,int targetRights){
if(Tools.isEmpty(sum))
return false;
return testRights(new BigInteger(sum),targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(String sum,String targetRights){
if(Tools.isEmpty(sum))
return false;
return testRights(new BigInteger(sum),targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(BigInteger sum,String targetRights){
return testRights(sum,Integer.parseInt(targetRights));
}
}
2.BigInteger的使用
String menuIds ="1,2,36,37,39,38,43,20,3,235,6,50";
BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
/**根据角色权限处理权限状态(递归处理)
* @param menuList:传入的总菜单
* @param roleRights:加密的权限字符串
* @return
*/
public List<Menu> readMenu(List<Menu> menuList,String roleRights){
for(int i=0;i<menuList.size();i++){
menuList.get(i).setHasMenu(RightsHelper.testRights(roleRights, menuList.get(i).getMENU_ID()));
this.readMenu(menuList.get(i).getSubMenu(), roleRights); //是:继续排查其子菜单
}
return menuList;
}
网友评论