/** * 得到开始菜单. * * @param userId 用户ID */ public List<Menu> getMenusByUserId(Long userId) { List<Menu> menus = Lists.newArrayList(); List<Resource> rootResources = Lists.newArrayList(); User user = userManager.loadById(userId); User superUser = userManager.getSuperUser(); if (user != null && superUser != null && user.getId() == superUser.getId()) { // 超级用户 rootResources = getByParentId(null, StatusState.normal.getValue()); } else if (user != null) { rootResources = getResourcesByUserId(userId, null); // 去除非菜单资源 Iterator<Resource> iterator = rootResources.iterator(); while (iterator.hasNext()) { if (!ResourceState.menu.getValue().equals(iterator.next().getType())) { iterator.remove(); } } } for (Resource parentResource : rootResources) { Menu menu = resourceToMenu(parentResource, true); if (menu != null) { menus.add(menu); } } return menus; }
// 使用缓存 @Cacheable( value = {CacheConstants.RESOURCE_USER_RESOURCE_TREE_CACHE}, key = "#userId +'getResourceTreeByUserId'") public List<TreeNode> getResourceTreeByUserId(Long userId) throws DaoException, SystemException, ServiceException { // Assert.notNull(userId, "参数[userId]为空!"); List<TreeNode> nodes = Lists.newArrayList(); List<Resource> userResources = Lists.newArrayList(); User user = userManager.loadById(userId); User superUser = userManager.getSuperUser(); if (user != null && superUser != null && user.getId() == superUser.getId()) { // 超级用户 userResources = this.getByParentId(null, StatusState.normal.getValue()); } else if (user != null) { userResources = this.getResourcesByUserId(userId, null); } for (Resource resource : userResources) { TreeNode node = this.resourceToTreeNode(resource, null, true); if (node != null) { nodes.add(node); } } logger.debug( "缓存:{}", CacheConstants.RESOURCE_USER_RESOURCE_TREE_CACHE + " 参数:userId=" + userId); return nodes; }
public List<Menu> getAppMenusByUserId(Long userId) { List<Menu> menus = Lists.newArrayList(); List<Resource> resources = Lists.newArrayList(); User user = userManager.loadById(userId); User superUser = userManager.getSuperUser(); if (user != null && superUser != null && user.getId() == superUser.getId()) { // 超级用户 resources = super.getAll(); } else if (user != null) { resources = getResourcesByUserId(userId); } for (Resource resource : resources) { if (StringUtils.isNotBlank(resource.getUrl())) { if (ResourceState.menu.getValue().equals(resource.getType())) { Menu menu = new Menu(); menu.setId(resource.getId().toString()); menu.setText(resource.getName()); menu.setHref(resource.getUrl()); menus.add(menu); } } } return menus; }