/** 加载菜单 */
  @PostConstruct
  public void cacheMenus() throws Exception {
    try {
      List<MenuVo> menuList =
          DBUtils.jt()
              .query("select * from T_MENU order by displayOrder", createRowMapper(MenuVo.class));

      // 形成树状关系
      Map<String, MenuVo> menuMap = new HashMap<String, MenuVo>();
      for (MenuVo menu : menuList) {
        menuMap.put(menu.getMenuId(), menu);
      }
      for (MenuVo menu : menuList) {
        String pid = menu.getParentMenuId();
        if (!StringUtils.isEmpty(pid)) {
          MenuVo pMenu = menuMap.get(pid);
          pMenu.addSubMenu(menu);
        }
      }

      for (MenuVo menu : menuList) {
        if (!StringUtils.isEmpty(menu.getParentMenuId())) {
          menuMap.remove(menu.getMenuId());
        }
      }

      LocalCache.put(CACHE_KEY_MENU_MAP, Collections.unmodifiableMap(menuMap));
      logger.info("菜单缓存构建完毕。");
    } catch (Exception e) {
      logger.error("加载菜单出错", e);
      throw e;
    }
  }
 /** 检查当前用户是否有该菜单的权限 */
 private boolean hasPrivilege(MenuVo menu) throws Exception {
   boolean hasPrivilege = false;
   String privilegeId = menu.getPrivilegeId();
   if (StringUtils.isEmpty(privilegeId)) {
     hasPrivilege = true;
   } else {
     UserService userService = (UserService) DeftUtils.getBean(UserService.BEAN);
     hasPrivilege = userService.hasPrivilege(privilegeId);
   }
   return hasPrivilege;
 }
  /** 权限过滤 */
  private MenuVo filter(MenuVo menu) throws Exception {
    if (!hasPrivilege(menu)) {
      return null;
    }

    List<MenuVo> subMenus = menu.getSubMenus();
    if (subMenus != null) {
      for (int i = subMenus.size() - 1; i >= 0; i--) {
        if (filter(subMenus.get(i)) == null) {
          subMenus.remove(i);
        }
      }
    }

    return menu;
  }
Exemple #4
0
 public int compareTo(MenuVo o) {
   Integer i1 = new Integer(this.orderno);
   Integer i2 = new Integer(o.getOrderNo());
   return i1.compareTo(i2);
 }
 /** 获取菜单,并根据当前登录用户的权限设置进行过滤 */
 public MenuVo getMenu(String menuId) throws Exception {
   @SuppressWarnings("unchecked")
   Map<String, MenuVo> menuMap = (Map<String, MenuVo>) LocalCache.get(CACHE_KEY_MENU_MAP);
   MenuVo tempMenu = menuMap.get(menuId);
   return filter(tempMenu.clone());
 }