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)));
}
}
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);
}
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();
}
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);
}
}
}
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();
}
Aggregations