Example #1
0
  /**
   * Build the top level menu
   *
   * @param context
   */
  private void buildMenu(Context context) {
    // build the menu
    Menu bar = new MenuImpl();
    bar.add(new MenuEntry(rb.getString("archive.button.single"), "doView_single"));
    bar.add(new MenuEntry(rb.getString("archive.button.batch"), "doView_batch"));
    bar.add(new MenuEntry(rb.getString("archive.button.download"), "doView_download"));

    context.put(Menu.CONTEXT_MENU, bar);
    context.put(Menu.CONTEXT_ACTION, "ArchiveAction");
  }
Example #2
0
  /** build the context for single mode import/export */
  public String buildSingleModeContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    return "";
  }
Example #3
0
  /** build the context for batch archive confirm */
  public String buildBatchModeArchiveConfirmContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);

    // go to template
    return "-batch-archive-confirm";
  }
Example #4
0
  /** build the context */
  public String buildMainPanelContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    String template = null;

    // if not logged in as the super user, we won't do anything
    if (!securityService.isSuperUser()) {
      context.put("tlang", rb);
      return (String) getContext(rundata).get("template") + "_noaccess";
    }

    // check mode and dispatch
    String mode = (String) state.getAttribute(STATE_MODE);

    if (StringUtils.equals(mode, SINGLE_MODE)) {
      template = buildSingleModeContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, BATCH_MODE)) {
      template = buildBatchModeContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, BATCH_ARCHIVE_CONFIRM_MODE)) {
      template = buildBatchModeArchiveConfirmContext(portlet, context, rundata, state);
    } else if (StringUtils.equals(mode, DOWNLOAD_MODE)) {
      template = buildDownloadContext(portlet, context, rundata, state);
    }

    return (String) getContext(rundata).get("template") + template;
  } // buildMainPanelContext
Example #5
0
  /** build the context for batch import/export */
  public String buildBatchModeContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    // check if we are already running. Template will render just the message if so
    String statusMessage = getCurrentBatchArchiveStatusMessage();
    if (StringUtils.isNotBlank(statusMessage)) {
      context.put("isRunning", true);
      context.put("statusMessage", statusMessage);
    }

    // get list of terms
    List<AcademicSession> terms = courseManagementService.getAcademicSessions();
    context.put("terms", terms);

    return "-batch";
  }
Example #6
0
  /** Setup the velocity context and choose the template for the response. */
  public String buildMainPanelContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    // set the resource bundle with our strings
    context.put("tlang", rb);

    context.put("doSave", BUTTON + "doSave");
    context.put("doCancel", BUTTON + "doCancel");
    context.put("doRemove", BUTTON + "doRemove");
    try {
      Site site;
      String siteId = getSiteId(state);

      site = SiteService.getSite(siteId);
      String parentId = site.getProperties().getProperty("sakai:parent-id");
      context.put("currentSite", site);
      if (parentId != null) {
        // Make sure parent site exists before we show it.
        // If the parent site does not exist, clear the property
        try {
          Site parentSite = SiteService.getSite(parentId);
          context.put("parentId", parentId);
          context.put("parentTitle", parentSite.getTitle());
          return "sakai_link";
        } catch (Exception e) {
          addAlert(state, rb.getFormattedMessage("alert.parent.removed", new Object[] {parentId}));
          ResourcePropertiesEdit rpe = site.getPropertiesEdit();
          rpe.removeProperty("sakai:parent-id");
          SiteService.save(site);
        }
      }

      // Give the user a list of sites to select as parent
      List<Site> sites =
          SiteService.getSites(
              org.sakaiproject.site.api.SiteService.SelectionType.UPDATE,
              null,
              null,
              null,
              SortType.TITLE_ASC,
              null);

      List<Site> goodSites = new ArrayList<Site>();
      // Do not include any sites which point to us as a parent
      // Do not include ourself in the candidate list
      for (Iterator i = sites.iterator(); i.hasNext(); ) {
        Site thisSite = (Site) i.next();
        String pid = thisSite.getProperties().getProperty("sakai:parent-id");
        if (siteId.equals(pid)) continue;
        if (siteId.equals(thisSite.getId())) continue;
        goodSites.add(thisSite);
      }
      if (goodSites.size() > 0) context.put("sites", goodSites);
    } catch (Exception e) {
      addAlert(state, rb.getString("error.cannot.access"));
    }

    return "sakai_link";
  }
