예제 #1
0
  /**
   * 删除权限
   *
   * @param perId
   * @return
   */
  public ResultVO delPermission(int perId) {
    ResultVO resultVO = new ResultVO(true);
    // 获取所有权限
    List<Permission> permissionList = permissionDao.selectAll();
    Permission permission = permissionDao.get(permissionList, perId);
    if (permission == null) {
      resultVO.setOk(false);
      resultVO.setMsg("权限不存在");
      return resultVO;
    }
    // 查看是否有权限
    Subject subject = SecurityUtils.getSubject();
    if (!subject.isPermitted(permission.getKey())) {
      resultVO.setOk(false);
      resultVO.setMsg("您没有操作权限");
      return resultVO;
    }
    // 判断是否是根级权限
    // 获取我拥有的权限
    List<Permission> myPermissionList = new ArrayList<Permission>();
    for (Permission p : permissionList) {
      String key = p.getKey();
      boolean permitted = subject.isPermitted(key);
      if (permitted) {
        myPermissionList.add(p);
      }
    }
    List<Permission> rootPermissions = getRootPermissions(myPermissionList);
    for (Permission p : rootPermissions) {
      if (p.getId().intValue() == perId) {
        resultVO.setOk(false);
        resultVO.setMsg("根级权限不能删除");
        return resultVO;
      }
    }

    // 获取子级权限id集合
    List<Integer> childrenPermissionIds = getChildrenPermissionIds(perId, permissionList);

    // 删除权限
    int num = permissionDao.deletePermission(perId);

    for (Integer id : childrenPermissionIds) {
      num = permissionDao.deletePermission(id);
      if (num == 1) {
        rolePermissionDao.deleteByPerId(id);
        menuPermissionDao.deleteByPerId(id);
      }
    }
    rolePermissionDao.deleteByPerId(perId);
    menuPermissionDao.deleteByPerId(perId);
    resultVO.setMsg("删除权限成功");
    return resultVO;
  }
예제 #2
0
  /**
   * 获取角色显示权限
   *
   * @param roleId
   * @return
   */
  public ResultVO getRoleShowPermissions(int roleId) {
    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<Role> roles = roleDao.selectAll();
    Role role = roleDao.get(roles, roleId);
    if (role == null) {
      resultVO.setOk(false);
      resultVO.setMsg("角色不存在");
      return resultVO;
    }
    // 获取角色拥有的权限id
    Set<Integer> permissionIdSet = rolePermissionDao.getPermissionIdSetByRoleId(roleId);

    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;
  }