Search in sources :

Example 6 with UserPreferencesManager

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

the class FavoritesController method initializeView.

/**
 * Handles all Favorites portlet VIEW mode renders. Populates model with user's favorites and
 * selects a view to display those favorites.
 *
 * <p>View selection:
 *
 * <p>Returns "jsp/Favorites/view" in the normal case where the user has at least one favorited
 * portlet or favorited collection.
 *
 * <p>Returns "jsp/Favorites/view_zero" in the edge case where the user has zero favorited
 * portlets AND zero favorited collections.
 *
 * <p>Model: marketPlaceFname --> String functional name of Marketplace portlet, or null if not
 * available. collections --> List of favorited collections (IUserLayoutNodeDescription s)
 * favorites --> List of favorited individual portlets (IUserLayoutNodeDescription s)
 *
 * @param model . Spring model. This method adds three model attributes.
 * @return jsp/Favorites/view[_zero]
 */
@RenderMapping
public String initializeView(PortletRequest req, Model model) {
    final IUserInstance ui = userInstanceManager.getUserInstance(portalRequestUtils.getCurrentPortalRequest());
    final UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    final IUserLayoutManager ulm = upm.getUserLayoutManager();
    final IUserLayout userLayout = ulm.getUserLayout();
    // TODO: the portlet could predicate including a non-null marketplace portlet fname
    // on the accessing user having permission to render the portlet referenced by that fname
    // so that portlet would gracefully degrade when configured with bad marketplace portlet
    // fname
    // and also gracefully degrade when the accessing user doesn't have permission to access an
    // otherwise
    // viable configured marketplace.  This complexity may not be worth it.  Anyway it is not
    // yet implemented.
    model.addAttribute("marketplaceFname", this.marketplaceFName);
    final List<IUserLayoutNodeDescription> collections = FavoritesUtils.getFavoriteCollections(userLayout);
    model.addAttribute("collections", collections);
    final List<IUserLayoutNodeDescription> rawFavorites = FavoritesUtils.getFavoritePortlets(userLayout);
    /*
         * Filter the collection by SUBSCRIBE permission.
         *
         * NOTE:  In the "regular" (non-Favorites) layout, this permissions check is handled by
         * the rendering engine.  It will refuse to spawn a worker for a portlet to which you
         * cannot SUBSCRIBE.
         */
    final String username = req.getRemoteUser() != null ? req.getRemoteUser() : // First item is the default
    PersonFactory.getGuestUsernames().get(0);
    final IAuthorizationPrincipal principal = authorizationService.newPrincipal(username, IPerson.class);
    final List<IUserLayoutNodeDescription> favorites = new ArrayList<>();
    for (IUserLayoutNodeDescription nodeDescription : rawFavorites) {
        if (nodeDescription instanceof IUserLayoutChannelDescription) {
            final IUserLayoutChannelDescription channelDescription = (IUserLayoutChannelDescription) nodeDescription;
            if (principal.canRender(channelDescription.getChannelPublishId())) {
                favorites.add(nodeDescription);
            }
        }
    }
    model.addAttribute("favorites", favorites);
    // default to the regular old view
    String viewName = "jsp/Favorites/view";
    if (collections.isEmpty() && favorites.isEmpty()) {
        // special edge case of zero favorites, switch to special view
        viewName = "jsp/Favorites/view_zero";
    }
    logger.debug("Favorites Portlet VIEW mode render populated model [{}] for render by view {}.", model, viewName);
    return viewName;
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) ArrayList(java.util.ArrayList) IUserLayout(org.apereo.portal.layout.IUserLayout) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) RenderMapping(org.springframework.web.portlet.bind.annotation.RenderMapping)

Example 7 with UserPreferencesManager

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

the class FavoritesController method initializeView.

/**
     * Handles all Favorites portlet VIEW mode renders. Populates model with user's favorites and
     * selects a view to display those favorites.
     *
     * <p>View selection:
     *
     * <p>Returns "jsp/Favorites/view" in the normal case where the user has at least one favorited
     * portlet or favorited collection.
     *
     * <p>Returns "jsp/Favorites/view_zero" in the edge case where the user has zero favorited
     * portlets AND zero favorited collections.
     *
     * <p>Model: marketPlaceFname --> String functional name of Marketplace portlet, or null if not
     * available. collections --> List of favorited collections (IUserLayoutNodeDescription s)
     * favorites --> List of favorited individual portlets (IUserLayoutNodeDescription s)
     *
     * @param model . Spring model. This method adds three model attributes.
     * @return jsp/Favorites/view[_zero]
     */
