Search in sources :

Example 1 with PortletCategory

use of org.apereo.portal.portlet.om.PortletCategory 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) throws Exception {
    // 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());
    for (JsonEntityBean bean : form.getPrincipals()) {
        final String subscribePerm = bean.getTypeAndIdHash() + "_" + IPermission.PORTLET_SUBSCRIBER_ACTIVITY;
        final String browsePerm = bean.getTypeAndIdHash() + "_" + IPermission.PORTLET_BROWSE_ACTIVITY;
        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)) {
            subscribePrincipalSet.add(principal);
        }
        if (form.getPermissions().contains(browsePerm)) {
            browsePrincipalSet.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[prefValues.size()]);
            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.PORTLET_BROWSE_ACTIVITY);
    return this.createPortletDefinitionForm(publisher, portletDef.getPortletDefinitionId().getStringId());
}
Also used : BooleanAttribute(org.apereo.portal.portlets.BooleanAttribute) EntityEnum(org.apereo.portal.portlets.groupselector.EntityEnum) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) ArrayList(java.util.ArrayList) PortletPublishingDefinition(org.apereo.portal.portletpublishing.xml.PortletPublishingDefinition) TreeSet(java.util.TreeSet) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) HashSet(java.util.HashSet) IGroupMember(org.apereo.portal.groups.IGroupMember) JsonEntityBean(org.apereo.portal.layout.dlm.remoting.JsonEntityBean) IPortletType(org.apereo.portal.portlet.om.IPortletType) PortletDefinitionImpl(org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl)

Example 2 with PortletCategory

use of org.apereo.portal.portlet.om.PortletCategory in project uPortal by Jasig.

the class PortletCategoryRegistryImpl method getParentCategories.

/* (non-Javadoc)
     * @see org.apereo.portal.portlet.registry.IPortletCategoryRegistry#getParentCategories(org.apereo.portal.portlet.om.IPortletDefinition)
     */
@Override
public Set<PortletCategory> getParentCategories(IPortletDefinition child) {
    String childKey = child.getPortletDefinitionId().getStringId();
    IEntity childEntity = GroupService.getEntity(childKey, IPortletDefinition.class);
    Set<PortletCategory> parents = new HashSet<PortletCategory>();
    for (IGroupMember gm : childEntity.getParentGroups()) {
        if (gm.isGroup()) {
            String categoryId = gm.getKey();
            parents.add(getPortletCategory(categoryId));
        }
    }
    return parents;
}
Also used : IGroupMember(org.apereo.portal.groups.IGroupMember) IEntity(org.apereo.portal.groups.IEntity) HashSet(java.util.HashSet) PortletCategory(org.apereo.portal.portlet.om.PortletCategory)

Example 3 with PortletCategory

use of org.apereo.portal.portlet.om.PortletCategory in project uPortal by Jasig.

the class PortletMarketplaceController method getPermittedCategories.

private Set<PortletCategory> getPermittedCategories(PortletRequest req) {
    // default
    Set<PortletCategory> rslt = Collections.emptySet();
    final PortletPreferences prefs = req.getPreferences();
    final String[] permittedCategories = prefs.getValues(PERMITTED_CATEGORIES_PREFERENCE, new String[0]);
    if (permittedCategories.length != 0) {
        // Expensive to create, use cache for this collection...
        Set<String> cacheKey = new HashSet<>(Arrays.asList(permittedCategories));
        net.sf.ehcache.Element cacheElement = marketplaceCategoryCache.get(cacheKey);
        if (cacheElement == null) {
            // Nothing in cache currently;  need to populate cache
            HashSet<PortletCategory> portletCategories = new HashSet<>();
            for (final String categoryName : permittedCategories) {
                EntityIdentifier[] cats = GroupService.searchForGroups(categoryName, IGroupConstants.IS, IPortletDefinition.class);
                if (cats != null && cats.length > 0) {
                    PortletCategory pc = portletCategoryRegistry.getPortletCategory(cats[0].getKey());
                    if (pc != null) {
                        portletCategories.add(pc);
                    } else {
                        logger.warn("No PortletCategory found in portletCategoryRegistry for id '{}'", cats[0].getKey());
                    }
                } else {
                    logger.warn("No category found in GroupService for name '{}'", categoryName);
                }
            }
            /*
                 * Sanity Check:  Since at least 1 category name was specified, we
                 * need to make certain there's at least 1 PortletCategory in the
                 * set;  otherwise, a restricted Marketplace portlet would become
                 * an unrestricted one.
                 */
            if (portletCategories.isEmpty()) {
                throw new IllegalStateException("None of the specified category " + "names could be resolved to a PortletCategory:  " + Arrays.asList(permittedCategories));
            }
            cacheElement = new net.sf.ehcache.Element(cacheKey, portletCategories);
            this.marketplaceCategoryCache.put(cacheElement);
        }
        rslt = (Set<PortletCategory>) cacheElement.getObjectValue();
    }
    return rslt;
}
Also used : EntityIdentifier(org.apereo.portal.EntityIdentifier) PortletPreferences(javax.portlet.PortletPreferences) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) HashSet(java.util.HashSet)

