Search in sources :

Example 41 with IUserInstance

use of org.apereo.portal.user.IUserInstance 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<String, String>();
        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)

Example 42 with IUserInstance

use of org.apereo.portal.user.IUserInstance in project uPortal by Jasig.

the class UpdatePreferencesServlet method changeColumns.

/**
     * Change the number of columns on a specified tab. In the event that the user is decreasing the
     * number of columns, extra columns will be stripped from the right-hand side. Any channels in
     * these columns will be moved to the bottom of the last preserved column.
     *
     * @param widths array of column widths
     * @param deleted array of deleted column IDs
     * @param acceptor not sure what this is
     * @param request HttpRequest
     * @param response HttpResponse
     * @throws IOException
     * @throws PortalException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=changeColumns")
public ModelAndView changeColumns(HttpServletRequest request, HttpServletResponse response, @RequestParam("tabId") String tabId, @RequestParam("widths[]") String[] widths, @RequestParam(value = "deleted[]", required = false) String[] deleted, @RequestParam(value = "acceptor", required = false) String acceptor) throws IOException, PortalException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    int newColumnCount = widths.length;
    // build a list of the current columns for this tab
    Enumeration<String> columns = ulm.getChildIds(tabId);
    List<String> columnList = new ArrayList<String>();
    while (columns.hasMoreElements()) {
        columnList.add(columns.nextElement());
    }
    int oldColumnCount = columnList.size();
    Map<String, Object> model = new HashMap<String, Object>();
    // if the new layout has more columns
    if (newColumnCount > oldColumnCount) {
        List<String> newColumnIds = new ArrayList<String>();
        for (int i = columnList.size(); i < newColumnCount; i++) {
            // create new column element
            IUserLayoutFolderDescription newColumn = new UserLayoutFolderDescription();
            newColumn.setName("Column");
            newColumn.setId("tbd");
            newColumn.setFolderType(IUserLayoutFolderDescription.REGULAR_TYPE);
            newColumn.setHidden(false);
            newColumn.setUnremovable(false);
            newColumn.setImmutable(false);
            // add the column to our layout
            IUserLayoutNodeDescription node = ulm.addNode(newColumn, tabId, null);
            newColumnIds.add(node.getId());
            model.put("newColumnIds", newColumnIds);
            columnList.add(node.getId());
        }
    } else // if the new layout has fewer columns
    if (deleted != null && deleted.length > 0) {
        if (columnList.size() != widths.length + deleted.length) {
        // TODO: error?
        }
        for (String columnId : deleted) {
            // move all channels in the current column to the last valid column
            Enumeration channels = ulm.getChildIds(columnId);
            while (channels.hasMoreElements()) {
                ulm.addNode(ulm.getNode((String) channels.nextElement()), acceptor, null);
            }
            // delete the column from the user's layout
            ulm.deleteNode(columnId);
            columnList.remove(columnId);
        }
    }
    int count = 0;
    for (String columnId : columnList) {
        this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, columnId, "width", widths[count] + "%");
        try {
            // This sets the column attribute in memory but doesn't persist it.  Comment says saves changes "prior to persisting"
            Element folder = ulm.getUserLayoutDOM().getElementById(columnId);
            UserPrefsHandler.setUserPreference(folder, "width", per);
        } catch (Exception e) {
            logger.error("Error saving new column widths", e);
        }
        count++;
    }
    try {
        ulm.saveUserLayout();
    } catch (PortalException e) {
        logger.warn("Error saving layout", e);
    }
    return new ModelAndView("jsonView", model);
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PortalException(org.apereo.portal.PortalException) IOException(java.io.IOException) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with IUserInstance

use of org.apereo.portal.user.IUserInstance in project uPortal by Jasig.

the class LocaleManagerLocaleResolver method resolveLocale.

@Override
public Locale resolveLocale(HttpServletRequest request) {
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final LocaleManager localeManager = userInstance.getLocaleManager();
    Locale[] locales = localeManager.getLocales();
    if (locales != null && locales.length > 0) {
        return locales[0];
    }
    // specified by "accept-locale" HTTP header
    return request.getLocale();
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) Locale(java.util.Locale)

Example 44 with IUserInstance

use of org.apereo.portal.user.IUserInstance in project uPortal by Jasig.

the class LocaleManagerLocaleResolver method setLocale.

@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final LocaleManager localeManager = userInstance.getLocaleManager();
    localeManager.setSessionLocales(new Locale[] { locale });
    // if the current user is logged in, also update the persisted user locale
    final IUserInstance ui = userInstanceManager.getUserInstance(request);
    final IPerson person = ui.getPerson();
    if (!person.isGuest()) {
        try {
            localeManager.persistUserLocales(new Locale[] { locale });
            localeStore.updateUserLocales(person, new Locale[] { locale });
            final IUserPreferencesManager upm = ui.getPreferencesManager();
            upm.getUserLayoutManager().loadUserLayout();
        } catch (Exception e) {
            throw new PortalException(e);
        }
    }
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) PortalException(org.apereo.portal.PortalException) PortalException(org.apereo.portal.PortalException)

Example 45 with IUserInstance

use of org.apereo.portal.user.IUserInstance in project uPortal by Jasig.

the class SingleTabUrlNodeSyntaxHelper method getDefaultLayoutNodeId.

@Override
public String getDefaultLayoutNodeId(HttpServletRequest httpServletRequest) {
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(httpServletRequest);
    final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
    final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
    final IUserLayout userLayout = userLayoutManager.getUserLayout();
    //This logic is specific to tab/column layouts
    final String defaultTabIndex = this.getDefaultTabIndex(httpServletRequest);
    if (defaultTabIndex != null) {
        final String defaultTabId = this.getTabId(userLayout, defaultTabIndex);
        if (StringUtils.isNotEmpty(defaultTabId)) {
            return defaultTabId;
        }
    }
    this.logger.warn("Failed to find default tab id for '" + userInstance.getPerson().getUserName() + "' with default tab index " + defaultTabIndex + ". Index 1 will be tried as a fall-back.");
    final String firstTabId = getTabId(userLayout, "1");
    if (StringUtils.isNotEmpty(firstTabId)) {
        return firstTabId;
    }
    this.logger.warn("Failed to find default tab id for '" + userInstance.getPerson().getUserName() + "' with default tab index 1. The user has no tabs.");
    return userLayout.getRootId();
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) IUserLayout(org.apereo.portal.layout.IUserLayout) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager)

Aggregations

IUserInstance (org.apereo.portal.user.IUserInstance)62 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)28 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)27 IPerson (org.apereo.portal.security.IPerson)25 PortalException (org.apereo.portal.PortalException)14 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)12 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)12 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)12 ModelAndView (org.springframework.web.servlet.ModelAndView)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 IUserProfile (org.apereo.portal.IUserProfile)10 Locale (java.util.Locale)9 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)7 HashMap (java.util.HashMap)6 IUserLayout (org.apereo.portal.layout.IUserLayout)6 IPortletEntityId (org.apereo.portal.portlet.om.IPortletEntityId)6 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)6 UserLayoutFolderDescription (org.apereo.portal.layout.node.UserLayoutFolderDescription)5