@RenderMapping
public String initializeView(Model model) {
    IUserInstance ui = userInstanceManager.getUserInstance(portalRequestUtils.getCurrentPortalRequest());
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    IUserLayout userLayout = ulm.getUserLayout();
    // TODO: the portlet could predicate including a non-null marketplace portlet fname
    // on the accessing user having permission to render the portlet referenced by that fname
    // so that portlet would gracefully degrade when configured with bad marketplace portlet fname
    // and also gracefully degrade when the accessing user doesn't have permission to access an otherwise
    // viable configured marketplace.  This complexity may not be worth it.  Anyway it is not yet implemented.
    model.addAttribute("marketplaceFname", this.marketplaceFName);
    List<IUserLayoutNodeDescription> collections = FavoritesUtils.getFavoriteCollections(userLayout);
    model.addAttribute("collections", collections);
    List<IUserLayoutNodeDescription> favorites = FavoritesUtils.getFavoritePortlets(userLayout);
    model.addAttribute("favorites", favorites);
    // default to the regular old view
    String viewName = "jsp/Favorites/view";
    if (collections.isEmpty() && favorites.isEmpty()) {
        // special edge case of zero favorites, switch to special view
        viewName = "jsp/Favorites/view_zero";
    }
    logger.trace("Favorites Portlet VIEW mode render populated model [{}] for render by view {}.", model, viewName);
    return viewName;
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserLayout(org.apereo.portal.layout.IUserLayout) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) RenderMapping(org.springframework.web.portlet.bind.annotation.RenderMapping)

Example 8 with UserPreferencesManager

use of org.apereo.portal.UserPreferencesManager 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)

Example 9 with UserPreferencesManager

use of org.apereo.portal.UserPreferencesManager 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.emptyMap());
}
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 10 with UserPreferencesManager

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

the class UpdatePreferencesServlet method addFavorite.

@RequestMapping(method = RequestMethod.POST, params = "action=addFavorite")
public ModelAndView addFavorite(@RequestParam String channelId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    final IUserInstance ui = userInstanceManager.getUserInstance(request);
    final IPerson person = getPerson(ui, response);
    final IPortletDefinition pdef = portletDefinitionRegistry.getPortletDefinition(channelId);
    final Locale locale = RequestContextUtils.getLocale(request);
    final IAuthorizationPrincipal authPrincipal = this.getUserPrincipal(person.getUserName());
    final String targetString = PermissionHelper.permissionTargetIdForPortletDefinition(pdef);
    if (!authPrincipal.hasPermission(IPermission.PORTAL_SYSTEM, IPermission.PORTLET_FAVORITE_ACTIVITY, targetString)) {
        logger.warn("Unauthorized attempt to favorite portlet '{}' through the REST API by user '{}'", pdef.getFName(), person.getUserName());
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.favorite.not.permitted", "Favorite not permitted", locale)));
    }
    final UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    final IUserLayoutManager ulm = upm.getUserLayoutManager();
    final IUserLayoutChannelDescription channel = new UserLayoutChannelDescription(pdef);
    // get favorite tab
    final String favoriteTabNodeId = FavoritesUtils.getFavoriteTabNodeId(ulm.getUserLayout());
    if (favoriteTabNodeId != null) {
        // add portlet to favorite tab
        final IUserLayoutNodeDescription node = addNodeToTab(ulm, channel, favoriteTabNodeId);
        if (node == null) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.add.portlet.in.tab", "Can''t add a new favorite", locale)));
        }
        try {
            // save the user's layout
            ulm.saveUserLayout();
        } catch (PortalException e) {
            return handlePersistError(request, response, e);
        }
        // document success for notifications
        final Map<String, String> model = new HashMap<>();
        final String channelTitle = channel.getTitle();
        model.put("response", getMessage("favorites.added.favorite", channelTitle, "Added " + channelTitle + " as a favorite.", locale));
        model.put("newNodeId", node.getId());
        return new ModelAndView("jsonView", model);
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.finding.favorite.tab", "Can''t find favorite tab", locale)));
    }
}
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) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) 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)

Aggregations

UserPreferencesManager (org.apereo.portal.UserPreferencesManager)17 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)17 IUserInstance (org.apereo.portal.user.IUserInstance)15 PortalException (org.apereo.portal.PortalException)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ModelAndView (org.springframework.web.servlet.ModelAndView)11 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)9 IPerson (org.apereo.portal.security.IPerson)7 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)4 IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)4 IUserLayout (org.apereo.portal.layout.IUserLayout)3 UserLayoutChannelDescription (org.apereo.portal.layout.node.UserLayoutChannelDescription)3 UserLayoutFolderDescription (org.apereo.portal.layout.node.UserLayoutFolderDescription)3 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)3 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)3 RenderMapping (org.springframework.web.portlet.bind.annotation.RenderMapping)3 ArrayList (java.util.ArrayList)2 DistributedUserLayout (org.apereo.portal.layout.dlm.DistributedUserLayout)2