Example 4 with PortletCategory

use of org.apereo.portal.portlet.om.PortletCategory in project uPortal by Jasig.

the class MarketplacePortletDefinition method initRelatedPortlets.

/**
     * Initialize related portlets. This must be called lazily so that MarketplacePortletDefinitions
     * instantiated as related portlets off of a MarketplacePortletDefinition do not always
     * instantiate their related MarketplacePortletDefinitions, ad infinitem.
     */
private void initRelatedPortlets() {
    final Set<MarketplacePortletDefinition> allRelatedPortlets = new HashSet<>();
    for (PortletCategory parentCategory : this.portletCategoryRegistry.getParentCategories(this)) {
        final Set<IPortletDefinition> portletsInCategory = this.portletCategoryRegistry.getAllChildPortlets(parentCategory);
        for (IPortletDefinition portletDefinition : portletsInCategory) {
            allRelatedPortlets.add(new MarketplacePortletDefinition(portletDefinition, this.marketplaceService, this.portletCategoryRegistry));
        }
    }
    allRelatedPortlets.remove(this);
    this.relatedPortlets = allRelatedPortlets;
}
Also used : HashSet(java.util.HashSet) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition)

Example 5 with PortletCategory

use of org.apereo.portal.portlet.om.PortletCategory in project uPortal by Jasig.

the class MarketplacePortletDefinition method initCategories.

/**
     * private method that sets the parentCategories field and the categories field This will ensure
     * that the public methods {@link #getParentCategories() getParentCategories()} and {@link
     * #getCategories() getCategories()} will not return null. Empty sets are allowed
     */
private void initCategories() {
    Set<PortletCategory> allCategories = new HashSet<PortletCategory>();
    this.setParentCategories(this.portletCategoryRegistry.getParentCategories(this));
    for (PortletCategory childCategory : this.parentCategories) {
        allCategories.add(childCategory);
        allCategories.addAll(this.portletCategoryRegistry.getAllParentCategories(childCategory));
    }
    this.setCategories(allCategories);
}
Also used : HashSet(java.util.HashSet) PortletCategory(org.apereo.portal.portlet.om.PortletCategory)

Aggregations

PortletCategory (org.apereo.portal.portlet.om.PortletCategory)29 HashSet (java.util.HashSet)19 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)12 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)7 EntityIdentifier (org.apereo.portal.EntityIdentifier)6 IGroupMember (org.apereo.portal.groups.IGroupMember)6 ArrayList (java.util.ArrayList)5 MarketplaceEntry (org.apereo.portal.rest.layout.MarketplaceEntry)5 IPerson (org.apereo.portal.security.IPerson)5 IEntityGroup (org.apereo.portal.groups.IEntityGroup)4 Locale (java.util.Locale)3 SortedSet (java.util.SortedSet)3 TreeSet (java.util.TreeSet)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 PortletPreferences (javax.portlet.PortletPreferences)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 IEntity (org.apereo.portal.groups.IEntity)2 ExternalPermissionDefinition (org.apereo.portal.io.xml.portlettype.ExternalPermissionDefinition)2