@Test public void testInvalidPath() throws RepositoryException { MembershipOperation membersOperation = new MembershipOperation(); PostResponse response = new JSONResponse(); context.currentResource("/content"); context.request().setParameterMap(ImmutableMap.<String, Object>builder() .put(":members", new String[] { "/home/groups/sling-cms/authors" }).build()); membersOperation.run(context.request(), response, null); assertNotNull(response.getError()); }
@Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> groups = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERSHIP)) .ifPresent(p -> groups.addAll(Arrays.asList(p))); AuthorizableWrapper authWrapper = request.getResource().adaptTo(AuthorizableWrapper.class); Authorizable auth = authWrapper.getAuthorizable(); response.setPath(auth.getPath()); changes.add(Modification.onModified(auth.getPath())); auth.declaredMemberOf().forEachRemaining(group -> { try { if (!groups.contains(group.getPath())) { log.debug("Removing member {} from {}", auth, group); group.removeMember(auth); changes.add(Modification.onModified(group.getPath())); } else { groups.remove(group.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : groups) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Group group = (Group) resource.adaptTo(AuthorizableWrapper.class).getAuthorizable(); group.addMember(auth); changes.add(Modification.onModified(group.getPath())); log.debug("Adding member {} to {}", auth, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(auth.getPath()); } catch (Exception e) { log.warn("Failed to update membership", e); response.setError(e); } }
MembershipOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> groups = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERSHIP)) .ifPresent(p -> groups.addAll(Arrays.asList(p))); AuthorizableWrapper authWrapper = request.getResource().adaptTo(AuthorizableWrapper.class); Authorizable auth = authWrapper.getAuthorizable(); response.setPath(auth.getPath()); changes.add(Modification.onModified(auth.getPath())); auth.declaredMemberOf().forEachRemaining(group -> { try { if (!groups.contains(group.getPath())) { log.debug("Removing member {} from {}", auth, group); group.removeMember(auth); changes.add(Modification.onModified(group.getPath())); } else { groups.remove(group.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : groups) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Group group = (Group) resource.adaptTo(AuthorizableWrapper.class).getAuthorizable(); group.addMember(auth); changes.add(Modification.onModified(group.getPath())); log.debug("Adding member {} to {}", auth, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(auth.getPath()); } catch (Exception e) { log.warn("Failed to update membership", e); response.setError(e); } } }
MembershipOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> groups = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERSHIP)) .ifPresent(p -> groups.addAll(Arrays.asList(p))); AuthorizableWrapper authWrapper = request.getResource().adaptTo(AuthorizableWrapper.class); Authorizable auth = authWrapper.getAuthorizable(); response.setPath(auth.getPath()); changes.add(Modification.onModified(auth.getPath())); auth.declaredMemberOf().forEachRemaining(group -> { try { if (!groups.contains(group.getPath())) { log.debug("Removing member {} from {}", auth, group); group.removeMember(auth); changes.add(Modification.onModified(group.getPath())); } else { groups.remove(group.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : groups) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Group group = (Group) resource.adaptTo(AuthorizableWrapper.class).getAuthorizable(); group.addMember(auth); changes.add(Modification.onModified(group.getPath())); log.debug("Adding member {} to {}", auth, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(auth.getPath()); } catch (Exception e) { log.warn("Failed to update membership", e); response.setError(e); } }  }
MembershipOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> groups = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERSHIP)) .ifPresent(p -> groups.addAll(Arrays.asList(p))); AuthorizableWrapper authWrapper = request.getResource().adaptTo(AuthorizableWrapper.class); Authorizable auth = authWrapper.getAuthorizable(); response.setPath(auth.getPath()); changes.add(Modification.onModified(auth.getPath())); auth.declaredMemberOf().forEachRemaining(group -> { try { if (!groups.contains(group.getPath())) { log.debug("Removing member {} from {}", auth, group); group.removeMember(auth); changes.add(Modification.onModified(group.getPath())); } else { groups.remove(group.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : groups) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Group group = (Group) resource.adaptTo(AuthorizableWrapper.class).getAuthorizable(); group.addMember(auth); changes.add(Modification.onModified(group.getPath())); log.debug("Adding member {} to {}", auth, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(auth.getPath()); } catch (Exception e) { log.warn("Failed to update membership", e); response.setError(e); } }  @Override void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors); }
MembershipOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> groups = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERSHIP)) .ifPresent(p -> groups.addAll(Arrays.asList(p))); AuthorizableWrapper authWrapper = request.getResource().adaptTo(AuthorizableWrapper.class); Authorizable auth = authWrapper.getAuthorizable(); response.setPath(auth.getPath()); changes.add(Modification.onModified(auth.getPath())); auth.declaredMemberOf().forEachRemaining(group -> { try { if (!groups.contains(group.getPath())) { log.debug("Removing member {} from {}", auth, group); group.removeMember(auth); changes.add(Modification.onModified(group.getPath())); } else { groups.remove(group.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : groups) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Group group = (Group) resource.adaptTo(AuthorizableWrapper.class).getAuthorizable(); group.addMember(auth); changes.add(Modification.onModified(group.getPath())); log.debug("Adding member {} to {}", auth, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(auth.getPath()); } catch (Exception e) { log.warn("Failed to update membership", e); response.setError(e); } }  @Override void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors); static final String PN_MEMBERSHIP; }