Search in sources :

Example 26 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormStructureHelperImplTest method testUpdateformStructure.

@Test
public void testUpdateformStructure() {
    Resource resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid/container");
    formStructureHelper.updateFormStructure(resource);
    ValueMap properties = resource.adaptTo(ValueMap.class);
    assertEquals("foundation/components/form/actions/store", properties.get("actionType", String.class));
    String action = properties.get("action", String.class);
    assertNotNull(action);
    assertTrue(action.length() > 0);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 27 with Resource

use of org.apache.sling.api.resource.Resource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormStructureHelperImplTest method testcanManage.

@Test
public void testcanManage() {
    Resource resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid/start");
    assertFalse(formStructureHelper.canManage(resource));
    resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid/container/text");
    assertTrue(formStructureHelper.canManage(resource));
    resource = resourceResolver.getResource(CONTAINING_PAGE + "/jcr:content/root/responsivegrid/container");
    assertTrue(formStructureHelper.canManage(resource));
}
Also used : Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 28 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class JcrNodeResourceTest method testResourceSuperType.

public void testResourceSuperType() throws Exception {
    String name = "resourceSuperType";
    String typeNodeName = "some_resource_type";
    String typeName = rootPath + "/" + typeNodeName;
    Node node = rootNode.addNode(name, JcrConstants.NT_UNSTRUCTURED);
    node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, typeName);
    getSession().save();
    Resource jnr = new JcrNodeResource(null, node.getPath(), null, node, getHelperData());
    assertEquals(typeName, jnr.getResourceType());
    // default super type is null
    assertNull(jnr.getResourceSuperType());
    String superTypeName = "supertype";
    Node typeNode = rootNode.addNode(typeNodeName, JcrConstants.NT_UNSTRUCTURED);
    typeNode.setProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY, superTypeName);
    getSession().save();
    jnr = new JcrNodeResource(null, typeNode.getPath(), null, typeNode, getHelperData());
    assertEquals(JcrConstants.NT_UNSTRUCTURED, jnr.getResourceType());
    assertEquals(superTypeName, jnr.getResourceSuperType());
    // overwrite super type with direct property
    String otherSuperTypeName = "othersupertype";
    node.setProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY, otherSuperTypeName);
    getSession().save();
    jnr = new JcrNodeResource(null, node.getPath(), null, node, getHelperData());
    assertEquals(typeName, jnr.getResourceType());
    assertEquals(otherSuperTypeName, jnr.getResourceSuperType());
    // remove direct property to get supertype again
    node.getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY).remove();
    getSession().save();
    jnr = new JcrNodeResource(null, node.getPath(), null, node, getHelperData());
    assertEquals(typeName, jnr.getResourceType());
    assertNull(jnr.getResourceSuperType());
}
Also used : Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource)

Example 29 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class ResourceAccessSecurityImpl method getReadableResource.

@Override
public Resource getReadableResource(final Resource resource) {
    Resource returnValue = null;
    final Iterator<ResourceAccessGateHandler> accessGateHandlers = getMatchingResourceAccessGateHandlerIterator(resource.getPath(), ResourceAccessGate.Operation.READ);
    GateResult finalGateResult = null;
    List<ResourceAccessGate> accessGatesForReadValues = null;
    boolean canReadAllValues = false;
    if (accessGateHandlers != null) {
        boolean noGateMatched = true;
        while (accessGateHandlers.hasNext()) {
            noGateMatched = false;
            final ResourceAccessGateHandler resourceAccessGateHandler = accessGateHandlers.next();
            final GateResult gateResult = !resourceAccessGateHandler.getResourceAccessGate().hasReadRestrictions(resource.getResourceResolver()) ? GateResult.GRANTED : resourceAccessGateHandler.getResourceAccessGate().canRead(resource);
            if (!canReadAllValues && gateResult == GateResult.GRANTED) {
                if (resourceAccessGateHandler.getResourceAccessGate().canReadAllValues(resource)) {
                    canReadAllValues = true;
                    accessGatesForReadValues = null;
                } else {
                    if (accessGatesForReadValues == null) {
                        accessGatesForReadValues = new ArrayList<ResourceAccessGate>();
                    }
                    accessGatesForReadValues.add(resourceAccessGateHandler.getResourceAccessGate());
                }
            }
            if (finalGateResult == null) {
                finalGateResult = gateResult;
            } else if (finalGateResult != GateResult.GRANTED && gateResult != GateResult.CANT_DECIDE) {
                finalGateResult = gateResult;
            }
            // stop checking if the operation is final and the result not GateResult.CANT_DECIDE
            if (gateResult != GateResult.CANT_DECIDE && resourceAccessGateHandler.isFinalOperation(ResourceAccessGate.Operation.READ)) {
                break;
            }
        }
        // return null if access is denied or no ResourceAccessGate is present
        if (finalGateResult == GateResult.DENIED) {
            returnValue = null;
        } else if (finalGateResult == GateResult.GRANTED) {
            returnValue = resource;
        } else if (noGateMatched && this.defaultAllowIfNoGateMatches) {
            returnValue = resource;
        }
    }
    boolean canUpdateResource = canUpdate(resource);
    // wrap Resource if read access is not or partly (values) not granted
    if (returnValue != null) {
        if (!canReadAllValues || !canUpdateResource) {
            returnValue = new AccessGateResourceWrapper(returnValue, accessGatesForReadValues, canUpdateResource);
        }
    }
    return returnValue;
}
Also used : GateResult(org.apache.sling.resourceaccesssecurity.ResourceAccessGate.GateResult) Resource(org.apache.sling.api.resource.Resource) ResourceAccessGate(org.apache.sling.resourceaccesssecurity.ResourceAccessGate)

Example 30 with Resource

use of org.apache.sling.api.resource.Resource in project sling by apache.

the class ResourceAccessSecurityImplTests method testCanUpdateUsingReadableResource.

@Test
public void testCanUpdateUsingReadableResource() {
    // one needs to have also read rights to obtain the resource
    initMocks("/content", new String[] { "read", "update" });
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn("/content");
    ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
    when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);
    when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
    Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
    ModifiableValueMap resultValueMap = readableResource.adaptTo(ModifiableValueMap.class);
    resultValueMap.put("modified", "value");
    verify(valueMap, times(1)).put("modified", "value");
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1569 Test (org.junit.Test)695 ValueMap (org.apache.sling.api.resource.ValueMap)338 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)260 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)164 HashMap (java.util.HashMap)160 Node (javax.jcr.Node)151 ArrayList (java.util.ArrayList)137 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)119 PersistenceException (org.apache.sling.api.resource.PersistenceException)114 Test (org.junit.jupiter.api.Test)72 Map (java.util.Map)70 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 LoginException (org.apache.sling.api.resource.LoginException)58 RepositoryException (javax.jcr.RepositoryException)54 Calendar (java.util.Calendar)50 Session (javax.jcr.Session)49