UserInitializer implements WorkspaceInitializer, UserConstants, QueryIndexProviderAware { @Override public void initialize(NodeBuilder builder, String workspaceName) { NodeState base = squeeze(builder.getNodeState()); MemoryNodeStore store = new MemoryNodeStore(base); Root root = RootFactory.createSystemRoot(store, EmptyHook.INSTANCE, workspaceName, securityProvider, queryIndexProvider); UserConfiguration userConfiguration = securityProvider.getConfiguration(UserConfiguration.class); UserManager userManager = userConfiguration.getUserManager(root, NamePathMapper.DEFAULT); String errorMsg = "Failed to initialize user content."; try { Tree rootTree = root.getTree(PathUtils.ROOT_PATH); checkState(rootTree.exists()); Tree index = TreeUtil.getOrAddChild(rootTree, IndexConstants.INDEX_DEFINITIONS_NAME, JcrConstants.NT_UNSTRUCTURED); if (!index.hasChild("authorizableId")) { Tree authorizableId = IndexUtils.createIndexDefinition(index, "authorizableId", true, new String[]{REP_AUTHORIZABLE_ID}, NT_REP_AUTHORIZABLE); authorizableId.setProperty("info", "Oak index used by the user management " + "to enforce uniqueness of rep:authorizableId property values."); } if (!index.hasChild("principalName")) { Tree principalName = IndexUtils.createIndexDefinition(index, "principalName", true, new String[]{REP_PRINCIPAL_NAME}, NT_REP_AUTHORIZABLE); principalName.setProperty("info", "Oak index used by the user management " + "to enforce uniqueness of rep:principalName property values, " + "and to quickly search a principal by name if it was constructed manually."); } if (!index.hasChild("repMembers")) { Tree members = IndexUtils.createIndexDefinition(index, "repMembers", false, new String[]{REP_MEMBERS}, NT_REP_MEMBER_REFERENCES); members.setProperty("info", "Oak index used by the user management to lookup group membership."); } ConfigurationParameters params = userConfiguration.getParameters(); String adminId = params.getConfigValue(PARAM_ADMIN_ID, DEFAULT_ADMIN_ID); if (userManager.getAuthorizable(adminId) == null) { boolean omitPw = params.getConfigValue(PARAM_OMIT_ADMIN_PW, false); userManager.createUser(adminId, (omitPw) ? null : adminId); } String anonymousId = Strings.emptyToNull(params.getConfigValue(PARAM_ANONYMOUS_ID, DEFAULT_ANONYMOUS_ID, String.class)); if (anonymousId != null && userManager.getAuthorizable(anonymousId) == null) { userManager.createUser(anonymousId, null); } if (root.hasPendingChanges()) { root.commit(); } } catch (RepositoryException | CommitFailedException e) { log.error(errorMsg, e); throw new RuntimeException(e); } NodeState target = store.getRoot(); target.compareAgainstBaseState(base, new ApplyDiff(builder)); } UserInitializer(@NotNull SecurityProvider securityProvider); @Override void initialize(NodeBuilder builder, String workspaceName); @Override void setQueryIndexProvider(QueryIndexProvider provider);  }
@Test public void testSecondInit() { NodeBuilder builder = spy(getTreeProvider().asNodeState(root.getTree(PathUtils.ROOT_PATH)).builder()); UserInitializer ui = new UserInitializer(getSecurityProvider()); ui.initialize(builder, adminSession.getWorkspaceName()); verify(builder, never()).child(anyString()); }