use of org.apereo.portal.portlet.om.IPortletPreference in project uPortal by Jasig.
the class HtmlPortletPreferenceSearchContentExtractorTest method testAppliesToMatchAll.
@Test
public void testAppliesToMatchAll() {
final IPortletDescriptorKey portletDescriptorKey = mock(IPortletDescriptorKey.class);
when(portletDescriptorKey.getPortletName()).thenAnswer(invocation -> PORTLET_NAME);
when(portletDescriptorKey.getWebAppName()).thenAnswer(invocation -> WEBAPP_NAME);
final IPortletPreference portletPreference = mock(IPortletPreference.class);
when(portletPreference.getName()).thenAnswer(invocation -> PREFERENCE_NAME);
final IPortletDefinition portletDefinition = mock(IPortletDefinition.class);
when(portletDefinition.getPortletDescriptorKey()).thenAnswer(invocation -> portletDescriptorKey);
when(portletDefinition.getPortletPreferences()).thenAnswer(invocation -> Collections.singletonList(portletPreference));
assertTrue(EXTRACTOR.appliesTo(portletDefinition));
}
use of org.apereo.portal.portlet.om.IPortletPreference in project uPortal by Jasig.
the class HtmlPortletPreferenceSearchContentExtractor method extractContent.
@Override
public String extractContent(IPortletDefinition portlet) {
// It's not ideal that we have to iterate the list to find the preference we want
final IPortletPreference preference = portlet.getPortletPreferences().stream().filter(item -> item.getName().equalsIgnoreCase(preferenceName)).findFirst().orElse(null);
if (preference == null) {
// Nothing we can index...
return null;
}
final StringBuilder stringBuilder = new StringBuilder();
Stream.of(preference.getValues()).forEach(item -> stringBuilder.append(item).append(" "));
// There must be a single root element
stringBuilder.insert(0, "<html><body>").append("</body></html>");
final String html = stringBuilder.toString();
try {
// Use JSoup to parse the HTML b/c it's often pretty sketchy
final Document doc = Jsoup.parse(html);
final String body = doc.body().text().trim();
return body.length() > 0 ? body : null;
} catch (Exception e) {
logger.warn("Failed to index preference '{}' for portlet with fname='{}'", preference.getName(), portlet.getFName(), e);
}
// Indexing failed
return null;
}
use of org.apereo.portal.portlet.om.IPortletPreference in project uPortal by Jasig.
the class PortletAdministrationHelper method savePortletRegistration.
/**
* Persist a new or edited PortletDefinition from a form, replacing existing values.
*
* @param publisher {@code IPerson} that requires permission to save this definition
* @param form form data to persist
* @return new {@code PortletDefinitionForm} for this portlet ID
*/
public PortletDefinitionForm savePortletRegistration(IPerson publisher, PortletDefinitionForm form) {
logger.trace("In savePortletRegistration() - for: {}", form.getPortletName());
// is made when the user enters the lifecycle-selection step in the wizard.)
if (!hasLifecyclePermission(publisher, form.getLifecycleState(), form.getCategories())) {
logger.warn("User '" + publisher.getUserName() + "' attempted to save the following portlet without the selected MANAGE permission: " + form);
throw new SecurityException("Not Authorized");
}
if (!form.isNew()) {
// User must have the previous lifecycle permission
// in AT LEAST ONE previous category as well
IPortletDefinition def = this.portletDefinitionRegistry.getPortletDefinition(form.getId());
Set<PortletCategory> categories = portletCategoryRegistry.getParentCategories(def);
SortedSet<JsonEntityBean> categoryBeans = new TreeSet<>();
for (PortletCategory cat : categories) {
categoryBeans.add(new JsonEntityBean(cat));
}
if (!hasLifecyclePermission(publisher, def.getLifecycleState(), categoryBeans)) {
logger.warn("User '" + publisher.getUserName() + "' attempted to save the following portlet without the previous MANAGE permission: " + form);
throw new SecurityException("Not Authorized");
}
}
if (form.isNew() || portletDefinitionRegistry.getPortletDefinition(form.getId()).getType().getId() != form.getTypeId()) {
// User must have access to the selected CPD if s/he selected it in this interaction
final int selectedTypeId = form.getTypeId();
final PortletPublishingDefinition cpd = portletPublishingDefinitionDao.getChannelPublishingDefinition(selectedTypeId);
final Map<IPortletType, PortletPublishingDefinition> allowableCpds = this.getAllowableChannelPublishingDefinitions(publisher);
if (!allowableCpds.containsValue(cpd)) {
logger.warn("User '" + publisher.getUserName() + "' attempted to administer the following portlet without the selected " + IPermission.PORTLET_MANAGER_SELECT_PORTLET_TYPE + " permission: " + form);
throw new SecurityException("Not Authorized");
}
}
// create the principal array from the form's principal list -- only principals with
// permissions
final Set<IGroupMember> subscribePrincipalSet = new HashSet<>(form.getPrincipals().size());
final Set<IGroupMember> browsePrincipalSet = new HashSet<>(form.getPrincipals().size());
final Set<IGroupMember> configurePrincipalSet = new HashSet<>(form.getPrincipals().size());
for (JsonEntityBean bean : form.getPrincipals()) {
final String subscribePerm = bean.getTypeAndIdHash() + "_" + IPermission.PORTLET_SUBSCRIBER_ACTIVITY;
final String browsePerm = bean.getTypeAndIdHash() + "_" + IPermission.PORTLET_BROWSE_ACTIVITY;
final String configurePerm = bean.getTypeAndIdHash() + "_" + IPermission.PORTLET_MODE_CONFIG;
final EntityEnum entityEnum = bean.getEntityType();
final IGroupMember principal = entityEnum.isGroup() ? (GroupService.findGroup(bean.getId())) : (GroupService.getGroupMember(bean.getId(), entityEnum.getClazz()));
if (form.getPermissions().contains(subscribePerm)) {
logger.info("In savePortletRegistration() - Found a subscribePerm for principal: {}", principal);
subscribePrincipalSet.add(principal);
}
if (form.getPermissions().contains(browsePerm)) {
logger.info("In savePortletRegistration() - Found a browsePerm for principal: {}", principal);
browsePrincipalSet.add(principal);
}
if (form.getPermissions().contains(configurePerm)) {
logger.info("In savePortletRegistration() - Found a configurePerm for principal: {}", principal);
configurePrincipalSet.add(principal);
}
}
// create the category list from the form's category bean list
List<PortletCategory> categories = new ArrayList<>();
for (JsonEntityBean category : form.getCategories()) {
String id = category.getId();
String iCatID = id.startsWith("cat") ? id.substring(3) : id;
categories.add(portletCategoryRegistry.getPortletCategory(iCatID));
}
final IPortletType portletType = portletTypeRegistry.getPortletType(form.getTypeId());
if (portletType == null) {
throw new IllegalArgumentException("No IPortletType exists for ID " + form.getTypeId());
}
IPortletDefinition portletDef;
if (form.getId() == null) {
portletDef = new PortletDefinitionImpl(portletType, form.getFname(), form.getName(), form.getTitle(), form.getApplicationId(), form.getPortletName(), form.isFramework());
} else {
portletDef = portletDefinitionRegistry.getPortletDefinition(form.getId());
portletDef.setType(portletType);
portletDef.setFName(form.getFname());
portletDef.setName(form.getName());
portletDef.setTitle(form.getTitle());
portletDef.getPortletDescriptorKey().setWebAppName(form.getApplicationId());
portletDef.getPortletDescriptorKey().setPortletName(form.getPortletName());
portletDef.getPortletDescriptorKey().setFrameworkPortlet(form.isFramework());
}
portletDef.setDescription(form.getDescription());
portletDef.setTimeout(form.getTimeout());
// portletDef reflect the state of the form, in case any have changed.
for (String key : form.getParameters().keySet()) {
String value = form.getParameters().get(key).getValue();
if (!StringUtils.isBlank(value)) {
portletDef.addParameter(key, value);
}
}
portletDef.addParameter(IPortletDefinition.EDITABLE_PARAM, Boolean.toString(form.isEditable()));
portletDef.addParameter(IPortletDefinition.CONFIGURABLE_PARAM, Boolean.toString(form.isConfigurable()));
portletDef.addParameter(IPortletDefinition.HAS_HELP_PARAM, Boolean.toString(form.isHasHelp()));
portletDef.addParameter(IPortletDefinition.HAS_ABOUT_PARAM, Boolean.toString(form.isHasAbout()));
// Now add portlet preferences
List<IPortletPreference> preferenceList = new ArrayList<>();
for (String key : form.getPortletPreferences().keySet()) {
List<String> prefValues = form.getPortletPreferences().get(key).getValue();
if (prefValues != null && prefValues.size() > 0) {
String[] values = prefValues.toArray(new String[0]);
BooleanAttribute readOnly = form.getPortletPreferenceReadOnly().get(key);
preferenceList.add(new PortletPreferenceImpl(key, readOnly.getValue(), values));
}
}
portletDef.setPortletPreferences(preferenceList);
// Lastly update the PortletDefinition's lifecycle state & lifecycle-related metadata
updateLifecycleState(form, portletDef, publisher);
// The final parameter of IGroupMembers is used to set the initial SUBSCRIBE permission set
portletPublishingService.savePortletDefinition(portletDef, publisher, categories, new ArrayList<>(subscribePrincipalSet));
// updatePermissions(portletDef, subscribePrincipalSet,
// IPermission.PORTLET_SUBSCRIBER_ACTIVITY);
updatePermissions(portletDef, browsePrincipalSet, IPermission.PORTAL_SUBSCRIBE, IPermission.PORTLET_BROWSE_ACTIVITY);
updatePermissions(portletDef, configurePrincipalSet, IPermission.PORTAL_PUBLISH, IPermission.PORTLET_MODE_CONFIG);
return this.createPortletDefinitionForm(publisher, portletDef.getPortletDefinitionId().getStringId());
}
use of org.apereo.portal.portlet.om.IPortletPreference in project uPortal by Jasig.
the class AbstractPortletPreferencesImplTest method testSetMatchesBase.
@Test
public void testSetMatchesBase() throws ReadOnlyException, ValidatorException, IOException {
addPref(basePrefs, "key", false, new String[] { "default" });
// Set a modified value
portletPreferences.setValues("key", new String[] { "modified" });
// Initial store, check that correct stored map is created
portletPreferences.store();
// Actually "store" the stored prefs
this.targetPrefs = new LinkedHashMap<String, IPortletPreference>(this.storedPrefs);
assertEquals(1, this.storedPrefs.size());
IPortletPreference pref = this.storedPrefs.get("key");
assertNotNull(pref);
assertEquals("key", pref.getName());
assertArrayEquals(new String[] { "modified" }, pref.getValues());
assertFalse(pref.isReadOnly());
// Set the default value
portletPreferences.setValues("key", new String[] { "default" });
// Store again, should have nothing stored after this
portletPreferences.store();
assertEquals(0, this.storedPrefs.size());
}
use of org.apereo.portal.portlet.om.IPortletPreference in project uPortal by Jasig.
the class AbstractPortletPreferencesImplTest method testNullSetValueValues.
@Test
public void testNullSetValueValues() throws ReadOnlyException, ValidatorException, IOException {
portletPreferences.setValues("key", null);
portletPreferences.store();
assertEquals(1, this.storedPrefs.size());
final IPortletPreference pref = this.storedPrefs.get("key");
assertNotNull(pref);
assertEquals("key", pref.getName());
assertNull(pref.getValues());
assertFalse(pref.isReadOnly());
}
Aggregations