Example #7
0
  /** build the context for batch archive confirm */
  public String buildDownloadContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    // get list of existing archives
    Collection<File> files = Collections.<File>emptySet();
    File archiveBaseDir =
        new File(serverConfigurationService.getString("archive.storage.path", "sakai/archive"));

    if (archiveBaseDir.exists() && archiveBaseDir.isDirectory()) {
      files = FileUtils.listFiles(archiveBaseDir, new SuffixFileFilter(".zip"), null);
    }

    List<SparseFile> zips = new ArrayList<SparseFile>();

    SimpleDateFormat dateFormatIn = new SimpleDateFormat("yyyyMMddHHmmss");
    SimpleDateFormat dateFormatOut = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Calendar calendar = Calendar.getInstance();

    // porcess the list. also get the hash for the file if it exists
    for (File f : files) {

      String absolutePath = f.getAbsolutePath();

      SparseFile sf = new SparseFile();
      sf.setFilename(f.getName());
      sf.setAbsolutePath(absolutePath);
      sf.setSize(FileUtils.byteCountToDisplaySize(f.length()));

      // get the datetime string, its the last part of the file name, convert back to a date that we
      // can display
      String dateTimeStr =
          StringUtils.substringAfterLast(StringUtils.removeEnd(f.getName(), ".zip"), "-");

      try {
        Date date = dateFormatIn.parse(dateTimeStr);
        sf.setDateCreated(dateFormatOut.format(date));
      } catch (ParseException pe) {
        // ignore, just don't set the date
      }

      // get siteId, first part of name
      String siteId = StringUtils.substringBeforeLast(f.getName(), "-");
      sf.setSiteId(siteId);

      // try to get site title if the site still exists
      try {
        Site site = siteService.getSite(siteId);
        sf.setSiteTitle(site.getTitle());
      } catch (IdUnusedException e) {
        // ignore, no site available
      }

      // get the hash. need to read it from the file. Same filename but diff extension
      String hashFilePath = StringUtils.removeEnd(absolutePath, ".zip");
      hashFilePath = hashFilePath + ".sha1";

      File hashFile = new File(hashFilePath);
      try {
        String hash = FileUtils.readFileToString(hashFile);
        sf.setHash(hash);
      } catch (IOException e) {
        // ignore, dont use the hash
      }

      zips.add(sf);
    }

    context.put("archives", zips);

    return "-download";
  }
  /**
   * build the context.
   *
   * @return The name of the template to use.
   */
  public static String buildHelperContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    // in state is the realm id
    context.put("thelp", rb);
    String realmId = (String) state.getAttribute(STATE_REALM_ID);

    // in state is the realm to use for roles - if not, use realmId
    String realmRolesId = (String) state.getAttribute(STATE_REALM_ROLES_ID);
    context.put("viewRealmId", realmRolesId);

    // get the realm locked for editing
    AuthzGroup edit = (AuthzGroup) state.getAttribute(STATE_REALM_EDIT);
    if (edit == null) {
      if (AuthzGroupService.allowUpdate(realmId)) {
        try {
          edit = AuthzGroupService.getAuthzGroup(realmId);
          state.setAttribute(STATE_REALM_EDIT, edit);
        } catch (GroupNotDefinedException e) {
          try {
            // we can create the realm
            edit = AuthzGroupService.addAuthzGroup(realmId);
            state.setAttribute(STATE_REALM_EDIT, edit);
          } catch (GroupIdInvalidException ee) {
            M_log.warn("PermissionsAction.buildHelperContext: addRealm: " + ee);
            cleanupState(state);
            return null;
          } catch (GroupAlreadyDefinedException ee) {
            M_log.warn("PermissionsAction.buildHelperContext: addRealm: " + ee);
            cleanupState(state);
            return null;
          } catch (AuthzPermissionException ee) {
            M_log.warn("PermissionsAction.buildHelperContext: addRealm: " + ee);
            cleanupState(state);
            return null;
          }
        }
      }

      // no permission
      else {
        M_log.warn("PermissionsAction.buildHelperContext: no permission: " + realmId);
        cleanupState(state);
        return null;
      }
    }

    AuthzGroup viewEdit = null;
    // check wither the current realm id is of site group type
    if (realmId.indexOf(SiteService.REFERENCE_ROOT) != -1) {
      String siteId = realmId.replaceAll(SiteService.REFERENCE_ROOT + "/", "");
      context.put("siteRef", realmId);

      if (state.getAttribute(STATE_GROUP_AWARE) != null
          && ((Boolean) state.getAttribute(STATE_GROUP_AWARE)).booleanValue()) {
        // only show groups for group-aware tools
        try {
          Site site = SiteService.getSite(siteId);
          Collection groups = site.getGroups();
          if (groups != null && !groups.isEmpty()) {
            Iterator iGroups = groups.iterator();
            for (; iGroups.hasNext(); ) {
              Group group = (Group) iGroups.next();
              // need to either have realm update permission on the group level or better at the
              // site level
              if (!AuthzGroupService.allowUpdate(group.getReference())) {
                iGroups.remove();
              }
            }
            context.put("groups", groups);
          }

        } catch (Exception siteException) {
          M_log.warn(
              "PermissionsAction.buildHelperContext: getsite of realm id =  "
                  + realmId
                  + siteException);
        }
      }

      // get the realm locked for editing
      viewEdit = (AuthzGroup) state.getAttribute(STATE_VIEW_REALM_EDIT);
      if (viewEdit == null) {
        if (AuthzGroupService.allowUpdate(realmRolesId)
            || AuthzGroupService.allowUpdate(SiteService.siteReference(siteId))) {
          try {
            viewEdit = AuthzGroupService.getAuthzGroup(realmRolesId);
            state.setAttribute(STATE_VIEW_REALM_EDIT, viewEdit);
          } catch (GroupNotDefinedException e) {
            M_log.warn(
                "PermissionsAction.buildHelperContext: getRealm with id= "
                    + realmRolesId
                    + " : "
                    + e);
            cleanupState(state);
            return null;
          }
        }

        // no permission
        else {
          M_log.warn("PermissionsAction.buildHelperContext: no permission: " + realmId);
          cleanupState(state);
          return null;
        }
      }
    }

    // in state is the prefix for abilities to present
    String prefix = (String) state.getAttribute(STATE_PREFIX);

    // in state is the list of abilities we will present
    List functions = (List) state.getAttribute(STATE_ABILITIES);
    if (functions == null) {
      // get all functions prefixed with our prefix
      functions = FunctionManager.getRegisteredFunctions(prefix);
    }

    if (functions != null && !functions.isEmpty()) {
      List<String> nFunctions = new Vector<String>();
      if (!realmRolesId.equals(realmId)) {
        // editing groups within site, need to filter out those permissions only applicable to site
        // level
        for (Iterator iFunctions = functions.iterator(); iFunctions.hasNext(); ) {
          String function = (String) iFunctions.next();
          if (function.indexOf("all.groups") == -1) {
            nFunctions.add(function);
          }
        }
      } else {
        nFunctions.addAll(functions);
      }
      state.setAttribute(STATE_ABILITIES, nFunctions);
      context.put("abilities", nFunctions);

      // get function description from passed in HashMap
      // output permission descriptions
      Map<String, String> functionDescriptions =
          (Map<String, String>) state.getAttribute(STATE_PERMISSION_DESCRIPTIONS);
      if (functionDescriptions != null) {
        Set keySet = functionDescriptions.keySet();
        for (Object function : functions) {
          String desc = (String) function;
          String descKey = PermissionsHelper.PREFIX_PERMISSION_DESCRIPTION + function;
          if (keySet.contains(descKey)) {
            // use function description
            desc = (String) functionDescriptions.get(descKey);
          }

          functionDescriptions.put((String) function, desc);
        }
        context.put("functionDescriptions", functionDescriptions);
      }
    }

    // in state is the description of the edit
    String description = (String) state.getAttribute(STATE_DESCRIPTION);

    // the list of roles
    List roles = (List) state.getAttribute(STATE_ROLES);
    if (roles == null) {
      // get the roles from the edit, unless another is specified
      AuthzGroup roleRealm = viewEdit != null ? viewEdit : edit;
      if (realmRolesId != null) {
        try {
          roleRealm = AuthzGroupService.getAuthzGroup(realmRolesId);
        } catch (Exception e) {
          M_log.warn(
              "PermissionsHelperAction.buildHelperContext: getRolesRealm: "
                  + realmRolesId
                  + " : "
                  + e);
        }
      }
      roles = new Vector();
      roles.addAll(roleRealm.getRoles());
      Collections.sort(roles);
      state.setAttribute(STATE_ROLES, roles);
    }

    // the abilities not including this realm for each role
    Map rolesAbilities = (Map) state.getAttribute(STATE_ROLE_ABILITIES);
    if (rolesAbilities == null) {
      rolesAbilities = new Hashtable();
      state.setAttribute(STATE_ROLE_ABILITIES, rolesAbilities);

      // get this resource's role Realms,those that refine the role definitions, but not it's own
      Reference ref =
          EntityManager.newReference(viewEdit != null ? viewEdit.getId() : edit.getId());
      Collection realms = ref.getAuthzGroups();
      realms.remove(ref.getReference());

      for (Iterator iRoles = roles.iterator(); iRoles.hasNext(); ) {
        Role role = (Role) iRoles.next();
        Set locks = AuthzGroupService.getAllowedFunctions(role.getId(), realms);
        rolesAbilities.put(role.getId(), locks);
      }
    }

    context.put("realm", viewEdit != null ? viewEdit : edit);
    context.put("prefix", prefix);
    context.put("description", description);
    if (roles.size() > 0) {
      context.put("roles", roles);
    }
    context.put("rolesAbilities", rolesAbilities);

    // make sure observers are disabled
    VelocityPortletPaneledAction.disableObservers(state);

    return TEMPLATE_MAIN;
  }
