use of org.apereo.portal.fragment.subscribe.IUserFragmentSubscription in project uPortal by Jasig.
the class UpdatePreferencesServlet method removeSubscription.
protected void removeSubscription(IPerson per, String elementId, IUserLayoutManager ulm) {
// get the fragment owner's ID from the element string
String userIdString = StringUtils.substringBetween(elementId, Constants.FRAGMENT_ID_USER_PREFIX, Constants.FRAGMENT_ID_LAYOUT_PREFIX);
int userId = NumberUtils.toInt(userIdString, 0);
// construct a new person object representing the fragment owner
RestrictedPerson fragmentOwner = PersonFactory.createRestrictedPerson();
fragmentOwner.setID(userId);
fragmentOwner.setUserName(userIdentityStore.getPortalUserName(userId));
// attempt to find a subscription for this fragment
IUserFragmentSubscription subscription = userFragmentInfoDao.getUserFragmentInfo(per, fragmentOwner);
// if a subscription was found, remove it's registration
if (subscription != null) {
userFragmentInfoDao.deleteUserFragmentInfo(subscription);
ulm.loadUserLayout(true);
} else // otherwise, delete the node
{
ulm.deleteNode(elementId);
}
}
use of org.apereo.portal.fragment.subscribe.IUserFragmentSubscription in project uPortal by Jasig.
the class SubscribableTabsRESTController method getSubscriptionList.
@RequestMapping(value = "/subscribableTabs.json", method = RequestMethod.GET)
public ModelAndView getSubscriptionList(HttpServletRequest request) {
Map<String, Object> model = new HashMap<String, Object>();
/** Retrieve the IPerson and IAuthorizationPrincipal for the currently authenticated user */
IUserInstance userInstance = userInstanceManager.getUserInstance(request);
IPerson person = userInstance.getPerson();
AuthorizationService authService = AuthorizationService.instance();
IAuthorizationPrincipal principal = authService.newPrincipal(person.getUserName(), IPerson.class);
/**
* Build a collection of owner IDs for the fragments to which the authenticated user is
* subscribed
*/
// get the list of current subscriptions for this user
List<IUserFragmentSubscription> subscriptions = userFragmentSubscriptionDao.getUserFragmentInfo(person);
// transform it into the set of owners
Set<String> subscribedOwners = new HashSet<String>();
for (IUserFragmentSubscription subscription : subscriptions) {
if (subscription.isActive()) {
subscribedOwners.add(subscription.getFragmentOwner());
}
}
/**
* Iterate through the list of all currently defined DLM fragments and determine if the
* current user has permissions to subscribe to each. Any subscribable fragments will be
* transformed into a JSON-friendly bean and added to the model.
*/
final List<SubscribableFragment> jsonFragments = new ArrayList<SubscribableFragment>();
// get the list of fragment definitions from DLM
final List<FragmentDefinition> fragmentDefinitions = configurationLoader.getFragments();
final Locale locale = RequestContextUtils.getLocale(request);
// iterate through the list
for (FragmentDefinition fragmentDefinition : fragmentDefinitions) {
if (isSubscribable(fragmentDefinition, principal)) {
String owner = fragmentDefinition.getOwnerId();
// this fragment
if (principal.hasPermission("UP_FRAGMENT", "FRAGMENT_SUBSCRIBE", owner)) {
// create a JSON fragment bean and add it to our list
boolean subscribed = subscribedOwners.contains(owner);
final String name = getMessage("fragment." + owner + ".name", fragmentDefinition.getName(), locale);
final String description = getMessage("fragment." + owner + ".description", fragmentDefinition.getDescription(), locale);
SubscribableFragment jsonFragment = new SubscribableFragment(name, description, owner, subscribed);
jsonFragments.add(jsonFragment);
}
}
}
model.put("fragments", jsonFragments);
return new ModelAndView("json", model);
}
use of org.apereo.portal.fragment.subscribe.IUserFragmentSubscription in project uPortal by Jasig.
the class SubscribedFragmentImporterExporter method deleteData.
/*
* (non-Javadoc)
* @see org.apereo.portal.io.xml.IDataImporterExporter#deleteData(java.lang.String)
*/
@Transactional
@Override
public ExternalSubscribedFragments deleteData(String id) {
final IPerson person = this.getPerson(id, false);
if (person == null) {
//Nothing to delete
return null;
}
final ExternalSubscribedFragments data = exportInternal(person);
for (final IUserFragmentSubscription userFragmentSubscription : this.userFragmentSubscriptionDao.getUserFragmentInfo(person)) {
this.userFragmentSubscriptionDao.deleteUserFragmentInfo(userFragmentSubscription);
}
return data;
}
use of org.apereo.portal.fragment.subscribe.IUserFragmentSubscription in project uPortal by Jasig.
the class SubscribedTabEvaluator method isApplicable.
@Override
public boolean isApplicable(IPerson person) {
IUserFragmentSubscriptionDao userFragmentInfoDao = UserFragmentSubscriptionDaoLocator.getUserIdentityStore();
// get the list of current fragment subscriptions for this person
List<IUserFragmentSubscription> fragments = userFragmentInfoDao.getUserFragmentInfo(person);
// with this evaluator instance
for (IUserFragmentSubscription fragment : fragments) {
if (fragment.isActive() && fragment.getFragmentOwner().equals(ownerId)) {
return true;
}
}
return false;
}
use of org.apereo.portal.fragment.subscribe.IUserFragmentSubscription 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);
}
}
Aggregations