Search in sources :

Example 11 with PathFilterSet

use of org.apache.jackrabbit.vault.fs.api.PathFilterSet in project acs-aem-commons by Adobe-Consulting-Services.

the class ACLPackagerServletImpl method findResources.

/**
 * Search the JCR for all rep:ACE nodes to be further filtered by Grant/Deny ACE rep:principalNames.
 *
 * @param resourceResolver ResourceResolver of initiating user
 * @param principalNames   Principal Names to filter rep:ACE nodes with; Only rep:ACE nodes with children
 *                         with rep:principalNames in this list will be returned
 * @return Set (ordered by path) of rep:ACE coverage who hold permissions for at least one Principal
 * enumerated in principleNames
 */
@SuppressWarnings("squid:S3776")
private List<PathFilterSet> findResources(final ResourceResolver resourceResolver, final List<String> principalNames, final List<Pattern> includePatterns) {
    boolean isOak = true;
    try {
        isOak = aemCapabilityHelper.isOak();
    } catch (RepositoryException e) {
        isOak = true;
    }
    final Set<Resource> resources = new TreeSet<Resource>(resourceComparator);
    final List<PathFilterSet> pathFilterSets = new ArrayList<PathFilterSet>();
    String[] queries = CQ5_QUERIES;
    if (isOak) {
        queries = AEM6_QUERIES;
    }
    for (final String query : queries) {
        final Iterator<Resource> hits = resourceResolver.findResources(query, QUERY_LANG);
        while (hits.hasNext()) {
            final Resource hit = hits.next();
            Resource repPolicy = null;
            if (isOak) {
                // If Oak, get the parent node since the query is for the Grant/Deny nodes
                if (hit.getParent() != null) {
                    repPolicy = hit.getParent();
                }
            } else {
                // If not Oak, then the rep:ACL is the hit
                repPolicy = hit;
            }
            if (this.isIncluded(repPolicy, includePatterns)) {
                log.debug("Included by pattern [ {} ]", repPolicy.getPath());
            } else {
                continue;
            }
            final Iterator<Resource> aces = repPolicy.listChildren();
            while (aces.hasNext()) {
                final Resource ace = aces.next();
                final ValueMap props = ace.adaptTo(ValueMap.class);
                final String repPrincipalName = props.get("rep:principalName", String.class);
                if (principalNames == null || principalNames.isEmpty() || principalNames.contains(repPrincipalName)) {
                    resources.add(repPolicy);
                    log.debug("Included by principal [ {} ]", repPolicy.getPath());
                    break;
                }
            }
        }
    }
    for (final Resource resource : resources) {
        pathFilterSets.add(new PathFilterSet(resource.getPath()));
    }
    log.debug("Found {} matching rep:policy resources.", pathFilterSets.size());
    return pathFilterSets;
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) TreeSet(java.util.TreeSet) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException)

Example 12 with PathFilterSet

use of org.apache.jackrabbit.vault.fs.api.PathFilterSet in project acs-aem-commons by Adobe-Consulting-Services.

the class ACLPackagerServletImpl method doPost.

