Search in sources :

Example 1 with PortletDefinitionImpl

use of org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl 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 PortletDefinitionImpl

use of org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl in project uPortal by Jasig.

the class PortletDefinitionImporterExporter method importData.

@Transactional
@Override
public void importData(ExternalPortletDefinition portletRep) {
    final PortletDescriptor portletDescriptor = portletRep.getPortletDescriptor();
    final Boolean isFramework = portletDescriptor.isIsFramework();
    if (isFramework != null && isFramework && "UPGRADED_CHANNEL_IS_NOT_A_PORTLET".equals(portletDescriptor.getPortletName())) {
        if (errorOnChannel) {
            throw new IllegalArgumentException(portletRep.getFname() + " is not a portlet. It was likely an IChannel from a previous version of uPortal and cannot be imported.");
        }
        logger.warn(portletRep.getFname() + " is not a portlet. It was likely an IChannel from a previous version of uPortal and will not be imported.");
        return;
    }
    // get the portlet type
    final IPortletType portletType = portletTypeRegistry.getPortletType(portletRep.getType());
    if (portletType == null) {
        throw new IllegalArgumentException("No portlet type registered for: " + portletRep.getType());
    }
    final List<PortletCategory> categories = new ArrayList<PortletCategory>();
    for (String categoryName : portletRep.getCategories()) {
        EntityIdentifier[] cats = GroupService.searchForGroups(categoryName, IGroupConstants.IS, IPortletDefinition.class);
        PortletCategory category = null;
        if (cats != null && cats.length > 0) {
            category = portletCategoryRegistry.getPortletCategory(cats[0].getKey());
        } else {
            category = portletCategoryRegistry.getPortletCategory(categoryName);
        }
        if (category == null) {
            throw new IllegalArgumentException("No category '" + categoryName + "' found when importing portlet: " + portletRep.getFname());
        }
        categories.add(category);
    }
    final String fname = portletRep.getFname();
    final Map<ExternalPermissionDefinition, Set<IGroupMember>> permissions = new HashMap<>();
    final Set<IGroupMember> subscribeMembers = toGroupMembers(portletRep.getGroups(), fname);
    permissions.put(ExternalPermissionDefinition.SUBSCRIBE, subscribeMembers);
    if (portletRep.getPermissions() != null && portletRep.getPermissions().getPermissions() != null) {
        for (ExternalPermissionMemberList perm : portletRep.getPermissions().getPermissions()) {
            Set<IGroupMember> members = toGroupMembers(perm.getGroups(), fname);
            ExternalPermissionDefinition permDef = toExternalPermissionDefinition(perm.getSystem(), perm.getActivity());
            if (permissions.containsKey(permDef)) {
                permissions.get(permDef).addAll(members);
            } else {
                permissions.put(permDef, members);
            }
        }
    }
    IPortletDefinition def = portletDefinitionDao.getPortletDefinitionByFname(fname);
    if (def == null) {
        def = new PortletDefinitionImpl(portletType, fname, portletRep.getName(), portletRep.getTitle(), portletDescriptor.getWebAppName(), portletDescriptor.getPortletName(), isFramework != null ? isFramework : false);
    } else {
        final IPortletDescriptorKey portletDescriptorKey = def.getPortletDescriptorKey();
        portletDescriptorKey.setPortletName(portletDescriptor.getPortletName());
        if (isFramework != null && isFramework) {
            portletDescriptorKey.setFrameworkPortlet(true);
            portletDescriptorKey.setWebAppName(null);
        } else {
            portletDescriptorKey.setFrameworkPortlet(false);
            portletDescriptorKey.setWebAppName(portletDescriptor.getWebAppName());
        }
        def.setName(portletRep.getName());
        def.setTitle(portletRep.getTitle());
        def.setType(portletType);
    }
    def.setDescription(portletRep.getDesc());
    final BigInteger timeout = portletRep.getTimeout();
    if (timeout != null) {
        def.setTimeout(timeout.intValue());
    }
    final BigInteger actionTimeout = portletRep.getActionTimeout();
    if (actionTimeout != null) {
        def.setActionTimeout(actionTimeout.intValue());
    }
    final BigInteger eventTimeout = portletRep.getEventTimeout();
    if (eventTimeout != null) {
        def.setEventTimeout(eventTimeout.intValue());
    }
    final BigInteger renderTimeout = portletRep.getRenderTimeout();
    if (renderTimeout != null) {
        def.setRenderTimeout(renderTimeout.intValue());
    }
    final BigInteger resourceTimeout = portletRep.getResourceTimeout();
    if (resourceTimeout != null) {
        def.setResourceTimeout(resourceTimeout.intValue());
    }
    handleLifecycleApproval(def, portletRep.getLifecycle());
    handleLifecyclePublished(def, portletRep.getLifecycle());
    handleLifecycleExpired(def, portletRep.getLifecycle());
    final Set<IPortletDefinitionParameter> parameters = new LinkedHashSet<IPortletDefinitionParameter>();
    for (ExternalPortletParameter param : portletRep.getParameters()) {
        parameters.add(new PortletDefinitionParameterImpl(param.getName(), param.getValue()));
    }
    def.setParameters(parameters);
    final ArrayList<IPortletPreference> preferenceList = new ArrayList<IPortletPreference>();
    for (ExternalPortletPreference pref : portletRep.getPortletPreferences()) {
        final List<String> valueList = pref.getValues();
        final String[] values = valueList.toArray(new String[valueList.size()]);
        final Boolean readOnly = pref.isReadOnly();
        preferenceList.add(new PortletPreferenceImpl(pref.getName(), readOnly != null ? readOnly : false, values));
    }
    def.setPortletPreferences(preferenceList);
    savePortletDefinition(def, systemUser, categories, permissions);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EntityIdentifier(org.apereo.portal.EntityIdentifier) IPortletDescriptorKey(org.apereo.portal.portlet.om.IPortletDescriptorKey) PortletDescriptor(org.apereo.portal.xml.PortletDescriptor) IPortletDefinitionParameter(org.apereo.portal.portlet.om.IPortletDefinitionParameter) ExternalPermissionDefinition(org.apereo.portal.io.xml.portlettype.ExternalPermissionDefinition) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl) PortletCategory(org.apereo.portal.portlet.om.PortletCategory) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) IGroupMember(org.apereo.portal.groups.IGroupMember) IPortletType(org.apereo.portal.portlet.om.IPortletType) BigInteger(java.math.BigInteger) PortletDefinitionImpl(org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl) PortletDefinitionParameterImpl(org.apereo.portal.portlet.dao.jpa.PortletDefinitionParameterImpl) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 IGroupMember (org.apereo.portal.groups.IGroupMember)2 PortletDefinitionImpl (org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl)2 PortletPreferenceImpl (org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)2 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)2 IPortletPreference (org.apereo.portal.portlet.om.IPortletPreference)2 IPortletType (org.apereo.portal.portlet.om.IPortletType)2 PortletCategory (org.apereo.portal.portlet.om.PortletCategory)2 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 EntityIdentifier (org.apereo.portal.EntityIdentifier)1 ExternalPermissionDefinition (org.apereo.portal.io.xml.portlettype.ExternalPermissionDefinition)1 JsonEntityBean (org.apereo.portal.layout.dlm.remoting.JsonEntityBean)1 PortletDefinitionParameterImpl (org.apereo.portal.portlet.dao.jpa.PortletDefinitionParameterImpl)1 IPortletDefinitionParameter (org.apereo.portal.portlet.om.IPortletDefinitionParameter)1 IPortletDescriptorKey (org.apereo.portal.portlet.om.IPortletDescriptorKey)1