Skip to content

Instantly share code, notes, and snippets.

@a-blank-slate
Created September 21, 2023 14:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save a-blank-slate/c5b63d250761f16dbef336f9f443af9a to your computer and use it in GitHub Desktop.
Groovy script to add rep policy for the results pulled in from xpath query
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import javax.jcr.*;
def queryManager = session.workspace.queryManager;
def statement = "/jcr:root/content/geeksdemo//*[(jcr:content/@cq:template = '/conf/geeksdemo/settings/wcm/templates/page')]"
def query = queryManager.createQuery(statement, 'xpath')
final def result = query.execute()
result.nodes.each {
node ->
AccessControlManager accessControlManager = session.getAccessControlManager();
JackrabbitAccessControlList specificPathGroupAcl = AccessControlUtils.getAccessControlList(session, node.path);
if (specificPathGroupAcl != null) {
JackrabbitSession jackrabbitSession = (JackrabbitSession) session;
PrincipalManager principalManager = jackrabbitSession.getPrincipalManager();
Principal principal = principalManager.getPrincipal("admin-test");
Privilege[] privileges = AccessControlUtils.privilegesFromNames(session, Privilege.JCR_REMOVE_NODE);
specificPathGroupAcl.addEntry(principal, privileges, false);
accessControlManager.setPolicy(specificPathGroupAcl.getPath(), specificPathGroupAcl);
session.save();
println "rep policy added";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment