X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FContentUtils.java;h=17144cfdb660cfceaf84c54016b46b6f44c1ac8e;hb=1d9f61172318be0dbf7f219d140ba01c4c29cdc9;hp=272a5c5a148d3f51b3315fad79918e4837aed3c9;hpb=4c7e1885b8bf3c93fa0919ace122e3f289a925ea;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java index 272a5c5a1..17144cfdb 100644 --- a/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/acr/ContentUtils.java @@ -3,11 +3,22 @@ package org.argeo.cms.acr; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; +import java.util.StringJoiner; import java.util.function.BiConsumer; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; import javax.xml.namespace.QName; import org.argeo.api.acr.Content; +import org.argeo.api.acr.ContentRepository; +import org.argeo.api.acr.ContentSession; +import org.argeo.api.acr.CrName; +import org.argeo.api.cms.CmsAuth; +import org.argeo.cms.CmsUserManager; +import org.argeo.osgi.useradmin.UserDirectory; +import org.argeo.util.CurrentSubject; +import org.osgi.service.useradmin.Role; /** Utilities and routines around {@link Content}. */ public class ContentUtils { @@ -78,6 +89,13 @@ public class ContentUtils { path.substring(parentIndex + 1) }; } + public static String toPath(List segments) { + // TODO checks + StringJoiner sj = new StringJoiner("/"); + segments.forEach((s) -> sj.add(s)); + return sj.toString(); + } + public static List toPathSegments(String path) { List res = new ArrayList<>(); if ("".equals(path) || ROOT_SLASH.equals(path)) @@ -101,6 +119,53 @@ public class ContentUtils { throw new IllegalArgumentException("Path " + path + " contains //"); } + public static Content roleToContent(CmsUserManager userManager, ContentSession contentSession, Role role) { + UserDirectory userDirectory = userManager.getDirectory(role); + String path = CmsContentRepository.DIRECTORY_BASE + SLASH + userDirectory.getName() + SLASH + + userDirectory.getRolePath(role); + Content content = contentSession.get(path); + return content; + } + + /* + * CONSUMER UTILS + */ + + public static Content createCollections(ContentSession session, String path) { + if (session.exists(path)) { + Content content = session.get(path); + if (!content.isContentClass(CrName.collection.qName())) { + throw new IllegalStateException("Content " + path + " already exists, but is not a collection"); + } else { + return content; + } + } else { + String[] parentPath = getParentPath(path); + Content parent = createCollections(session, parentPath[0]); + Content content = parent.add(parentPath[1], CrName.collection.qName()); + return content; + } + } + + public static ContentSession openDataAdminSession(ContentRepository repository) { + LoginContext loginContext; + try { + loginContext = CmsAuth.DATA_ADMIN.newLoginContext(); + loginContext.login(); + } catch (LoginException e1) { + throw new RuntimeException("Could not login as data admin", e1); + } finally { + } + + ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(ContentUtils.class.getClassLoader()); + return CurrentSubject.callAs(loginContext.getSubject(), () -> repository.get()); + } finally { + Thread.currentThread().setContextClassLoader(currentCl); + } + } + /** Singleton. */ private ContentUtils() {