/** * 获取子级权限 * * @param permissions * @param parentId * @return */ public static List<Map<String, Object>> getChildrenPermissions( List<Permission> permissions, Integer parentId, Set<Integer> checkedPermissionIdSet, List<Integer> childrenIdList) { List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); for (Permission permission : permissions) { if ((parentId == null && permission.getParentId() == null) || (parentId != null && permission.getParentId() != null && permission.getParentId().intValue() == parentId.intValue())) { if (childrenIdList == null || !childrenIdList.contains(permission.getId())) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", permission.getId()); map.put("text", permission.getName()); map.put("key", permission.getKey()); map.put( "checked", checkedPermissionIdSet == null ? false : checkedPermissionIdSet.contains(permission.getId())); map.put("order", permission.getOrder()); map.put( "children", getChildrenPermissions( permissions, permission.getId(), checkedPermissionIdSet, childrenIdList)); mapList.add(map); } } } return mapList; }
/** * 获取菜单显示权限 * * @param menuId * @return */ public ResultVO getMenuShowPermissions(int menuId) { ResultVO resultVO = new ResultVO(true); List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); // 获取我拥有的权限 List<Permission> myPermissionList = new ArrayList<Permission>(); // 获取所有权限 List<Permission> permissionList = permissionDao.selectAll(); Subject subject = SecurityUtils.getSubject(); for (Permission permission : permissionList) { String key = permission.getKey(); boolean permitted = subject.isPermitted(key); if (permitted) { myPermissionList.add(permission); } } // 获取所有菜单 List<Menu> menus = menuDao.selectAll(); Menu menu = menuDao.get(menus, menuId); if (menu == null) { resultVO.setOk(false); resultVO.setMsg("菜单不存在"); return resultVO; } // 获取菜单拥有的权限id Set<Integer> permissionIdSet = menuPermissionDao.selectPermissionIdSet(menuId); List<Permission> rootPermissions = getRootPermissions(myPermissionList); for (Permission permission : rootPermissions) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", permission.getId()); map.put("text", permission.getName()); map.put("key", permission.getKey()); map.put("checked", permissionIdSet.contains(permission.getId())); map.put("order", permission.getOrder()); map.put( "children", getChildrenPermissions(myPermissionList, permission.getId(), permissionIdSet, null)); mapList.add(map); } resultVO.setData(mapList); return resultVO; }
/** * 获取显示权限 * * @return */ public ResultVO getShowPermissions(Integer rootId) { ResultVO resultVO = new ResultVO(true); List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); // 获取我拥有的权限 List<Permission> myPermissionList = new ArrayList<Permission>(); // 获取所有权限 List<Permission> permissionList = permissionDao.selectAll(); Subject subject = SecurityUtils.getSubject(); for (Permission permission : permissionList) { String key = permission.getKey(); boolean permitted = subject.isPermitted(key); if (permitted) { myPermissionList.add(permission); } } List<Integer> childrenIds = null; if (rootId != null) { childrenIds = getChildrenPermissionIds(rootId, permissionList); childrenIds.add(rootId); } List<Permission> rootPermissions = getRootPermissions(myPermissionList); for (Permission permission : rootPermissions) { if (childrenIds == null || !childrenIds.contains(permission.getId())) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", permission.getId()); map.put("text", permission.getName()); map.put("key", permission.getKey()); map.put("order", permission.getOrder()); map.put( "children", getChildrenPermissions(myPermissionList, permission.getId(), null, childrenIds)); mapList.add(map); } } resultVO.setData(mapList); return resultVO; }