@Test public void testExisting() throws RepositoryException { CreateUserOperation createUserOperation = new CreateUserOperation(); PostResponse response = new JSONResponse(); context.currentResource("/home/users"); context.request().setParameterMap( ImmutableMap.<String, Object>builder().put(SlingPostConstants.RP_NODE_NAME, "test5").build()); UserManager userManager = CommonUtils.getUserManager(context.resourceResolver()); Mockito.when(userManager.getAuthorizable(Mockito.any(Principal.class))).thenReturn(Mockito.mock(Group.class)); createUserOperation.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 { String name = request.getParameter(SlingPostConstants.RP_NODE_NAME); String password = request.getParameter(PN_PASSWORD); ResourceResolver resolver = request.getResourceResolver(); UserManager userManager = CommonUtils.getUserManager(resolver); if (userManager.getAuthorizable(new SimplePrincipal(name)) != null) { throw new RepositoryException("Authorizable with id " + name + " already exists"); } String intermediatePath = StringUtils.substringBeforeLast(request.getResource().getPath(), "/") .replaceAll("\\/home\\/users\\/?", ""); User user = userManager.createUser(name, password, new SimplePrincipal(name), intermediatePath); if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.setPath(user.getPath()); response.onCreated(user.getPath()); } catch (Exception e) { log.warn("Failed to create user", e); response.setError(e); } }
CreateUserOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { String name = request.getParameter(SlingPostConstants.RP_NODE_NAME); String password = request.getParameter(PN_PASSWORD); ResourceResolver resolver = request.getResourceResolver(); UserManager userManager = CommonUtils.getUserManager(resolver); if (userManager.getAuthorizable(new SimplePrincipal(name)) != null) { throw new RepositoryException("Authorizable with id " + name + " already exists"); } String intermediatePath = StringUtils.substringBeforeLast(request.getResource().getPath(), "/") .replaceAll("\\/home\\/users\\/?", ""); User user = userManager.createUser(name, password, new SimplePrincipal(name), intermediatePath); if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.setPath(user.getPath()); response.onCreated(user.getPath()); } catch (Exception e) { log.warn("Failed to create user", e); response.setError(e); } } }
CreateUserOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { String name = request.getParameter(SlingPostConstants.RP_NODE_NAME); String password = request.getParameter(PN_PASSWORD); ResourceResolver resolver = request.getResourceResolver(); UserManager userManager = CommonUtils.getUserManager(resolver); if (userManager.getAuthorizable(new SimplePrincipal(name)) != null) { throw new RepositoryException("Authorizable with id " + name + " already exists"); } String intermediatePath = StringUtils.substringBeforeLast(request.getResource().getPath(), "/") .replaceAll("\\/home\\/users\\/?", ""); User user = userManager.createUser(name, password, new SimplePrincipal(name), intermediatePath); if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.setPath(user.getPath()); response.onCreated(user.getPath()); } catch (Exception e) { log.warn("Failed to create user", e); response.setError(e); } }  }
CreateUserOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { String name = request.getParameter(SlingPostConstants.RP_NODE_NAME); String password = request.getParameter(PN_PASSWORD); ResourceResolver resolver = request.getResourceResolver(); UserManager userManager = CommonUtils.getUserManager(resolver); if (userManager.getAuthorizable(new SimplePrincipal(name)) != null) { throw new RepositoryException("Authorizable with id " + name + " already exists"); } String intermediatePath = StringUtils.substringBeforeLast(request.getResource().getPath(), "/") .replaceAll("\\/home\\/users\\/?", ""); User user = userManager.createUser(name, password, new SimplePrincipal(name), intermediatePath); if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.setPath(user.getPath()); response.onCreated(user.getPath()); } catch (Exception e) { log.warn("Failed to create user", e); response.setError(e); } }  @Override void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors); }
CreateUserOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { String name = request.getParameter(SlingPostConstants.RP_NODE_NAME); String password = request.getParameter(PN_PASSWORD); ResourceResolver resolver = request.getResourceResolver(); UserManager userManager = CommonUtils.getUserManager(resolver); if (userManager.getAuthorizable(new SimplePrincipal(name)) != null) { throw new RepositoryException("Authorizable with id " + name + " already exists"); } String intermediatePath = StringUtils.substringBeforeLast(request.getResource().getPath(), "/") .replaceAll("\\/home\\/users\\/?", ""); User user = userManager.createUser(name, password, new SimplePrincipal(name), intermediatePath); if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.setPath(user.getPath()); response.onCreated(user.getPath()); } catch (Exception e) { log.warn("Failed to create user", e); response.setError(e); } }  @Override void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors); @SuppressWarnings("squid:S2068") // Just the parameter key, not the value
    static final String PN_PASSWORD; }