Search in sources :

Example 66 with ResourceResolver

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

the class XSSAPIImplTest method setup.

@Before
public void setup() {
    try {
        XSSFilterImpl xssFilter = new XSSFilterImpl();
        setDefaultHandler(xssFilter, "./src/main/resources/SLING-INF/content/config.xml");
        xssAPI = new XSSAPIImpl();
        Whitebox.invokeMethod(xssAPI, "activate");
        Field filterField = XSSAPIImpl.class.getDeclaredField("xssFilter");
        filterField.setAccessible(true);
        filterField.set(xssAPI, xssFilter);
        ResourceResolver mockResolver = mock(ResourceResolver.class);
        when(mockResolver.map(anyString())).thenAnswer(new Answer() {

            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                String url = (String) args[0];
                return url.replaceAll("jcr:", "_jcr_");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Field(java.lang.reflect.Field) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Matchers.anyString(org.mockito.Matchers.anyString) Before(org.junit.Before)

Example 67 with ResourceResolver

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

the class UpdateGroupServlet method updateGroup.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.UpdateGroup#updateGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
     */
public Group updateGroup(Session jcrSession, String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
    Group group = null;
    UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
    Authorizable authorizable = userManager.getAuthorizable(name);
    if (authorizable instanceof Group) {
        group = (Group) authorizable;
    } else {
        throw new ResourceNotFoundException("Group to update could not be determined");
    }
    String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
    Collection<RequestProperty> reqProperties = collectContent(properties);
    try {
        // cleanup any old content (@Delete parameters)
        processDeletes(group, reqProperties, changes);
        // write content from form
        writeContent(jcrSession, group, reqProperties, changes);
        // update the group memberships
        ResourceResolver resourceResolver = null;
        try {
            //create a resource resolver to resolve the relative paths used for group membership values
            final Map<String, Object> authInfo = new HashMap<String, Object>();
            authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
            resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
            Resource baseResource = resourceResolver.getResource(groupPath);
            updateGroupMembership(baseResource, properties, group, changes);
        } catch (LoginException e) {
            throw new RepositoryException(e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    } catch (RepositoryException re) {
        throw new RepositoryException("Failed to update group.", re);
    }
    return group;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) UpdateGroup(org.apache.sling.jackrabbit.usermanager.UpdateGroup) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) LoginException(org.apache.sling.api.resource.LoginException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 68 with ResourceResolver

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

the class AuthorizableResourceProvider method listChildren.

@Override
public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
    try {
        String path = parent.getPath();
        ResourceResolver resourceResolver = parent.getResourceResolver();
        // handle children of /system/userManager
        if (SYSTEM_USER_MANAGER_PATH.equals(path)) {
            List<Resource> resources = new ArrayList<Resource>();
            if (resourceResolver != null) {
                resources.add(getResource(ctx, SYSTEM_USER_MANAGER_USER_PATH, null, null));
                resources.add(getResource(ctx, SYSTEM_USER_MANAGER_GROUP_PATH, null, null));
            }
            return resources.iterator();
        }
        int searchType = -1;
        if (SYSTEM_USER_MANAGER_USER_PATH.equals(path)) {
            searchType = PrincipalManager.SEARCH_TYPE_NOT_GROUP;
        } else if (SYSTEM_USER_MANAGER_GROUP_PATH.equals(path)) {
            searchType = PrincipalManager.SEARCH_TYPE_GROUP;
        }
        if (searchType != -1) {
            PrincipalIterator principals = null;
            Session session = resourceResolver.adaptTo(Session.class);
            if (session != null) {
                PrincipalManager principalManager = AccessControlUtil.getPrincipalManager(session);
                principals = principalManager.getPrincipals(searchType);
            }
            if (principals != null) {
                return new ChildrenIterator(parent, principals);
            }
        }
    } catch (RepositoryException re) {
        throw new SlingException("Error listing children of resource: " + parent.getPath(), re);
    }
    return null;
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) PrincipalIterator(org.apache.jackrabbit.api.security.principal.PrincipalIterator) SlingException(org.apache.sling.api.SlingException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 69 with ResourceResolver

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

the class CreateGroupServlet method createGroup.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.CreateGroup#createGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
     */
public Group createGroup(Session jcrSession, final String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
    // check that the parameter values have valid values.
    if (jcrSession == null) {
        throw new IllegalArgumentException("JCR Session not found");
    }
    if (name == null || name.length() == 0) {
        throw new IllegalArgumentException("Group name was not supplied");
    }
    UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
    Authorizable authorizable = userManager.getAuthorizable(name);
    Group group = null;
    if (authorizable != null) {
        // principal already exists!
        throw new RepositoryException("A group already exists with the requested name: " + name);
    } else {
        group = userManager.createGroup(new Principal() {

            public String getName() {
                return name;
            }
        });
        String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
        Collection<RequestProperty> reqProperties = collectContent(properties);
        changes.add(Modification.onCreated(groupPath));
        // write content from form
        writeContent(jcrSession, group, reqProperties, changes);
        // update the group memberships
        ResourceResolver resourceResolver = null;
        try {
            //create a resource resolver to resolve the relative paths used for group membership values
            final Map<String, Object> authInfo = new HashMap<String, Object>();
            authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
            resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
            Resource baseResource = resourceResolver.getResource(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PATH);
            updateGroupMembership(baseResource, properties, group, changes);
        } catch (LoginException e) {
            throw new RepositoryException(e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    }
    return group;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) CreateGroup(org.apache.sling.jackrabbit.usermanager.CreateGroup) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) LoginException(org.apache.sling.api.resource.LoginException) Principal(java.security.Principal)

Example 70 with ResourceResolver

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

the class ResourceTypeResolutionIT method checkResourceType.

@Test
public void checkResourceType() throws Exception {
    JackrabbitSession adminSession = (JackrabbitSession) slingRepository.loginAdministrative(null);
    Node contentBar = JcrUtils.getOrCreateByPath("/content/foo/bar", "nt:unstructured", adminSession);
    contentBar.setProperty("sling:resourceType", "types/foo/bar");
    Node appsBar = JcrUtils.getOrCreateByPath("/apps/types/foo/bar", "nt:unstructured", adminSession);
    appsBar.setProperty("sling:resourceSuperType", "types/foo/parent");
    JcrUtils.getOrCreateByPath("/apps/types/foo/parent", "nt:unstructured", adminSession);
    adminSession.getUserManager().createUser("test-user", "test");
    adminSession.save();
    AccessControlUtils.allow(contentBar, "test-user", "jcr:read");
    adminSession.save();
    adminSession.logout();
    HashMap<String, Object> authenticationInfo = new HashMap<>();
    authenticationInfo.put(ResourceResolverFactory.USER, "test-user");
    authenticationInfo.put(ResourceResolverFactory.PASSWORD, "test".toCharArray());
    ResourceResolver testResolver = resourceResolverFactory.getResourceResolver(authenticationInfo);
    try {
        Resource resource = testResolver.getResource("/content/foo/bar");
        assertNotNull(resource);
        assertEquals("/content/foo/bar", resource.getPath());
        assertTrue(resource.isResourceType("types/foo/bar"));
        // this assertion causes the private ResourceResolverControl#getResourceTypeResourceResolver
        // to be called, which needs to inject the resourceresolver bundle via the authenticationInfo
        // see SLING-6329
        assertTrue(resource.isResourceType("types/foo/parent"));
    } finally {
        testResolver.close();
    }
}
Also used : HashMap(java.util.HashMap) Node(javax.jcr.Node) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15