Search in sources :

Example 1 with PortletPreferenceImpl

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

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

the class AbstractPortletPreferencesImplTest method addPref.

protected void addPref(Map<String, IPortletPreference> prefs, String name, boolean readOnly, String[] values) {
    final PortletPreferenceImpl preference = new PortletPreferenceImpl(name, readOnly);
    preference.setValues(values);
    prefs.put(name, preference);
}
Also used : PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)

Example 3 with PortletPreferenceImpl

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

the class PortletEntityRegistryImplTest method testPersistentRemovePrefs.

//persistent with no prefs & in db - delete & create interim
@Test
public void testPersistentRemovePrefs() throws Exception {
    final IPortletDefinitionId portletDefId = this.createDefaultPorltetDefinition();
    final String nodeId = "u1l1n1";
    //Mock setup
    final MockHttpServletRequest request = new MockHttpServletRequest();
    when(portalRequestUtils.getOriginalPortalRequest(request)).thenReturn(request);
    when(portalRequestUtils.getOriginalPortletOrPortalRequest(request)).thenReturn(request);
    when(userInstanceManager.getUserInstance(request)).thenReturn(userInstance);
    when(userInstance.getPreferencesManager()).thenReturn(preferencesManager);
    when(userInstance.getPerson()).thenReturn(person);
    when(preferencesManager.getUserLayoutManager()).thenReturn(userLayoutManager);
    when(userLayoutManager.getNode(nodeId)).thenReturn(node);
    when(node.getType()).thenReturn(LayoutNodeType.PORTLET);
    when(node.getChannelPublishId()).thenReturn(portletDefId.getStringId());
    final IPortletEntityId portletEntityId = this.execute(new Callable<IPortletEntityId>() {

        @Override
        public IPortletEntityId call() throws Exception {
            //Create the entity
            IPortletEntity portletEntity = portletEntityRegistry.getOrCreatePortletEntity(request, portletDefId, nodeId, 12);
            assertEquals(SessionPortletEntityImpl.class, portletEntity.getClass());
            return portletEntity.getPortletEntityId();
        }
    });
    this.execute(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final IPortletEntity portletEntity = portletEntityRegistry.getPortletEntity(request, portletEntityId);
            //Add a preference
            final List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
            final IPortletPreference portletPreference = new PortletPreferenceImpl("pref", false, "value");
            preferences.add(portletPreference);
            //Store the entity
            portletEntityRegistry.storePortletEntity(request, portletEntity);
            return null;
        }
    });
    this.execute(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            //Verify it was converted from interim to persistent
            final IPortletEntity portletEntity = portletEntityRegistry.getPortletEntity(request, portletEntityId);
            assertEquals(PersistentPortletEntityWrapper.class, portletEntity.getClass());
            final List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
            assertEquals(1, preferences.size());
            //remove all preferences
            preferences.clear();
            //Store the entity
            portletEntityRegistry.storePortletEntity(request, portletEntity);
            return null;
        }
    });
    this.execute(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            //Verify it switched from persistent to interim
            final IPortletEntity portletEntity = portletEntityRegistry.getPortletEntity(request, portletEntityId);
            assertEquals(SessionPortletEntityImpl.class, portletEntity.getClass());
            final List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
            assertEquals(0, preferences.size());
            return null;
        }
    });
}
Also used : IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) List(java.util.List) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 4 with PortletPreferenceImpl

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

the class PortletEntityRegistryImplTest method testInterimNoPrefsAlreadyPersistent.