Example #9
0
  /** Setup the velocity context and choose the template for options. */
  public String buildOptionsPanelContext(
      VelocityPortlet portlet, Context context, RunData data, SessionState state) {
    // provide the source, and let the user edit, if not special
    String special = (String) state.getAttribute(SPECIAL);
    String source = "";
    String siteId = "";
    if (special == null) {
      source = (String) state.getAttribute(SOURCE);
      if (source == null) source = "";
      context.put(SOURCE, source);
      context.put("heading", rb.getString("gen.custom"));
    }

    // set the heading based on special
    else {
      if (SPECIAL_SITE.equals(special)) {
        context.put("heading", rb.getString("gen.custom.site"));
      } else if (SPECIAL_WORKSPACE.equals(special)) {
        context.put("heading", rb.getString("gen.custom.workspace"));
      } else if (SPECIAL_WORKSITE.equals(special)) {
        context.put("heading", rb.getString("gen.custom.worksite"));

        // for worksite, also include the Site's infourl and description
        try {
          Site s = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
          siteId = s.getId();

          String infoUrl = StringUtils.trimToNull(s.getInfoUrl());
          if (infoUrl != null) {
            context.put("info_url", infoUrl);
          }

          String description = StringUtils.trimToNull(s.getDescription());
          if (description != null) {
            description = FormattedText.escapeHtmlFormattedTextarea(description);
            context.put("description", description);
          }
        } catch (Throwable e) {
        }
      } else if (SPECIAL_ANNOTATEDURL.equals(special)) {

        context.put("heading", rb.getString("gen.custom.annotatedurl"));

        // for Annotated URL Tool page, also include the description
        try {
          String desp = state.getAttribute(ANNOTATED_TEXT).toString();
          context.put("description", desp);

        } catch (Throwable e) {
        }
      } else {
        context.put("heading", rb.getString("gen.custom"));
      }
    }

    boolean selected = false;
    String height = state.getAttribute(HEIGHT).toString();
    for (int i = 0; i < ourPixels.length; i++) {
      if (height.equals(ourPixels[i])) {
        selected = true;
        continue;
      }
    }
    if (!selected) {
      String[] strings = height.trim().split("px");
      context.put("custom_height", strings[0]);
      height = rb.getString("gen.heisomelse");
    }
    context.put(HEIGHT, height);

    context.put(TITLE, state.getAttribute(TITLE));
    context.put("tlang", rb);

    context.put("doUpdate", BUTTON + "doConfigure_update");
    context.put("doCancel", BUTTON + "doCancel");

    context.put("form_tool_title", FORM_TOOL_TITLE);
    context.put("form_page_title", FORM_PAGE_TITLE);

    // if we are part of a site, and the only tool on the page, offer the popup to edit
    Placement placement = ToolManager.getCurrentPlacement();
    ToolConfiguration toolConfig = SiteService.findTool(placement.getId());
    if ((state.getAttribute(SPECIAL) == null) && (toolConfig != null)) {
      try {
        Site site = SiteService.getSite(toolConfig.getSiteId());
        siteId = site.getId();
        SitePage page = site.getPage(toolConfig.getPageId());

        // if this is the only tool on that page, update the page's title also
        if ((page.getTools() != null) && (page.getTools().size() == 1)) {
          context.put("showPopup", Boolean.TRUE);
          context.put("popup", Boolean.valueOf(page.isPopUp()));

          context.put("pageTitleEditable", Boolean.TRUE);
          context.put("page_title", (String) state.getAttribute(STATE_PAGE_TITLE));
        }
      } catch (Throwable e) {
      }
    }

    // pick the "-customize" template based on the standard template name
    String template = (String) getContext(data).get("template");

    // pick the site customize template if we are in that mode
    if (SPECIAL_WORKSITE.equals(special)) {
      template = template + "-site-customize";
    } else if (SPECIAL_WORKSPACE.equals(special)) {
      template = template + "-customize";
    } else if (SPECIAL_ANNOTATEDURL.equals(special)) {
      template = template + "-annotatedurl-customize";
    } else {
      template = template + "-customize";
    }

    // tracking event
    if (siteId.length() == 0) {
      try {
        Site s = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
        siteId = s.getId();
      } catch (Throwable e) {

      }
    }
    if (special == null) {
      if (state.getAttribute(EVENT_REVISE_WEB_CONTENT) == null) {

        // this is a Web Content tool
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                EVENT_REVISE_WEB_CONTENT, source, siteId, true, NotificationService.NOTI_NONE));
      } else {
        // event in tool registration file will be used
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                (String) state.getAttribute(EVENT_REVISE_WEB_CONTENT),
                source,
                siteId,
                true,
                NotificationService.NOTI_NONE));
      }
    } else {
      if (state.getAttribute(EVENT_REVISE_WEB_CONTENT) != null) {
        // special and event in tool registration file
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                (String) state.getAttribute(EVENT_REVISE_WEB_CONTENT),
                source,
                siteId,
                true,
                NotificationService.NOTI_NONE));
      }
    }

    // output the max limit
    context.put("max_length_title", MAX_TITLE_LENGTH);
    context.put("max_length_info_url", MAX_SITE_INFO_URL_LENGTH);

    return template;
  }
