Search in sources :

Example 26 with PortalException

use of org.apereo.portal.PortalException in project uPortal by Jasig.

the class UpdatePreferencesServlet method updatePermissions.

@RequestMapping(method = RequestMethod.POST, params = "action=updatePermissions")
public ModelAndView updatePermissions(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    String elementId = request.getParameter("elementID");
    IUserLayoutNodeDescription node = ulm.getNode(elementId);
    if (node == null) {
        logger.warn("Failed to locate node for permissions update");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Invalid node id " + elementId));
    }
    String deletable = request.getParameter("deletable");
    if (!StringUtils.isBlank(deletable)) {
        node.setDeleteAllowed(Boolean.valueOf(deletable));
    }
    String movable = request.getParameter("movable");
    if (!StringUtils.isBlank(movable)) {
        node.setMoveAllowed(Boolean.valueOf(movable));
    }
    String editable = request.getParameter("editable");
    if (!StringUtils.isBlank(editable)) {
        node.setEditAllowed(Boolean.valueOf(editable));
    }
    String canAddChildren = request.getParameter("addChildAllowed");
    if (!StringUtils.isBlank(canAddChildren)) {
        node.setAddChildAllowed(Boolean.valueOf(canAddChildren));
    }
    ulm.updateNode(node);
    try {
        // save the user's layout
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    return new ModelAndView("jsonView", Collections.EMPTY_MAP);
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with PortalException

use of org.apereo.portal.PortalException in project uPortal by Jasig.

the class UpdatePreferencesServlet method removeElement.

/**
     * Remove an element from the layout.
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=removeElement")
public ModelAndView removeElement(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    try {
        // if the element ID starts with the fragment prefix and is a folder,
        // attempt first to treat it as a pulled fragment subscription
        String elementId = request.getParameter("elementID");
        if (elementId != null && elementId.startsWith(Constants.FRAGMENT_ID_USER_PREFIX) && ulm.getNode(elementId) instanceof org.apereo.portal.layout.node.UserLayoutFolderDescription) {
            removeSubscription(per, elementId, ulm);
        } else {
            // all node types, so we can just have a generic action.
            if (!ulm.deleteNode(elementId)) {
                logger.info("Failed to remove element ID {} from layout root folder ID {}, delete node returned false", elementId, ulm.getRootFolderId());
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.element.update", "Unable to update element", RequestContextUtils.getLocale(request))));
            }
        }
        ulm.saveUserLayout();
        return new ModelAndView("jsonView", Collections.EMPTY_MAP);
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with PortalException

use of org.apereo.portal.PortalException in project uPortal by Jasig.

the class UpdatePreferencesServlet method addPortlet.

/**
     * Add a new channel.
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws PortalException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=addPortlet")
public ModelAndView addPortlet(HttpServletRequest request, HttpServletResponse response) throws IOException, PortalException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    // gather the parameters we need to move a channel
    String destinationId = request.getParameter("elementID");
    String sourceId = request.getParameter("channelID");
    String method = request.getParameter("position");
    String fname = request.getParameter("fname");
    if (destinationId == null) {
        String tabName = request.getParameter("tabName");
        if (tabName != null) {
            destinationId = getTabIdFromName(ulm.getUserLayout(), tabName);
        }
    }
    IPortletDefinition definition = null;
    if (sourceId != null)
        definition = portletDefinitionRegistry.getPortletDefinition(sourceId);
    else if (fname != null)
        definition = portletDefinitionRegistry.getPortletDefinitionByFname(fname);
    else {
        logger.error("SourceId or fname invalid when adding a portlet");
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "SourceId or fname invalid"));
    }
    IUserLayoutChannelDescription channel = new UserLayoutChannelDescription(definition);
    IUserLayoutNodeDescription node = null;
    if (isTab(ulm, destinationId)) {
        node = addNodeToTab(ulm, channel, destinationId);
    } else {
        boolean isInsert = method != null && method.equals("insertBefore");
        //If neither an insert or type folder - Can't "insert into" non-folder
        if (!(isInsert || isFolder(ulm, destinationId))) {
            logger.error("Cannot insert into portlet element");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return new ModelAndView("jsonView", Collections.singletonMap("error", "Cannot insert into portlet element"));
        }
        String siblingId = isInsert ? destinationId : null;
        String target = isInsert ? ulm.getParentId(destinationId) : destinationId;
        // move the channel into the column
        node = ulm.addNode(channel, target, siblingId);
    }
    if (node == null) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.add.element", "Unable to add element", locale)));
    }
    String nodeId = node.getId();
    try {
        // save the user's layout
        ulm.saveUserLayout();
        if (addedWindowState != null) {
            IPortletWindow portletWindow = this.portletWindowRegistry.getOrCreateDefaultPortletWindowByFname(request, channel.getFunctionalName());
            portletWindow.setWindowState(addedWindowState);
            this.portletWindowRegistry.storePortletWindow(request, portletWindow);
        }
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    Map<String, String> model = new HashMap<String, String>();
    model.put("response", getMessage("success.add.portlet", "Added a new channel", locale));
    model.put("newNodeId", nodeId);
    return new ModelAndView("jsonView", model);
}
Also used : Locale(java.util.Locale) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) IUserInstance(org.apereo.portal.user.IUserInstance) PortalException(org.apereo.portal.PortalException) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserLayoutChannelDescription(org.apereo.portal.layout.node.UserLayoutChannelDescription) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with PortalException

use of org.apereo.portal.PortalException in project uPortal by Jasig.

the class UpdatePreferencesServlet method moveTab.

/**
     * Move a tab left or right.
     *
     * @param sourceId node ID of tab to move
     * @param method insertBefore or appendAfter. If appendAfter, tab is added as last tab (parent
     *     of destinationId).
     * @param destinationId insertBefore: node ID of tab to move sourceId before. insertAfter: node
     *     ID of another tab
     * @param request
     * @param response
     * @throws PortalException
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=moveTab")
public ModelAndView moveTab(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "sourceID") String sourceId, @RequestParam String method, @RequestParam(value = "elementID") String destinationId) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    // If we're moving this element before another one, we need
    // to know what the target is. If there's no target, just
    // assume we're moving it to the very end of the list.
    String siblingId = null;
    if ("insertBefore".equals(method))
        siblingId = destinationId;
    try {
        // move the node as requested and save the layout
        if (!ulm.moveNode(sourceId, ulm.getParentId(destinationId), siblingId)) {
            logger.warn("Failed to move tab in user layout. moveNode returned false");
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.move.tab", "There was an issue moving the tab, please refresh the page and try again.", locale)));
        }
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("success.move.tab", "Tab moved successfully", locale)));
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) Locale(java.util.Locale) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with PortalException

use of org.apereo.portal.PortalException in project uPortal by Jasig.

the class UpdatePreferencesServlet method subscribeToTab.

/**
     * Subscribe a user to a pre-formatted tab (pulled DLM fragment).
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=subscribeToTab")
public ModelAndView subscribeToTab(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    // Get the fragment owner's name from the request and construct
    // an IPerson object representing that user
    String fragmentOwnerName = request.getParameter("sourceID");
    if (StringUtils.isBlank(fragmentOwnerName)) {
        logger.warn("Attempted to subscribe to tab with null owner ID");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Attempted to subscribe to tab with null owner ID"));
    }
    RestrictedPerson fragmentOwner = PersonFactory.createRestrictedPerson();
    fragmentOwner.setUserName(fragmentOwnerName);
    // Mark the currently-authenticated user as subscribed to this fragment.
    // If an inactivated fragment registration already exists, update it
    // as an active subscription.  Otherwise, create a new fragment
    // subscription.
    IUserFragmentSubscription userFragmentInfo = userFragmentInfoDao.getUserFragmentInfo(per, fragmentOwner);
    if (userFragmentInfo == null) {
        userFragmentInfo = userFragmentInfoDao.createUserFragmentInfo(per, fragmentOwner);
    } else {
        userFragmentInfo.setActive(true);
        userFragmentInfoDao.updateUserFragmentInfo(userFragmentInfo);
    }
    try {
        // reload user layout and stylesheet to incorporate new DLM fragment
        ulm.loadUserLayout(true);
        // get the target node this new tab should be moved after
        String destinationId = request.getParameter("elementID");
        // get the user layout for the currently-authenticated user
        int uid = userIdentityStore.getPortalUID(fragmentOwner, false);
        final DistributedUserLayout userLayout = userLayoutStore.getUserLayout(per, upm.getUserProfile());
        Document layoutDocument = userLayout.getLayout();
        // attempt to find the new subscribed tab in the layout so we can
        // move it
        StringBuilder expression = new StringBuilder("//folder[@type='root']/folder[starts-with(@ID,'").append(Constants.FRAGMENT_ID_USER_PREFIX).append(uid).append("')]");
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(expression.toString(), layoutDocument, XPathConstants.NODESET);
        String sourceId = nodes.item(0).getAttributes().getNamedItem("ID").getTextContent();
        ulm.moveNode(sourceId, ulm.getParentId(destinationId), destinationId);
        ulm.saveUserLayout();
        return new ModelAndView("jsonView", Collections.singletonMap("tabId", sourceId));
    } catch (XPathExpressionException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Xpath error"));
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserFragmentSubscription(org.apereo.portal.fragment.subscribe.IUserFragmentSubscription) Document(org.w3c.dom.Document) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) XPathFactory(javax.xml.xpath.XPathFactory) RestrictedPerson(org.apereo.portal.security.provider.RestrictedPerson) DistributedUserLayout(org.apereo.portal.layout.dlm.DistributedUserLayout) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PortalException (org.apereo.portal.PortalException)47 Element (org.w3c.dom.Element)19 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)16 IUserInstance (org.apereo.portal.user.IUserInstance)14 Document (org.w3c.dom.Document)14 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)13 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 ModelAndView (org.springframework.web.servlet.ModelAndView)13 IPerson (org.apereo.portal.security.IPerson)9 Node (org.w3c.dom.Node)9 Locale (java.util.Locale)7 IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)7 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)7 HashMap (java.util.HashMap)6 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)6 UserLayoutFolderDescription (org.apereo.portal.layout.node.UserLayoutFolderDescription)6 XPathExpressionException (javax.xml.xpath.XPathExpressionException)5 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4