//interim with no prefs & in db - delete db version
@Test
public void testInterimNoPrefsAlreadyPersistent() throws Throwable {
    final IPortletDefinitionId portDefId1 = this.createDefaultPorltetDefinition();
    final String nodeId = "u1l1n1";
    //Mock setup
    final MockHttpServletRequest request = new MockHttpServletRequest();
    when(portalRequestUtils.getOriginalPortalRequest(request)).thenReturn(request);
    when(portalRequestUtils.getOriginalPortletOrPortalRequest(request)).thenReturn(request);
    when(userInstanceManager.getUserInstance(request)).thenReturn(userInstance);
    when(userInstance.getPreferencesManager()).thenReturn(preferencesManager);
    when(userInstance.getPerson()).thenReturn(person);
    when(preferencesManager.getUserLayoutManager()).thenReturn(userLayoutManager);
    when(userLayoutManager.getNode(nodeId)).thenReturn(node);
    when(node.getType()).thenReturn(LayoutNodeType.PORTLET);
    when(node.getChannelPublishId()).thenReturn(portDefId1.getStringId());
    final IPortletEntityId portletEntityId = this.execute(new Callable<IPortletEntityId>() {

        @Override
        public IPortletEntityId call() throws Exception {
            //T1 - Create the entity
            final IPortletEntity portletEntity = portletEntityRegistry.getOrCreatePortletEntity(request, portDefId1, nodeId, 12);
            ;
            assertEquals(SessionPortletEntityImpl.class, portletEntity.getClass());
            //T2 - get the entity and add preferences
            final IPortletEntityId portletEntityId = executeInThread("T2.1", new Callable<IPortletEntityId>() {

                @Override
                public IPortletEntityId call() throws Exception {
                    //T2 - Get entity
                    final IPortletEntity localPortletEntity = portletEntityRegistry.getPortletEntity(request, portletEntity.getPortletEntityId().getStringId());
                    assertEquals(portletEntity, localPortletEntity);
                    //T2 - Add a preference
                    final List<IPortletPreference> preferences = localPortletEntity.getPortletPreferences();
                    final IPortletPreference portletPreference = new PortletPreferenceImpl("pref2", false, "value");
                    preferences.add(portletPreference);
                    //T2 - Store the entity
                    portletEntityRegistry.storePortletEntity(request, localPortletEntity);
                    return localPortletEntity.getPortletEntityId();
                }
            });
            //T2 - verify entity was made persistent
            executeInThread("T2.2", new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    //T2 - Verify it was converted from interim to persistent
                    IPortletEntity portletEntity = portletEntityRegistry.getPortletEntity(request, portletEntityId);
                    assertEquals(PersistentPortletEntityWrapper.class, portletEntity.getClass());
                    List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
                    assertEquals(1, preferences.size());
                    return null;
                }
            });
            //T1 - clear preferences
            final List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
            preferences.clear();
            //T1 - Store the entity
            portletEntityRegistry.storePortletEntity(request, portletEntity);
            return portletEntity.getPortletEntityId();
        }
    });
    this.execute(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            //T1 - Verify it was converted from interim to persistent
            final IPortletEntity portletEntity = portletEntityRegistry.getPortletEntity(request, portletEntityId);
            assertEquals(SessionPortletEntityImpl.class, portletEntity.getClass());
            final List<IPortletPreference> preferences = portletEntity.getPortletPreferences();
            assertEquals(0, preferences.size());
            return null;
        }
    });
}
Also used : IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Callable(java.util.concurrent.Callable) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) List(java.util.List) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 5 with PortletPreferenceImpl

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

the class GuestPortletEntityPreferencesImpl method loadBasePortletPreferences.

@Override
protected void loadBasePortletPreferences(IPortletEntity portletEntity, Map<String, IPortletPreference> basePortletPreferences) {
    final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
    //Add descriptor prefs to base Map
    final IPortletDefinitionId portletDefinitionId = portletDefinition.getPortletDefinitionId();
    final PortletDefinition portletDescriptor = this.portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId);
    final Preferences descriptorPreferences = portletDescriptor.getPortletPreferences();
    for (final Preference preference : descriptorPreferences.getPortletPreferences()) {
        final IPortletPreference preferenceWrapper = new PortletPreferenceImpl(preference);
        basePortletPreferences.put(preferenceWrapper.getName(), preferenceWrapper);
    }
    //Add definition prefs to base Map
    final List<IPortletPreference> definitionPreferences = portletDefinition.getPortletPreferences();
    for (final IPortletPreference preference : definitionPreferences) {
        basePortletPreferences.put(preference.getName(), preference);
    }
    //Add entity prefs to base Map
    final List<IPortletPreference> entityPreferences = portletEntity.getPortletPreferences();
    for (final IPortletPreference preference : entityPreferences) {
        basePortletPreferences.put(preference.getName(), preference);
    }
}
Also used : IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) Preference(org.apache.pluto.container.om.portlet.Preference) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) Preferences(org.apache.pluto.container.om.portlet.Preferences) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) PortletDefinition(org.apache.pluto.container.om.portlet.PortletDefinition)

Aggregations

PortletPreferenceImpl (org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)16 IPortletPreference (org.apereo.portal.portlet.om.IPortletPreference)15 IPortletDefinitionId (org.apereo.portal.portlet.om.IPortletDefinitionId)11 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)9 List (java.util.List)7 IPortletEntityId (org.apereo.portal.portlet.om.IPortletEntityId)7 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)7 Test (org.junit.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)6 Callable (java.util.concurrent.Callable)4 ArrayList (java.util.ArrayList)3 PortletDefinition (org.apache.pluto.container.om.portlet.PortletDefinition)3 Preference (org.apache.pluto.container.om.portlet.Preference)3 Preferences (org.apache.pluto.container.om.portlet.Preferences)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 IGroupMember (org.apereo.portal.groups.IGroupMember)2 PortletDefinitionImpl (org.apereo.portal.portlet.dao.jpa.PortletDefinitionImpl)2 IPortletType (org.apereo.portal.portlet.om.IPortletType)2