Example #10
0
  /** Setup the velocity context and choose the template for the response. */
  public String buildMainPanelContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    // do options if we are in options mode
    if (MODE_OPTIONS.equals(state.getAttribute(STATE_MODE))) {
      return buildOptionsPanelContext(portlet, context, rundata, state);
    }

    // if we rely on state (like all the other tools), we won't pick up any changes others make to
    // the configuration till we are refreshed... -ggolden

    // set our configuration into the context for the vm
    String url = (String) state.getAttribute(URL);
    String special = (String) state.getAttribute(SPECIAL);
    context.put(URL, url);
    context.put(HEIGHT, state.getAttribute(HEIGHT));

    // for annotatedurl
    context.put(TARGETPAGE_URL, state.getAttribute(TARGETPAGE_URL));
    context.put(TARGETPAGE_NAME, state.getAttribute(TARGETPAGE_NAME));
    context.put(ANNOTATED_TEXT, state.getAttribute(ANNOTATED_TEXT));

    // set the resource bundle with our strings
    context.put("tlang", rb);

    // setup for the options menu if needed

    String hideOptions = (String) state.getAttribute(HIDE_OPTIONS);

    if (hideOptions != null && "true".equalsIgnoreCase(hideOptions)) {
      // always hide Options menu if hide.options is specified
    } else if (SiteService.allowUpdateSite(ToolManager.getCurrentPlacement().getContext())) {

      context.put(
          "options_title",
          ToolManager.getCurrentPlacement().getTitle() + " " + rb.getString("gen.options"));
    }

    // tracking event
    String siteId = "";
    try {
      Site s = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
      siteId = s.getId();
    } catch (Throwable e) {

    }
    if (special == null) {
      if (state.getAttribute(EVENT_ACCESS_WEB_CONTENT) == null) {

        // this is a Web Content tool
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                EVENT_ACCESS_WEB_CONTENT, url, siteId, false, NotificationService.NOTI_NONE));
      } else {

        // event in tool registration file will be used
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                (String) state.getAttribute(EVENT_ACCESS_WEB_CONTENT),
                url,
                siteId,
                false,
                NotificationService.NOTI_NONE));
      }
    } else {
      if (state.getAttribute(EVENT_ACCESS_WEB_CONTENT) != null) {

        // special and event in tool registration file
        m_eventTrackingService.post(
            m_eventTrackingService.newEvent(
                (String) state.getAttribute(EVENT_ACCESS_WEB_CONTENT),
                url,
                siteId,
                false,
                NotificationService.NOTI_NONE));
      }
    }

    return (String) getContext(rundata).get("template");
  }