Esempio n. 1
0
  @RequestMapping(method = RequestMethod.GET)
  protected String processGet(
      @RequestAttribute Context context,
      ModelMap model,
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {

    Group group = null;
    group = checkGroup(context, request);

    if (group != null) {

      // unknown action, show edit page
      model.addAttribute("group", group);
      model.addAttribute("members", group.getMembers());
      model.addAttribute("membergroups", group.getMemberGroups());

      String utilsGrpName = Utils.addEntities(group.getName());
      model.addAttribute("utilsGrpName", utilsGrpName);
      return "pages/admin/group-edit";

    } else {

      return showMainPage(context, model, request, response);
    }
  } // end processGet
  /**
   * Is the given eperson in the given group, or any of the groups that are also members of that
   * group. This method recurses until it has exhausted the tree of groups or finds the given
   * eperson
   *
   * @param group
   * @param eperson
   * @return true if in group, false if not
   */
  public boolean isInGroup(Group group, EPerson eperson) {
    EPerson[] eps = group.getMembers();
    Group[] groups = group.getMemberGroups();

    // is the user in the current group
    for (int i = 0; i < eps.length; i++) {
      if (eperson.getID() == eps[i].getID()) {
        return true;
      }
    }

    // is the eperson in the sub-groups (recurse)
    if (groups != null && groups.length > 0) {
      for (int j = 0; j < groups.length; j++) {
        if (isInGroup(groups[j], eperson)) {
          return true;
        }
      }
    }

    // ok, we didn't find you
    return false;
  }
Esempio n. 3
0
  @RequestMapping(method = RequestMethod.POST)
  protected String processPost(
      @RequestAttribute Context context,
      ModelMap model,
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {

    Group group = null;
    group = checkGroup(context, request);

    if (group != null) {

      // is this user authorized to edit this group?
      AuthorizeManager.authorizeAction(context, group, Constants.ADD);

      boolean submit_edit = (request.getParameter("submit_edit") != null);
      boolean submit_group_update = (request.getParameter("submit_group_update") != null);
      boolean submit_group_delete = (request.getParameter("submit_group_delete") != null);
      boolean submit_confirm_delete = (request.getParameter("submit_confirm_delete") != null);
      boolean submit_cancel_delete = (request.getParameter("submit_cancel_delete") != null);

      // just chosen a group to edit - get group and pass it to
      // group-edit.jsp
      if (submit_edit && !submit_group_update && !submit_group_delete) {
        model.addAttribute("group", group);
        model.addAttribute("members", group.getMembers());
        model.addAttribute("membergroups", group.getMemberGroups());
        String utilsGrpName = Utils.addEntities(group.getName());
        model.addAttribute("utilsGrpName", utilsGrpName);

        return "pages/admin/group-edit";
      } // update the members of the group
      else if (submit_group_update) {
        // first off, did we change the group name?
        String newName = request.getParameter("group_name");

        if (!newName.equals(group.getName())) {
          group.setName(newName);
          group.update();
        }

        int[] eperson_ids = Util.getIntParameters(request, "eperson_id");
        int[] group_ids = Util.getIntParameters(request, "group_ids");

        // now get members, and add new ones and remove missing ones
        EPerson[] members = group.getMembers();
        Group[] membergroups = group.getMemberGroups();

        if (eperson_ids != null) {
          // some epeople were listed, now make group's epeople match
          // given epeople
          Set memberSet = new HashSet();
          Set epersonIDSet = new HashSet();

          // add all members to a set
          for (int x = 0; x < members.length; x++) {
            Integer epersonID = Integer.valueOf(members[x].getID());
            memberSet.add(epersonID);
          }

          // now all eperson_ids are put in a set
          for (int x = 0; x < eperson_ids.length; x++) {
            epersonIDSet.add(Integer.valueOf(eperson_ids[x]));
          }

          // process eperson_ids, adding those to group not already
          // members
          Iterator i = epersonIDSet.iterator();

          while (i.hasNext()) {
            Integer currentID = (Integer) i.next();

            if (!memberSet.contains(currentID)) {
              group.addMember(EPerson.find(context, currentID.intValue()));
            }
          }

          // process members, removing any that aren't in eperson_ids
          for (int x = 0; x < members.length; x++) {
            EPerson e = members[x];

            if (!epersonIDSet.contains(Integer.valueOf(e.getID()))) {
              group.removeMember(e);
            }
          }
        } else {
          // no members found (ids == null), remove them all!

          for (int y = 0; y < members.length; y++) {
            group.removeMember(members[y]);
          }
        }

        if (group_ids != null) {
          // some groups were listed, now make group's member groups
          // match given group IDs
          Set memberSet = new HashSet();
          Set groupIDSet = new HashSet();

          // add all members to a set
          for (int x = 0; x < membergroups.length; x++) {
            Integer myID = Integer.valueOf(membergroups[x].getID());
            memberSet.add(myID);
          }

          // now all eperson_ids are put in a set
          for (int x = 0; x < group_ids.length; x++) {
            groupIDSet.add(Integer.valueOf(group_ids[x]));
          }

          // process group_ids, adding those to group not already
          // members
          Iterator i = groupIDSet.iterator();

          while (i.hasNext()) {
            Integer currentID = (Integer) i.next();

            if (!memberSet.contains(currentID)) {
              group.addMember(Group.find(context, currentID.intValue()));
            }
          }

          // process members, removing any that aren't in eperson_ids
          for (int x = 0; x < membergroups.length; x++) {
            Group g = membergroups[x];

            if (!groupIDSet.contains(Integer.valueOf(g.getID()))) {
              group.removeMember(g);
            }
          }

        } else {
          // no members found (ids == null), remove them all!
          for (int y = 0; y < membergroups.length; y++) {
            group.removeMember(membergroups[y]);
          }
        }

        group.update();

        model.addAttribute("group", group);
        model.addAttribute("members", group.getMembers());
        model.addAttribute("membergroups", group.getMemberGroups());
        String utilsGrpName = Utils.addEntities(group.getName());
        model.addAttribute("utilsGrpName", utilsGrpName);

        context.commit();
        return "pages/admin/group-edit";
      } else if (submit_group_delete) {
        // direct to a confirmation step
        model.addAttribute("group", group);
        return "pages/admin/group-confirm-delete";
      } else if (submit_confirm_delete) {
        // phony authorize, only admins can do this
        AuthorizeManager.authorizeAction(context, group, Constants.WRITE);

        // delete group, return to group-list.jsp
        group.delete();

        return showMainPage(context, model, request, response);
      } else if (submit_cancel_delete) {
        // show group list
        return showMainPage(context, model, request, response);
      } else {
        // unknown action, show edit page
        model.addAttribute("group", group);
        model.addAttribute("members", group.getMembers());
        model.addAttribute("membergroups", group.getMemberGroups());
        String utilsGrpName = Utils.addEntities(group.getName());
        model.addAttribute("utilsGrpName", utilsGrpName);

        return "pages/admin/group-edit";
      }
    } else {

      // want to add a group - create a blank one, and pass to
      // group_edit.jsp
      String button = UIUtil.getSubmitButton(request, "submit");

      if (button.equals("submit_add")) {
        group = Group.create(context);

        group.setName("new group" + group.getID());
        group.update();

        model.addAttribute("group", group);
        model.addAttribute("members", group.getMembers());
        model.addAttribute("membergroups", group.getMemberGroups());
        String utilsGrpName = Utils.addEntities(group.getName());
        model.addAttribute("utilsGrpName", utilsGrpName);

        context.commit();
        return "pages/admin/group-edit";

      } else {
        // show the main page (select groups)
        return showMainPage(context, model, request, response);
      }
    } // end
  } // end processGet
  public void addBody(Body body) throws WingException, SQLException {
    // Get all our parameters
    String baseURL = contextPath + "/admin/groups?administrative-continue=" + knot.getId();
    String query = parameters.getParameter("query", "");
    int page = parameters.getParameterAsInteger("page", 0);
    int highlightID = parameters.getParameterAsInteger("highlightID", -1);
    // FIXME: Bad!
    //        int resultCount = Group.searchResultCount(context, query);
    int resultCount = Group.search(context, query).length;
    Group[] groups = Group.search(context, query, page * PAGE_SIZE, PAGE_SIZE);

    // DIVISION: groups-main
    Division main =
        body.addInteractiveDivision(
            "groups-main",
            contextPath + "/admin/groups",
            Division.METHOD_POST,
            "primary administrative groups");
    main.setHead(T_main_head);

    // DIVISION: group-actions
    Division actions = main.addDivision("group-actions");
    actions.setHead(T_actions_head);

    // Browse Epeople
    List actionsList = actions.addList("actions");
    actionsList.addLabel(T_actions_create);
    actionsList.addItemXref(baseURL + "&submit_add", T_actions_create_link);
    actionsList.addLabel(T_actions_browse);
    actionsList.addItemXref(baseURL + "&query&submit_search", T_actions_browse_link);

    actionsList.addLabel(T_actions_search);
    org.dspace.app.xmlui.wing.element.Item actionItem = actionsList.addItem();
    Text queryField = actionItem.addText("query");
    if (query != null) queryField.setValue(query);
    queryField.setHelp(T_search_help);
    actionItem.addButton("submit_search").setValue(T_go);

    // DIVISION: group-search
    Division search = main.addDivision("group-search");
    search.setHead(T_search_head);

    if (resultCount > PAGE_SIZE) {
      // If there are enough results then paginate the results
      int firstIndex = page * PAGE_SIZE + 1;
      int lastIndex = page * PAGE_SIZE + groups.length;

      String nextURL = null, prevURL = null;
      if (page < (resultCount / PAGE_SIZE)) nextURL = baseURL + "&page=" + (page + 1);
      if (page > 0) prevURL = baseURL + "&page=" + (page - 1);

      search.setSimplePagination(resultCount, firstIndex, lastIndex, prevURL, nextURL);
    }

    Table table = search.addTable("groups-search-table", groups.length + 1, 1);
    Row header = table.addRow(Row.ROLE_HEADER);
    header.addCell().addContent(T_search_column1);
    header.addCell().addContent(T_search_column2);
    header.addCell().addContent(T_search_column3);
    header.addCell().addContent(T_search_column4);
    header.addCell().addContent(T_search_column5);

    for (Group group : groups) {
      Row row;
      if (group.getID() == highlightID) row = table.addRow(null, null, "highlight");
      else row = table.addRow();

      if (group.getID() > 1) {
        CheckBox select = row.addCell().addCheckBox("select_group");
        select.setLabel(new Integer(group.getID()).toString());
        select.addOption(new Integer(group.getID()).toString());
      } else {
        // Don't allow the user to remove the administrative (id:1) or
        // anonymous group (id:0)
        row.addCell();
      }

      row.addCell().addContent(group.getID());
      row.addCell().addXref(baseURL + "&submit_edit&groupID=" + group.getID(), group.getName());

      int memberCount = group.getMembers().length + group.getMemberGroups().length;
      row.addCell().addContent(memberCount == 0 ? "-" : String.valueOf(memberCount));

      Cell cell = row.addCell();
      if (FlowGroupUtils.getCollectionId(group.getName()) > -1) {
        Collection collection =
            Collection.find(context, FlowGroupUtils.getCollectionId(group.getName()));
        if (collection != null) {
          String collectionName = collection.getMetadata("name");

          if (collectionName == null) collectionName = "";
          else if (collectionName.length() > MAX_COLLECTION_NAME)
            collectionName = collectionName.substring(0, MAX_COLLECTION_NAME - 3) + "...";

          cell.addContent(collectionName + " ");

          Highlight highlight = cell.addHighlight("fade");

          highlight.addContent("[");
          highlight.addXref(
              contextPath + "/handle/" + collection.getExternalIdentifier().getCanonicalForm(),
              T_collection_link);
          highlight.addContent("]");
        }
      }
    }

    if (groups.length <= 0) {
      Cell cell = table.addRow().addCell(1, 5);
      cell.addHighlight("italic").addContent(T_no_results);
    } else {
      search.addPara().addButton("submit_delete").setValue(T_submit_delete);
    }

    search.addHidden("administrative-continue").setValue(knot.getId());
  }