@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
    log.trace("Preview mode: {}", preview);
    final ValueMap properties = this.getProperties(request);
    final String[] principalNames = properties.get(PRINCIPAL_NAMES, new String[] {});
    final List<PathFilterSet> packageResources = this.findResources(resourceResolver, Arrays.asList(principalNames), toPatterns(Arrays.asList(properties.get(INCLUDE_PATTERNS, new String[] {}))));
    try {
        // Add Principals
        if (properties.get(INCLUDE_PRINCIPALS, DEFAULT_INCLUDE_PRINCIPALS)) {
            packageResources.addAll(this.getPrincipalResources(resourceResolver, principalNames));
        }
        doPackaging(request, response, preview, properties, packageResources);
    } catch (RepositoryException ex) {
        log.error(ex.getMessage());
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (IOException ex) {
        log.error(ex.getMessage());
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (JSONException ex) {
        log.error(ex.getMessage());
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    }
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 13 with PathFilterSet

use of org.apache.jackrabbit.vault.fs.api.PathFilterSet in project acs-aem-commons by Adobe-Consulting-Services.

the class ACLPackagerServletImpl method getPrincipalResources.

/**
 * Gets the resources for the param principals.
 *
 * @param resourceResolver the ResourceResolver obj to get the principal resources;
 *                         Must have read access to the principal resources.
 * @param principalNames   the principals to get
 * @return a list of PathFilterSets covering the selectes principal names (if they exist)
 * @throws RepositoryException
 */
private List<PathFilterSet> getPrincipalResources(final ResourceResolver resourceResolver, final String[] principalNames) throws RepositoryException {
    final UserManager userManager = resourceResolver.adaptTo(UserManager.class);
    final List<PathFilterSet> pathFilterSets = new ArrayList<PathFilterSet>();
    for (final String principalName : principalNames) {
        final Authorizable authorizable = userManager.getAuthorizable(principalName);
        if (authorizable != null) {
            final Resource resource = resourceResolver.getResource(authorizable.getPath());
            if (resource != null) {
                final PathFilterSet principal = new PathFilterSet(resource.getPath());
                // Exclude tokens as they are not vlt installable in AEM6/Oak
                principal.addExclude(new DefaultPathFilter(resource.getPath() + "/\\.tokens"));
                pathFilterSets.add(principal);
            }
        }
    }
    return pathFilterSets;
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) DefaultPathFilter(org.apache.jackrabbit.vault.fs.filter.DefaultPathFilter)

Example 14 with PathFilterSet

use of org.apache.jackrabbit.vault.fs.api.PathFilterSet in project acs-aem-commons by Adobe-Consulting-Services.

the class AuthorizablePackagerServletImpl method doPost.

@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
    log.debug("Preview mode: {}", preview);
    final ValueMap properties = this.getProperties(request);
    try {
        final List<PathFilterSet> paths = this.findPaths(resourceResolver, properties.get("authorizableIds", new String[0]));
        doPackaging(request, response, preview, properties, paths);
    } catch (RepositoryException ex) {
        log.error("Repository error while creating Query Package", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (IOException ex) {
        log.error("IO error while creating Query Package", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (JSONException ex) {
        log.error("JSON error while creating Query Package response", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    }
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 15 with PathFilterSet

use of org.apache.jackrabbit.vault.fs.api.PathFilterSet in project acs-aem-commons by Adobe-Consulting-Services.

the class PackageHelperImpl method getPathFilterSetPreviewJSON.

/**
 * {@inheritDoc}
 */
public String getPathFilterSetPreviewJSON(final Collection<PathFilterSet> pathFilterSets) throws JSONException {
    final JSONObject json = new JSONObject();
    json.put(KEY_STATUS, "preview");
    json.put(KEY_PATH, "Not applicable (Preview)");
    json.put(KEY_FILTER_SETS, new JSONArray());
    for (final PathFilterSet pathFilterSet : pathFilterSets) {
        final JSONObject tmp = new JSONObject();
        tmp.put(KEY_IMPORT_MODE, "Not applicable (Preview)");
        tmp.put(KEY_ROOT_PATH, pathFilterSet.getRoot());
        json.accumulate("filterSets", tmp);
    }
    return json.toString();
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) JSONArray(org.apache.sling.commons.json.JSONArray)

Aggregations

PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)18 ArrayList (java.util.ArrayList)7 RepositoryException (javax.jcr.RepositoryException)4 WorkspaceFilter (org.apache.jackrabbit.vault.fs.api.WorkspaceFilter)4 DefaultWorkspaceFilter (org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter)4 Test (org.junit.Test)4 DefaultPathFilter (org.apache.jackrabbit.vault.fs.filter.DefaultPathFilter)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ValueMap (org.apache.sling.api.resource.ValueMap)3 JSONArray (org.apache.sling.commons.json.JSONArray)3 JSONObject (org.apache.sling.commons.json.JSONObject)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)2 UserManager (org.apache.jackrabbit.api.security.user.UserManager)2 JcrPackage (org.apache.jackrabbit.vault.packaging.JcrPackage)2 Resource (org.apache.sling.api.resource.Resource)2 JSONException (org.apache.sling.commons.json.JSONException)2